Fork me on GitHub
#malli
<
2022-09-22
>
Ben Sless16:09:29

Found some performance improvement opportunities around parsing, especially around regex parsers. Also, trying to fold and/or parsers, but it's confusing 🙃

ikitommi17:09:13

looking forward!

Ben Sless17:09:14

Would appreciate a pointer regarding what I'm getting wrong with orn this is the or parser:

(defn- -or->parser
  [children]
  (fn [f]
    (let [parsers (-vmap f children)]
      #?(:clj
         (reduce
          (fn [acc parser]
            (fn [data]
              (let [parsed (acc data)]
                (if (miu/-invalid? parsed)
                  (parser data)
                  parsed))))
          (fn [_] ::invalid)
          (reverse parsers))
         :cljs
         #(reduce (fn [_ parser] (miu/-map-valid reduced (parser %))) ::invalid parsers)))))
And the orn parser doesn't work
(defn- -orn->parser
  [this]
  #?(:clj
     (reduce
      (fn [acc [k _ c]]
        (let [parser (-parser c)]
          (fn [data]
            (let [parsed (acc data)]
              (if (miu/-invalid? parsed)
                (parser data)
                (miu/-tagged k parsed))))))
      (fn [_] ::invalid)
      (reverse (-children this)))
     :cljs
     (let [parsers (-vmap (fn [[k _ c]]
                            (let [c (-parser c)]
                              (fn [x] (miu/-map-valid #(reduced (miu/-tagged k %)) (c x)))))
                          (-children this))]
       (fn [x] (reduce (fn [_ parser] (parser x)) x parsers)))))

Ben Sless06:09:27

Anyway, first MR which I'm sure of submitted. Would you like to see before/after comparisons?

Ben Sless17:09:37

I decided to break things apart and start with the improvements I'm sure of https://github.com/metosin/malli/pull/756

Ben Sless17:09:38

One radical thing I've considered trying is pivoting the values array in the Cache to as many arrays as CacheEntry has fields, avoids allocation and makes the entire thing cache friendly

dvingo18:09:12

after a comment by Thomas Heller (https://github.com/metosin/malli/pull/754#issuecomment-1252581659) I refactored the cljs function instrumentation https://github.com/metosin/malli/pull/755 to happen all in cljs with only collecting schemas happening in a macro. The benefits include the namespaces matching with the clojure versions (aside from malli.dev.cljs), a simpler mental model as instrumentation doesn't occur via generated code, and a more straight forward implementation that matches clojure's pretty closely.

kenny20:09:23

When working with Malli and generators, how do folks typically handle lazy loading of test.check?

kenny01:09:39

Cool lib :thumbsup::skin-tone-2: How do you set the generator on a schema to be dynamically loaded? i.e., if I'm setting :gen/gen key, it'd need to be loaded when setting it.

dvingo12:09:24

i haven't done it myself, but perhaps walking the schema dynamically to produce a new schema? https://github.com/metosin/malli#programming-with-schemas the "Adding generated example values to Schemas" section in particular