-
Notifications
You must be signed in to change notification settings - Fork 223
Cache validators recursively #1180
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
base: master
Are you sure you want to change the base?
Conversation
There's something bugging me about the dynamism being lost here. For example, does this interfere with dynamically scoped registries in a way that matters? |
I think it does. I would assume that I can recreate a schema validator with current bindings when calling |
I'm not sure that holds even today, the caching of (ns malli.registry-test
(:require [clojure.test :refer [deftest is testing]]
[malli.core :as m]
[malli.registry :as mr]))
(let [registry* (atom {})
register! (fn [t ?s] (swap! registry* assoc t ?s))
registry (mr/composite-registry
{:map (m/-map-schema)}
(mr/mutable-registry registry*)
(mr/dynamic-registry))
_ (register! :maybe (m/-maybe-schema))
s (binding [mr/*registry* {::foo (m/-string-schema)}]
(m/schema [:map [:maybe [:maybe ::foo]]] {:registry registry}))]
(binding [mr/*registry* {::foo (m/-int-schema)}]
(is ((m/-validator s) {:maybe "sheep"}))
(is (not ((m/-validator s) {:maybe 1}))))) EDIT: it later occurred to me this probably isn't |
We discussed this with Tommi & some Metosin guys. We don't feel good about this PR for a couple of reasons:
|
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
(rescinding my previous approval)
In some cases, we can save a lot of work and memory by pushing in the
-cached
logic further into the schemas. The tests demonstrate a key use-case: a schema that is only ever used as a child.This is the output of the final line of the test on master:
i.e., this PR saves 27 calls to protocol methods, each returning fresh fns.
Moving the
explainer
cache to the entire fn also saves recreating the fn every call.