Skip to content

Commit

Permalink
Reduce the z/sexpr cost in find-namespace
Browse files Browse the repository at this point in the history
This patch addresses the slowdown introduced between 0.6.4 and 0.6.5
and fixes weavejester#181.

Prior to this patch, find-namespace and ns-form? called z/sexpr on
all traversed forms. The find-namespace logic needed only parts of
resulting sexprs.

The cost of z/sexpr was non-negligible when invoked on complex forms.
As a result, z/sexpr and nested functions dominated the CPU cost of
the indentation logic.

This patch makes z/sexpr calls more fine-granular and avoids invoking
the function on nodes irrelevant to find-namespace. This results in a
tenfold speedup.
  • Loading branch information
jstepien authored and lread committed Jun 15, 2020
1 parent 5abe96c commit 7aa0fed
Showing 1 changed file with 9 additions and 5 deletions.
14 changes: 9 additions & 5 deletions cljfmt/src/cljfmt/core.cljc
Expand Up @@ -192,21 +192,25 @@
(take-while (complement root?))
last)))

(defn- token? [zloc]
(= (z/tag zloc) :token))

(defn- ns-token? [zloc]
(and (token? zloc)
(= 'ns (z/sexpr zloc))))

(defn- ns-form? [zloc]
(some-> zloc z/child-sexprs first (= 'ns)))
(some-> zloc z/down ns-token?))

(defn- find-namespace [zloc]
(some-> zloc top (z/find z/next ns-form?) z/sexpr second))
(some-> zloc top (z/find z/next ns-form?) z/down z/next z/sexpr))

(defn- indent-matches? [key sym]
(if (symbol? sym)
(cond
(symbol? key) (= key sym)
(pattern? key) (re-find key (str sym)))))

(defn- token? [zloc]
(= (z/tag zloc) :token))

(defn- token-value [zloc]
(if (token? zloc) (z/sexpr zloc)))

Expand Down

0 comments on commit 7aa0fed

Please sign in to comment.