plumatic / schema

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

Idea: Cache some `defschema` specs #456

Closed frenchy64 closed 4 months ago

frenchy64 commented 4 months ago

I've been experimenting with caching the results of the Schema protocols. This seemed like promising low-hanging fruit, short-circuiting the need to reparse non-metadata schemas declared via defschema.

There would need to be a way to return the original syntax, but this explains the gist of the idea.

Sometimes the Node tests fail on this branch, haven't figured it out yet.

(comment
(defschema Bar (nth (iterate #(vector #{%}) Integer) 100))
(dotimes [_ 10]
  (time (spec Bar)))
;="Elapsed time: 0.926795 msecs"
;="Elapsed time: 0.055069 msecs"
;="Elapsed time: 0.02755 msecs"
;="Elapsed time: 0.02287 msecs"
;="Elapsed time: 0.018021 msecs"
;="Elapsed time: 0.020887 msecs"
;="Elapsed time: 0.023733 msecs"
;="Elapsed time: 0.02642 msecs"
;="Elapsed time: 0.022624 msecs"
;="Elapsed time: 0.034662 msecs"
(dotimes [_ 10]
  (time (spec (nth (iterate #(vector #{%}) Integer) 100))))
;="Elapsed time: 0.298001 msecs"
;="Elapsed time: 0.171665 msecs"
;="Elapsed time: 0.231007 msecs"
;="Elapsed time: 0.324284 msecs"
;="Elapsed time: 0.105173 msecs"
;="Elapsed time: 0.083637 msecs"
;="Elapsed time: 0.090645 msecs"
;="Elapsed time: 0.087503 msecs"
;="Elapsed time: 0.098164 msecs"
;="Elapsed time: 0.06527 msecs"
  )
frenchy64 commented 4 months ago

This doesn't seem to give any speedup.