Parsing JSON Objects without intermediate ASTs

23 points by abhin4v a day ago on lobsters | 4 comments

tobz619 | a day ago

Oooh that's quite interesting.

It feels a lot like a raw stack allocation with uninitialised fields. Passing a bitset to check that all fields are set is so simple and effective.

I do wonder if this pattern can be generalised through Haskell via some wrapper type and the bitset could be hidden.

I've been trying to implement a similar approach for a web app I've been working to make sure a policy is uploaded by one client by the time another client needs.

Nice to know I wasn't totally insane for trying it out haha :D

morj | a day ago

Cool approach. Defining the target records with lazy fields is likely unacceptable to the users, but this could likely be saved by first deserializing into a tuple, and then spilling this tuple into the record, code for this being produced by TH or generics; I hope the optimizer will be able to see through the tuple and make the transformation free

This shows that skipping the intermediary repr gives a 3x speedup. I once did a mini-benchmark of aeson vs serde_json, and the difference was 9x. Now it remains to recover the second 3x improvement and we would have officially proven that haskell is the superior of languages

vamolessa | a day ago

related, it's also possible to implement an "immediate mode" json read/write even in c and with reasonable ergonomics: https://git.sr.ht/~lessa/foundation/tree/master/item/test/json_test.c#L83

I thought aeson skips the AST too nowadays if you hold it right. For encoding there’s toEncoding instead of toJSON and for decoding decode uses a fairly efficient tokenizer. But indeed, fromJSON gives you a Value so the AST is constructed.