this post was submitted on 30 Mar 2026
10 points (100.0% liked)

Programming

26308 readers
630 users here now

Welcome to the main community in programming.dev! Feel free to post anything relating to programming here!

Cross posting is strongly encouraged in the instance. If you feel your post or another person's post makes sense in another community cross post into it.

Hope you enjoy the instance!

Rules

Rules

  • Follow the programming.dev instance rules
  • Keep content related to programming in some way
  • If you're posting long videos try to add in some form of tldr for those who don't want to watch videos

Wormhole

Follow the wormhole through a path of communities !webdev@programming.dev



founded 2 years ago
MODERATORS
 

I need to scan very large JSONL files efficiently and am considering a parallel grep-style approach over line-delimited text.

Would love to hear how you would design it.

top 15 comments
sorted by: hot top controversial new old
[–] Jayjader@jlai.lu 1 points 12 hours ago* (last edited 12 hours ago)
  1. chunk_size := file_size / cpu_cores. Compile regex.

  2. spawn cpu_cores workers:
    2.a. worker #n starts at n * chunk_size bytes. If n > 0, skip bytes until newline encountered.
    2.b worker starts feeding bytes from file/chunk into regex. When match is found, write to output (stdout or file, whichever has better performance). When newline encountered, restart regex state automata.
    2.c after having read chunk_size bytes, continue until encountering a newline to ensure the whole file is covered by the parallel search.

Optionally, keep track of byte number and attach them to the found matches when outputting, to facilitate eventually de-duplicating and/or navigating to said match in the file.

To avoid problems, have each worker output to a separate file, and only combine these output files when the workers are all finished.

As others have said, it's going to be hard to get more speedup than this, and you will ultimately be limited by your storage's read speed and throughput if the whole file cannot fit into memory.

Read the JSONL into a real database like Postgres.

[–] mvirts@lemmy.world 1 points 17 hours ago

If you're writing a program, definitely multiple threads or processes that each scan a chunk of the file, which basically means seek to the start of the chunk, read lines into the scan code until you hit the end of the chunk. For jsonl each chunk will need an alignment step to not break the jsonl.

For command line trickery, maybe the file could be chunked up by running multiple dd instances with an offset parameter piped into grep. This has many synchronization issues and all the outputs should be captured separately then combined afterwards. I can't think of a good way to align this method to line edges but maybe you can put some fancy regular expression magic into the grep step to ignore malformed json at the beginning and end and overlap the chunks?

Grep is fast already, maybe test the simple approach and see how long it takes.

[–] eager_eagle@lemmy.world 4 points 1 day ago (1 children)
  1. How many grep-like ops per file?
  2. Is it interactive or run by another process?
  3. Do you know which files ahead of time?
  4. Do you have any control over that file creation?
  5. Is the JSONL append only? Is the grep running while the file is modified?
  6. How large is very large? 100s of MB? Few GB? 100s of GB? Whether or not it fits in memory could change the approach.
  7. You're using files, plural, would parallelizing at the file level (e.g. one thread per file) be enough?
  8. How many files and how often is that executed?
[–] dhruv3006@lemmy.world 3 points 1 day ago

100s of GBs yes.

[–] Bazell@lemmy.zip 4 points 1 day ago* (last edited 1 day ago) (1 children)

Splitting file in equal parts and analyzing in threads is basically the only efficient option to utilize modern CPU architectures efficiently. Since I doubt that the data stored in your files can be quickly processed by the GPU(I assume that you have text data).

[–] bleistift2@sopuli.xyz 3 points 1 day ago (2 children)

Can a file really be split efficiently? And is reading from multiple files on the same disk really faster than scanning a single file from top to bottom?

[–] entwine@programming.dev 5 points 1 day ago

You don't actually need to "split" anything, you just read from different offsets per thread. Mmap might be the most efficient way to do this (or at least the easiest)

Whether or not that's going to run into hardware bottlenecks is a separate issue from designing a parallel algorithm. Idk what OP is trying to accomplish, but if their hardware is known (eg this is an internal tool meant to run in a data center), they'll need to read up on their hardware and virtualization architecture to squeeze the most IO performance.

But if parsing is actually the bottleneck, there's a lot you can do to optimize it in software. Simdjson would be a good place to start.

[–] Ephera@lemmy.ml 2 points 1 day ago (1 children)

I think, you could open the same file multiple times and then just skip ahead by some number of bytes before you start reading.

But yeah, no idea if this would actually be efficient. The bottleneck is likely still the hard drive and trying to fit multiple sections of the file into RAM might end up being worse than reading linearly...

[–] Bazell@lemmy.zip 2 points 1 day ago* (last edited 1 day ago)

This approach will indeed hit bottleneck even in SSD. Thus, file can be read into RAM line by line in thread 0, then once specified amount of lines was gathered, schedule thread 1 to process them, while thread 0 still reads new lines. Once another chunk is ready, give to thread 2 and so on. This way you can start processing data in asynchronous regime in the fastest way possible. Slightly slower but more convenient approach is to firstly read all the file into RAM and only then assign parts of it to each thread at the same time.

[–] Lysergid@lemmy.ml 3 points 1 day ago

How large is very large? Would it be something that jq can’t do? Is it purely string search or JSON-tree search?

Generally you would want to get file size, split it into ranges which can be read as valid UTF-8. Feed each range into reader thread. Can be inefficient for HDDs because each thread will try to access random location on disk forcing needle to jump back and forth. Also you’ll need reread ranges at split point with some positive and negative offset in case desired content got split. Things are getting much more complicated if you want JSON-tree grep. Branches may get split from parent nodes across multiple ranges.

[–] vfscanf@discuss.tchncs.de 3 points 1 day ago (1 children)

The question is, what will be your limiting factor: CPU or disk I/O? Parallel processing doesn't do much good if the workers have to wait on the disk to deliver more data. I'd start with an async architecture, where the program can do its processing while it is waiting on more data.

[–] pelya@lemmy.world 3 points 1 day ago

One additinal trick is to compress your files before writing them to disk, using some kind of fast lightweight compression like parallel gzip (pigz command) or lzop. When parsing them, you will have smaller disk reads but higher CPU usage, which will give speed advantage if you have server-class CPU with lots of cache.

[–] bizdelnick@lemmy.ml 0 points 1 day ago (1 children)

Bad idea. First, file is read sequentially, and you can't parallelize this. Second, grep is a bad solution for structured files. Better use jq or something similar.

[–] thenextguy@lemmy.world 1 points 1 day ago

https://jsonltools.com/what-is-jsonl

First time hearing about it, but JSONL is like CSV with json per line. So not really structured.

I don’t see why you couldn’t just use grep on it.