Simdjson 0.3: Faster JSON parser
lemire.me
lemire.me
[1] https://github.com/simd-lite/simd-json [2] https://github.com/simdjson/simdjson/issues/10
Citation needed.
And seeing how AMD is already a faster and better CPU in general (I mean the latest AMD Ryzen 3000 vs Intel 9000 series, at time of writing this), I'm not sure what about that needs citation?
Safety can be roughly estimated by a number of critical vulnerabilities. Most have hit Intel, some being Intel specific bugs.
Alternatively, maybe their support for AMD is nascent and optimizations for Intel absent on AMD.
There's really no substitute for benchmarking AWS' actual offerings.
https://news.ycombinator.com/item?id=19214387
The inability to handle embedded NULs (issue #40) felt like cheating, but that has been fixed.
It looks like I can fetch a number as a string, so I can read into a decimal type. I'll have to try that. Edit: nope, doesn't work. "The JSON element does not have the requested type."
Also, iirc the biggest negative of this library last time was that the API was super unintuitive (compared to, say, nlohmann). Glad to see progress has been made here.
Would love to see a quality python wrapper for this library.
Parsing is notoriously serial and branchy, which is what makes simdjson so out of the ordinary. It's using a sort of "microparallel algorithm," running a small parallel parse on a SIMD-sized chunk of JSON (16-32 bytes depending on architecture), and then moving to the next.
And yeah, you have to go back over a decade to find CPUs that don't have SIMD. simdjson runs on those too, just obviously doesn't use SIMD instructions :)
An interesting point with the design of simdjson loses its branchlessness in "stage 2". I originally had a bunch of very elaborate plans to try to stay branchless far further into the parsing process. It proved just too hard to make it work. There were some promising things that ultra-modern Intel chips - meaning Icelake - and future iterations of ARM (SVE/SVE2) - are adding to their SIMD abilities, so it might be worth revisiting this in a few years (there aren't too many Icelake boxes out there and SVE barely exists).
Making it so you can handle all the brackets at once, all the strings at once, all the numbers at once, would make a big difference, and we're thinking about that. Another thing that could help is making the if statement more predictable using type information from the user. get<int>() could mean "I expect this next thing to be an integer, so parse it that way and just yell if it's not, please."
It's difficult. But it's why I'm still so fascinated! Solving JSON thoroughly and completely will give us a lot of information on how to quickly parse XML, YAML, and other file formats.
We've clearly been collectively doing parsing wrong (including me) if there's this much of a gap. It's exciting to see something innovative and new in this domain and even being able to contribute to it :) @lemire deserves a ton of credit for making an actual project out of his work and promoting it; I likely wouldn't have heard of it otherwise.
There used to be 4 stages (stage 1 was the marks, stage 2 was bits-to-indexes, stage 3 was the tape construction and stage 4 was the 'clean everything up and do all the hard stuff'). It's possible - though awkward - to do tape construction branchlessly, but the gyrations required were expensive and weird and it just delayed the reckoning.
I built a prototype of the 'gather all the X at once and handle it in one go' and the logic to gather that stuff was more expensive than just handling everything.
In my wacky world of branch free coding (which I've been doing a while) there are worse things than missing a branch. The idea that you can accumulate an array branchlessly (i.e. always put the thing you have in a location somewhere and bump a pointer when you need to) seems pretty cool, but branch miss is not the only hazard. This technique of branchlessly writing a log is a anti-pattern I've tried over and over again and a stream of unpredictable writes is just as big a pain as a bunch of unpredictable branches - it causes the pipeline to come to a screaming halt. If you can get it going somehow (new SIMD tricks? better algorithm?) I'd be impressed.
Get my email from Daniel or hit me up in Twitter DMs if you want to discuss further.
It's also supported by quite a few libraries[4] and tools[5] too -- plus many others that don't document it as being ndjson/jsonl (such as AWS CloudTrail logs). I've got support for it in the non-POSIX UNIX/Linux shell I'd written too[6]
The issue is really more that, like with comments, JSON doesn't support streaming nor even multiple documents (something formats like YAML already have built into the spec) so it becomes something you need to advertise if you're writing a parser simply because it's not part of the standard JSON specification.
[1] https://en.wikipedia.org/wiki/JSON_streaming#Line-delimited_...
[4] http://ndjson.org/libraries.html
And API wise filter hooks are needed to skip unneeded arrays or objects. This would also save a lot of unneeded buffering.
I absolutely love that the first comment I see coming into the discussion is someone arrogantly telling (us, not the author) how to do their job, when evidently we're looking at one of the most thought-out, better engineered pieces of software there is in their area (JSON parsing).
What's more, the authors of simdjson show a lot of benchmarks, explain how they got there, they provide their effort in a liberal license, and yet... This peep over here comes to tell us how he thinks that's wrong...
Just got a bit ranty. And sorry eska for taking a bit of your comment for it. You tried to give some sense into this guy.
Edit: at first I thought this was the top comment because of being newer... Nope, it's the top comment alltogether. Followed by another comment about the same time as old of someone telling how great this library is and how they're using it in production with a rust wrapper.
Note that there are real speed gains to be had by not being selective. The CPU is normally sprinting ahead, executing future instructions before you even get to them, and every branch can make it trip and land flat on its face.
We've found we have to be very careful with every branch we introduce. I've tried to introduce branches that skip tons of code and ought to make things a ton faster, but which instead slow it down.
Parsing is the neglible fast part, converting it into your business objects is the slow part. This is usually done with the 2nd tape step. This involves a lot of malloc's, and you really want to skip the unneeded parts and filter it upfront.
Still thinking how to design my new JSON parser around simdjson.
Although I would really love to see a more zero-parse binary format. At the end of the day simd, is just making markers in the original file of where an array begins, object begins, each of the object key value ranges e.t.c i.e the find marks -> build tape algorithm.
This could be achieved by having length prefixed values, kinda like flat buffers which is zero parse time, and much compact than json. https://google.github.io/flatbuffers/
I just wish json had a more universal alternative binary format which was insanely fast and compact to work with.
Protobufs are nice but parsing large files is still a pain and consumes a ton of memory. Ideally a format that could simply be lazy mmap-ed would be desirable.
---
> The work is supported by the Natural Sciences and Engineering Research Council of Canada under grant number RGPIN-2017-03910.
Well, thank you Canada!
simdjson UTF8-validation, exact numbers: 2.5 GB/s
RapidJSON insitu, UTF8-validation: 0.41 GB/s
RapidJSON insitu, UTF8-validation, exact numbers: 0.39 GB/s
RapidJSON UTF8-validation: 0.29 GB/s
RapidJSON UTF8-validation, exact numbers: 0.28 GB/s
It's also still faster than other parsers in our tests, even on the smallest documents (https://github.com/simdjson/simdjson/issues/312#issuecomment... advantage is just smaller, like 1.1x and 1.2x instead of 2.5x :) It really starts to pull ahead somewhere between 512 bytes and 1K.
I keep meaning to look into SIMD for both this and ordinary tokenisation, last time I tried it the bookkeeping overheads and alignment requirements weren't worth the speedup but it's always worth re-evaluating.
https://github.com/v8/v8/blob/master/src/json/json-parser.cc
Meanwhile I can barely get Chrome/NodeJS to parse 20MB in less than 100ms :(.
How useful (or useless) would Simdjson as a Native Addon to V8 be? I assume transferring the object into JS land would kill all the speed gains?
I wrote my own JSON parser just last week, to see if I could improve the NodeJS situation. Discovered some really interesting factoids:
(A) JSON parse is CPU-blocking, so if you get a large object, your server cannot handle any other web request until it finishes parsing, this sucks.
(B) At first I fixed this by using setImmediate/shim, but discovered to annoying issues:
(1) Scheduling too many setImmediates will cause the event loop to block at the "check" cycle, you actually have to load balance across turns in the event loop like so (https://twitter.com/marknadal/status/1242476619752591360)
(2) Doing the above will cause your code to be way slow, so a trick instead, is to actually skip setImmediate and invoke your code 3333 (some divider of NodeJS's ~11K stack depth limit) times or for 1ms before doing a real setImmediate.
(C) Now that we can parse without blocking, our parser's while loop (https://github.com/amark/gun/blob/master/lib/yson.js) marches X byte increments at a time (I found 32KB to be a sweet spot, not sure why).
(D) I'm seeing this pure JS parser be ~2.5X slower than native for big complex JSON objects (20MB).
(E) Interestingly enough, I'm seeing 10X~20X faster than native, for parsing JSON records that have large values (ex, embedded image, etc.).
(F) Why? This happened when I switched my parser to skip per-byte checks when encountering `"` to next indexOf. So it would seem V8's built in JSON parser is still checking every character for a token which slows it down?
(G) I hate switch statements, but woah, I got a minor but noticeable speed boost going from if/else token checks to a switch statement.
Happy to answer any other Qs!
But compared to OP's 2.5GB/s parsing?! Ha, mine is a joke.
Q: What happens when you parse "\\" ?
> JSON parse is CPU-blocking, so if you get a large object, your server cannot handle any other web request until it finishes parsing
Well, your CPU core is busy on one request or another, so I don't understand why this is an issue as long as you're guarding against maliciously large bodies. Blocking I/O is different because your core is partially idle while other hardware is doing async work. Using Node.js' cluster module lets you keep more cores busy. Chunking CPU-limited work increases total CPU time and memory required. (This is a pet peeve of mine and a hill I'm willing to die on :-) .)