malli

https://github.com/metosin/malli :malli:
steveb8n 2020-12-26T02:23:43.268100Z

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

steveb8n 2020-12-26T02:24:06.268200Z

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

ikitommi 2020-12-26T09:56:05.272Z

@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]]]]))

ikitommi 2020-12-26T10:01:02.273700Z

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:

ikitommi 2020-12-26T10:01:32.274Z

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]]]]

steveb8n 2020-12-26T11:35:58.274100Z

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