Skip to content

Commit c1ff3fe

Browse files
authored
Optimize ast-stats (#397)
Most of all, I was worried about `(keys asts)` and `(vals asts)` used in a coupled manner. It's plausible that their returned order may be inconsistent across those two individual invocations.
1 parent 0a9a6ee commit c1ff3fe

File tree

2 files changed

+26
-7
lines changed

2 files changed

+26
-7
lines changed

src/refactor_nrepl/analyzer.clj

Lines changed: 14 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -130,13 +130,20 @@
130130
ast-or-err)))
131131

132132
(defn- ast-stats []
133-
(let [asts @ast-cache]
134-
(interleave (keys asts)
135-
(->> (vals asts)
136-
(mapcat vals)
137-
(map #(if (instance? Throwable %)
138-
(list "error" (.getMessage ^Throwable %))
139-
"OK"))))))
133+
(let [asts @ast-cache
134+
map-entries (seq asts)]
135+
(reduce (fn [acc [k v]]
136+
(conj acc
137+
(->> v
138+
vals
139+
(reduce (fn [init x]
140+
(if (instance? Throwable x)
141+
(reduced (list "error" (.getMessage ^Throwable x)))
142+
init))
143+
"OK"))
144+
k))
145+
()
146+
map-entries)))
140147

141148
(defn warm-ast-cache []
142149
(doseq [f (tracker/project-files-in-topo-order true)]

test/refactor_nrepl/analyzer_test.clj

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
(ns refactor-nrepl.analyzer-test
22
(:require
33
[clojure.java.io :as io]
4+
[clojure.pprint :as pprint]
45
[clojure.test :refer [deftest is]]
56
[refactor-nrepl.analyzer :as sut]))
67

@@ -9,3 +10,14 @@
910
"clashing_defprotocol_method_name.clj"]
1011
:let [c (-> f io/resource slurp)]]
1112
(is (some? (sut/ns-ast c)))))
13+
14+
(deftest warm-ast-cache-test
15+
(let [pairs (partition 2 (sut/warm-ast-cache))]
16+
(when (System/getenv "CI")
17+
(pprint/pprint pairs))
18+
(doseq [[ns-sym result] pairs]
19+
(is (simple-symbol? ns-sym))
20+
(is (or (= "OK" result)
21+
(and (list? result)
22+
(-> result first #{"error"})
23+
(-> result second string?)))))))

0 commit comments

Comments
 (0)