From 7aa0fed93b1bd1b9708841bf928edba3ed09ac04 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Jan=20St=C4=99pie=C5=84?= Date: Sun, 16 Feb 2020 09:43:06 +0100 Subject: [PATCH] Reduce the z/sexpr cost in find-namespace This patch addresses the slowdown introduced between 0.6.4 and 0.6.5 and fixes #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. --- cljfmt/src/cljfmt/core.cljc | 14 +++++++++----- 1 file changed, 9 insertions(+), 5 deletions(-) diff --git a/cljfmt/src/cljfmt/core.cljc b/cljfmt/src/cljfmt/core.cljc index c3299d92..68b5bc1d 100644 --- a/cljfmt/src/cljfmt/core.cljc +++ b/cljfmt/src/cljfmt/core.cljc @@ -192,11 +192,18 @@ (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) @@ -204,9 +211,6 @@ (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)))