#2888·simdjson

Using ondemand::parser to parse a very large JSON array which is compressed

Author: spanshCreated Sep 29, 2026Updated Sep 29, 2026

In the issue you recently resolved in https://github.com/simdjson/simdjson/issues/128 I just spent quite a few hours with this over the last couple of days.

It's so close to being perfect for my use case, but just a little out of reach without quite a lot of acrobatics from my code. Ultimately, I would imagine most people who are processing a file of this sort are processing it compressed (In my case gzip compression).

This leaves 2 options, both of which end up in my case ending up with an istream.

The first option is that I use something like boost::iostreams::filtering_istream to decompress the input and then pass it through to the processor (I have this option in my code, but frequently use the second option to take advantage of multiple processors).

The second option is to decompress the input outside of my program and to the pass the decompressed input in via STDIN, this can then be read in by the STDIN stream.

There are a few other options but they boil down to similar things which don't gracefully support memory mapping.

I tried writing a chunking buffer to read a section of the file into a vector. I would initialise that with a full read from the file/stream and then refill it with whatever was left over after the last buffer was cleared out. I would then call iterate_many on that buffer and look at truncated_bytes() to get the remainder.

I found that I couldn't use simdjson::stream_format::comma_delimited_array because it expected each call to iterate_many to begin with a [ symbol.

Since I was already reading the stream into an array myself it wasn't much extra effort to switch to using simdjson::stream_format::comma_delimited and just strip off the first and last characters myself. Initially this looked very promising, however after seemingly random periods the code would crash with invalid JSON. Eventually I tracked this down to either a bug or simply due to the comment in https://github.com/simdjson/simdjson/blob/master/doc/iterate_many.md#incomplete-streams about being meaningless for comma_delimited (though it seemed to be totally correct in 99% of cases).

Everything seemed to work correctly, except when the buffer ended on the exact boundary between 2 JSON objects. In one specific case which caused the crash the last 2 characters read into the buffer were the trailing } and ,. Then the document was read and processed correctly, but truncated_bytes() returned 2631 (instead of 0 or 1) which was somewhere in the middle of the previous object.

I went around the houses trying iterator.current_index (gives me the start of the last successful document, or the end of the buffer if there was an incomplete document) and a few other things, but there appears to be no graceful way to get the size of the last document or the end of the last successful document.

After the best part of a couple of days trying quite a few options I gave up and decided to post my findings here. I apologise for the wall of text and hopefully I've made it clear what the issue has been.

I could probably make this work by creating a circular buffer of my own which reads the next blocks in the background and presents itself as an infinite vector/buffer but I thought that since you'd done so much work to make this use case workable, some feedback about problems with was warranted.