plumatic / schema

Clojure(Script) library for declarative data description and validation
Other
2.4k stars 256 forks source link

Performance of `instance-precondition` on JVM #454

Open frenchy64 opened 4 months ago

frenchy64 commented 4 months ago

Clojure compiles (let [cls String] (instance? klass "a")) less optimally than (instance? String "a"). This affects this line, which I imagine is a pretty hot code path in schema usage:

https://github.com/plumatic/schema/blob/8fcb57f87ffe74185e1f1da72be4933bd4aad4d2/src/cljc/schema/core.cljc#L166

The perf difference seems roughly 2x.

examples.server=> (time (dotimes [_ 10000] (let [cls String] (instance? cls "a"))))
"Elapsed time: 1.142388 msecs"
nil
examples.server=> (time (dotimes [_ 10000] (instance? String "a")))
"Elapsed time: 0.411797 msecs"
nil