From 4c6ff53fcd181a94df992bc40a7cbb25e2fde7d9 Mon Sep 17 00:00:00 2001 From: Greg Dennis Date: Thu, 18 Apr 2024 14:53:24 +1200 Subject: [PATCH] perf enhancements for logic --- .jekyll-metadata | Bin 24775 -> 25920 bytes .../2024/2024-04-17-logic-without-models.md | 53 ++++++++++++++++++ 2 files changed, 53 insertions(+) create mode 100644 _posts/2024/2024-04-17-logic-without-models.md diff --git a/.jekyll-metadata b/.jekyll-metadata index b59c459792f92ab607f01a92c8cb8a583d7d448f..1a33f67f40382f3789c6397243bc8e348c893b66 100644 GIT binary patch delta 416 zcmX?pknzANMs^mC>R?YLugMq96(=`H3vINr3ga(hm2WWkb;yd{nk$p(z>3Wer7gG^ zL!e5yCO3p~c$?@NnCs@`r)MVXmS>h^G{=H{oQ<`nDYrm$HvgjBOTaCnwlv++9d z$v3oK(cAzv%bMGP%Yi3a-ht^EqXW}(Abv6VqNo4lS)P2VuYkPQK>QYn-vRM^ApQWv zAA$H25Pt^ZFF@6?ir;{&??C(mh<^g{FChL6#D9SJFA)Em?C-S?)x{Aex`xENSXn`7 ZH<5}N=dDCp<>Jph-OfB=ugMq96(=`H3vINr3gbv-m2W8bkJOobUR-AL3u%2W09&69 A{r~^~ diff --git a/_posts/2024/2024-04-17-logic-without-models.md b/_posts/2024/2024-04-17-logic-without-models.md new file mode 100644 index 0000000..80134e6 --- /dev/null +++ b/_posts/2024/2024-04-17-logic-without-models.md @@ -0,0 +1,53 @@ +--- +title: "JSON Logic Without Models" +date: 2024-04-18 09:00:00 +1200 +tags: [json-logic, architecture, performance] +toc: true +pin: false +--- + +Holy performance increase, Batman! + +I recently made an update to _JsonLogic.Net_ that cut run times and memory usage **in half**! + +## In half?! + +Yes! Here's the benchmark: + +| Method | Count | Mean | Error | StdDev | Gen0 | Allocated | +|------- |------ |-------------:|------------:|------------:|-----------:|------------:| +| Models | 1 | 1,655.9 us | 26.76 us | 26.28 us | 410.1563 | 838.03 KB | +| Nodes | 1 | 734.5 us | 8.16 us | 7.23 us | 236.3281 | 482.61 KB | +| Models | 10 | 16,269.0 us | 167.06 us | 139.50 us | 4093.7500 | 8380.5 KB | +| Nodes | 10 | 7,210.7 us | 25.26 us | 21.09 us | 2359.3750 | 4826.08 KB | +| Models | 100 | 164,267.3 us | 2,227.54 us | 1,974.66 us | 41000.0000 | 83803.81 KB | +| Nodes | 100 | 72,195.7 us | 139.28 us | 116.30 us | 23571.4286 | 48262.05 KB | + +In this table, "Models" is the old way, and "Nodes" is the new way. + +As you can see, "Nodes" takes less than half as long to run, and it uses just over half the memory. + +## What do "Models" and "Nodes" represent? + +From the initial release of the library, JSON Logic is represented using its own object model via the `Rule` abstraction. It would result in a large tree structure of strongly typed rules. This is "Models". + +The benefit of this approach is that strong typing, meaning that if you wanted to build some logic in code, you could use the associated builder methods on the static `JsonLogic` class and you didn't have to worry about getting argument types wrong. + +However, as you can expect, building out this rule tree means heap allocations, and allocations, in general, are slow. + +The "Nodes" approach, introduced with v5.2.0, doesn't use the object model. Instead, the system is stateless. It uses `JsonNode` to represent the logic, and the system runs "static" handlers depending on which operation key is present. This is the approach that I took with JSON-e, and it worked out so well that I wanted to see where else I could apply it. + +> I've had several attempts at making this approach for JSON Schema, and while it works, the performance isn't there yet. +{: .prompt-info} + +JSON-e and JSON Logic also share a common basic design: they're both JSON representations of instructions that are processed with some kind of context data. + +## So no more strong typing? + +I think that's where I want to take this library. With all of the soft typing and implicit conversions that JSON Logic uses anyway, I don't think it's going to be much of a problem for users. + +Even on the [JSON Logic playground](https://jsonlogic.com/), you enter your logic and data as JSON and it runs from there. I don't see why this library can't work the same way. + +I don't really see a reason to need an object model. (And with functional programming on the rise, maybe this stateless approach is the way of the future.) + +But ultimately, it comes down to you. Have a play with the new setup. The [docs](https://docs.json-everything.net/logic/basics/) are already updated. I'd like to hear what you think.