metosin / malli

High-performance data-driven data specification library for Clojure/Script.
Eclipse Public License 2.0
1.44k stars 204 forks source link

Improve map gen creation performance #948

Closed bsless closed 10 months ago

bsless commented 10 months ago

This MR improves the performance for creating a map gen ~x5, mostly by removing a call to satisfies?, and further by choosing performant functions in the implementation.

ikitommi commented 10 months ago

Thanks for the improvements! This ~only~ mainly effects (e.g. satifies?) the case where the generator is not cached, right?

;; no cache
(mg/generate [:map [:x :int] [:y :int]])

;; instance cache
(let [s (m/schema [:map [:x :int] [:y :int]])]
  (mg/generate s))

;; cached function
(let [g (mg/generator [:map [:x :int] [:y :int]])]
  (mg/generate g))
ikitommi commented 10 months ago

On my machine, merging this. Thanks!!

(p/bench (mg/generate [:map [:x :int] [:y :int]]))
;; 18,5µs => 5,6 µs (-70%)

(let [s (m/schema [:map [:x :int] [:y :int]])]
  (p/bench (mg/generate s)))
;; => 3.3µs => 2.8µs (-15%)

(let [g (mg/generator [:map [:x :int] [:y :int]])]
  (p/bench (mg/generate g)))
;; => 2.8µs => 2.6µs (-8%)
bsless commented 10 months ago

Gladly :) For reference, this is the use case that made me care about construction performance https://github.com/bsless/fsm-test-check/blob/master/examples/add_delete_tx_malli.clj