Fork me on GitHub
#malli
<
2020-12-26
>
steveb8n02:12:43

I’ve started switching out Spec for Malli to improve performance in hot code. it works great except when using recursive schemas. I’ve created a gist to illustrate https://gist.github.com/stevebuik/e4f3475e46dd5ebb1de7707438fa073f

steveb8n02:12:06

anyone seen this before? Or am I making a mistake somehow?

ikitommi09:12:05

@steveb8n you are using :and, which composes the two maps - in validation it runs two validations, one for each map. If you use malli.util/merge, the end result is one schema, and validation runs it just once. tested the first example, it’s 90ns vs 97ns, which is the cost of iterating over 2 MapEntrys instead of just one.

(require '[malli.util :as mu])

(def simple-tree-node
  (mu/merge
    simple-node
    [:map {:registry {::child simple-node}}
     [:children {:optional true} [:vector [:ref ::child]]]]))

steveb8n11:12:58

Now that's a Xmas gift! I'll try it tomorrow. Thanks

ikitommi10:12:02

one note about merge: both the properties and children are merged (no deep merge atm), so the last :registry property wins. hadn’t thought of that :thinking_face:

ikitommi10:12:32

but, here, it’s:

simple-tree-node
;[:map
; {:registry #:recursive-malli-perf{:child [:map [:name string?]]}}
; [:name string?]
; [:children {:optional true} [:vector [:ref :recursive-malli-perf/child]]]]