Skip to content

Instantly share code, notes, and snippets.

@stevebuik
Created December 26, 2020 02:21
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save stevebuik/e4f3475e46dd5ebb1de7707438fa073f to your computer and use it in GitHub Desktop.
Save stevebuik/e4f3475e46dd5ebb1de7707438fa073f to your computer and use it in GitHub Desktop.
slow recursive validation in Malli
(ns recursive-malli-perf
(:require [malli.core :as m]
[criterium.core :as criterium]))
(def simple-node
[:map
[:name string?]])
(def simple-tree-node
[:and simple-node
[:map {:registry {::child simple-node}}
[:children {:optional true} [:vector [:ref ::child]]]]])
(def complex-node
[:and simple-node
[:map
[:phone {:optional true} string?]
[:weight {:optional true} pos-int?]
[:address [:map
[:street string?]
[:city string?]
[:postcode pos-int?]]]]])
(def complex-tree-node
[:and complex-node
[:map {:registry {::child complex-node}}
[:children {:optional true} [:vector [:ref ::child]]]]])
(def more-complex-node
[:and complex-node
[:map
[:work-address [:map
[:street string?]
[:city string?]
[:postcode pos-int?]]
:delivery-address [:map
[:street string?]
[:city string?]
[:postcode pos-int?]]
:holiday-address [:map
[:street string?]
[:city string?]
[:postcode pos-int?]]
:emergency-address [:map
[:street string?]
[:city string?]
[:postcode pos-int?]]]]])
(def more-complex-tree-node
[:and more-complex-node
[:map {:registry {::child more-complex-node}}
[:children {:optional true} [:vector [:ref ::child]]]]])
(def sample1 {:name "foo"})
(comment
; not recursive
(let [validator (m/validator simple-node)]
(criterium/quick-bench (validator sample1)))
; simple recursive : 1.9 x slower
(let [validator (m/validator simple-tree-node)]
(criterium/quick-bench (validator sample1)))
; complex recursive : 2.0 x slower
(let [validator (m/validator complex-tree-node)]
(criterium/quick-bench (validator sample1)))
; more complex recursive : 2.16 x slower
(let [validator (m/validator more-complex-tree-node)]
(criterium/quick-bench (validator sample1))))
; Observations:
; looks like time scales with schema complexity
; this makes sense if children are present but not when no recursion
; Q: why does simple recursion add so much when there are no children present?
; Q: why does increased schema complexity add work when there are no children present?
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment