Fork me on GitHub
#clojure-spec
<
2023-09-21
>
vemv09:09:12

(One of those "just curious" questions 🙂) Is there an intrinsic reason why spec (1) is slower than Malli? It's relatively common to hear that it's not performant enough to be used in something like a web handler. However it doesn't sound too crazy to implement some sort of drop-in replacement for a few key functions like conform. One would still use the regular spec/def for definitions - the custom checker could simply read the original registry.

nikolavojicic10:09:52

Never had a significantly slow spec such that it matters for web app... For data generation yes, it can be slow and can be fixed by tweaking generators. Is there an example?

vemv11:09:53

'That matters' is the tricky part... for many webapps a 1ms delay doesn't appear to matter, but if you are trying to serve thousands of requests per second, it becomes the bottleneck

seancorfield15:09:43

We use conform heavily in our main REST API at work and it's "fast enough". I guess if you have an app where Spec's performance matters and Malli satisfies while Spec does not, then use Malli? We've never seen Spec as a bottleneck. JDBC stuff, yes. Elastic Search stuff, yes. Spec, never.

vemv15:09:24

I once participated in a project where we used, roughly speaking, spec for code (fdefs and the like), and malli for data. Wasn't bad, I might even enjoy exercising all those skills on a good day, but it doesn't seem ideal. I'm a spec1 fan and would rather use it everywhere... but its performance is theoretically inferior. Again, you can say the same about e.g. Rails or Python web servers. In reality CPU is rarely the bottleneck, as you hint... but some of us enjoy squeezing the last drop of performance. Knowing it can do fast coercion, validation, etc can help in the occasional team-wide decision making processes.

vemv15:09:58

Reflecting a bit, a later project used Malli in a more advanced way. I had the impression that you could go as far as building "your own spec1" but in Malli terms. Would seem a clearer path than hacking on spec1, as it is a stalled project.

jasonjckn22:09:56

w.r.t conform vs decode/encode in malli a lot more optimization work is done at 'compile time' (in the sense of constructing a chain of lambda functions to arrive at a a parser) e.g. in malli , it's possible your decoder/conform compiles to an identity function

jasonjckn22:09:55

This is partly due to different designs, because the construction of the decoder/parser takes into account the structure of the input, if you know you're only trying to create morphisms between JSON <-> EDN , you can optimize more

jasonjckn22:09:53

Personally, i'm a huge fan of malli, i think its closer to rich hickey's definition of simplicity than spec version 1, since specs/schema are just data, ... this could change with spec version 2, but i'm a convert for now.