Welcome! Please see the About page for a little more info on how this works.

+1 vote
in tools.reader by

Tools.reader accepts this symbol: MyClazz:/3 while it does not accept the symbol MyClass:. This seems inconsistent. That's the main subject of this "ask".

Incidentally I noticed that this commit https://github.com/clojure/tools.reader/commit/31adc06cd56753ed5a3cd443cea32158ba68c45c#diff-11ba7fe0994caede16eeb1c6ed52fc3aba112849ce1fd23dff0b9fb72441a09fR111 uses a regex + re-matches to check if the string sym is a 1-sized numeric string. I noticed that this causes a slight performance regression compared to version 1.4.2. I think the check can be implemented using a somewhat more elaborate but more performant check:

  • check if the string is of size 1
  • only then check if that string represents a number between 1 and 9

E.g. this version seems to be roughly 6x faster than the re-matches approach:

user=> (defn array-dim? [^String s] (and (= 1 (.length s)) (some-> (try (Integer/parseInt s) (catch Exception e nil)) pos?)))
#'user/array-dim?
user=> (time (dotimes [i 10000000] (array-dim? "1")))
"Elapsed time: 45.287416 msecs"
user=> (defn array-dim2? [^String s] (re-matches #"[1-9]" s))
#'user/array-dim2?
user=> (time (dotimes [i 10000000] (array-dim2? "1")))
"Elapsed time: 304.30275 msecs"
nil

Note how clojure's own reader implemented it:

https://github.com/clojure/clojure/commit/1fa5b038a434da34f787e3aec56f9cb48ed4dd99#diff-9216bc4fd055e0567fa034ec14a8ba470d36ba5be16418c8f2e2ebe1939bbb21R259

by
This is my fault as I implemented the regex version. Once there's a JIRA ticket, I'll write up a patch.

1 Answer

+1 vote
by
by
Thanks. The perf issue was just a bonus. The main problem was:

Tools.reader accepts this symbol: MyClazz:/3 while it does not accept the symbol MyClass:. This seems inconsistent. That's the main subject of this "ask".
...