by salted-fry · 3y ago · 0 comments
One warning to note is that gron burns RAM. I've killed 32GB servers working with 15MB JSON files. (I think gron -u is even worse, but my memory is a bit fuzzy here).
https://github.com/adamritter/fastgron as an alternative has been pretty good to me in terms of performance, I think both in speed and RAM usage.
It reads the file into memory once, then just goes through it only once, so it shouldn't need much more memory than the file size.
Also I put a lot of work into making fastgron -u fast, but you can grep the file directly as well.
I'm thinking of doing some marketing (for example a blog entry just to show what was the main learnings in I/O and memory management in order to achieve this speed).
That sounds seriously like there is something wrong with the tool
`jq`: 1m26s 21G resident
`mygron -e --no-sort`: 18m14s 19M resident
`gron --no-sort`: 1m51s OOM killed at 54G resident
`gron -u` is basically impossible to optimise unless you know the input is in "sorted" order (ie the order it comes out of `gron`, including the `json.a = {};` bits) in which case my code can handle that in almost no RAM also. But if it's not sorted or you're missing the `json.a = {};` lines, there's not a lot you can do since you have to hold the whole data structure in RAM.
Sure, but something is seriously wrong if a 15 MB JSON data structure uses more than 32 GB of RAM.
Which is 20 levels of unknown-sized and unknown-typed slices of slices of `any` in Go and that is not super-efficient, alas. It gets worse when you have maps of slices of maps etc. `fastgron` gets around this by being able to manage its own memory.
(`gron` can, however, reconstruct the output correctly if you shuffle the input. `fastgron` cannot. Which suggests to me it's maybe using the same 'output as we go' trick that my `gron` fork uses for its "input is sorted" mode which uses almost no RAM but cannot deal with disordered input.)
(`gron` could/should maybe indicate the maximum size of the slices and if they're a single type which would make things more efficient and I might add that to my fork.)
https://github.com/adamritter/fastgron/blob/main/src/print_g...
And gron -u in theory should use less memory than gron-ifying a JSON, as you just have to fill a data structure in memory as you go.
You don't know the size, shape, or type of any of the levels in the data structure until you get to a line specifying one part of it. If you did, yep, it would be trivial!