You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
{{ message }}
Repository navigation
transpose is quadratic in the number of record columns #6979
hi, I'm trying to parse a huge json generated from Nix with nu 0.70.0
The following command fails, nu process is stuck at 100% CPU and its memory usage doesn't change over time.
nix search --json nixpkgs | from json | transpose key value
Parsing the json itself is fine, nix search --json nixpkgs | from json doesn't freeze, but trying to apply something like | first 10 on it also freeze.
How to reproduce
By generating the data:
You need nix installed:
nix search --json nixpkgs | from json | transpose key value
When you try to do open file.json | from json | first 10, the problem is that there isn't 10 anything. There's just 1. You can see this by doing ... | first which lists the entire file. So, what it's trying to do is take all the json records and make columns, one column for each record, which is kind of a non-starter when you have a zillion records. I'm sure we could probably handle this better and may be a bug here.
I found this out by working on a smaller segment of the big file. small-file.json.zip
Also, the file is a bit weird because it has escapes in it. so doing open small-file.json | from json is actually like doing open -r file.json | from json | from json - maybe this is a bug where we should handle escaped quotes in json?
After looking at the small-file.json and unescaping it, we're left with something like this
{
"legacyPackages.x86_64-linux.AMB-plugins": {
"pname": "AMB-plugins",
"version": "0.8.1",
"description": "A set of ambisonics ladspa plugins"
},
"legacyPackages.x86_64-linux.ArchiSteamFarm": {
"pname": "archisteamfarm",
"version": "5.3.1.2",
"description": "Application with primary purpose of idling Steam cards from multiple accounts simultaneously"
},
"legacyPackages.x86_64-linux.AusweisApp2": {
"pname": "AusweisApp2",
"version": "1.24.3",
"description": "Authentication software for the German ID card"
}
}
Which makes me realize that this file wants to be a "one json per line" aka jsonl or json objects type of json but without the '\n'. So, I can do things like open ~/Downloads/small-file.json | from json -o | get "legacyPackages.x86_64-linux.AMB-plugins" and have it return a table
╭───┬─────────────┬─────────┬────────────────────────────────────╮
│ # │ pname │ version │ description │
├───┼─────────────┼─────────┼────────────────────────────────────┤
│ 0 │ AMB-plugins │ 0.8.1 │ A set of ambisonics ladspa plugins │
├───┼─────────────┼─────────┼────────────────────────────────────┤
│ # │ pname │ version │ description │
╰───┴─────────────┴─────────┴────────────────────────────────────╯
You probably already knew all of this, sorry if you did. :) I think this file is a good benchmark file that we could use to better handle large json files. Even trying to open the file in json object mode from json -o it still takes forever. I don't actually think it's hanging but it's just such a large file that maybe it just takes a long time. (but it could be hanging too, I'm not ruling that out.)
I also noticed that there is some embedded html which may or may not be an issue, but I wanted to call it out.
I also tried to open this with our dataframes features, that is build to handle really large files, and got this error.
open-df file.json 1 08:08:18 AM
Error:
× Json reader error
╭─[entry #12:1:1]
1 │ open-df file.json
· ───┬───
· ╰── ArrowError(NotYetImplemented("read an Array from a non-Array JSON"))
╰────
On the positive side, you can still do something like this open file.json | from json | get "legacyPackages.x86_64-linux.zzuf" and it works just fine without converting it to a table.
I'm not sure how to make this json processing more robust on big files. Any input is welcome.
@fdncred one thing I noticed when working on #7010 is that json parser currently converts all input to a single huge string and processes that even when -o is used. It also makes a few more allocations than what would be strictly required. I plan to send a PR processing external stream on-the-fly.
@rapenne-s - tracked it down to - I think - some inefficiency in transpose. My hunch is that other parts of Nushell can handle processing of the various parts a bit better and should be reused (basically, the goal would be to rewrite transpose using what we learned from more recent commands)
Here's a script that shows that it's likely transpose:
let pkg = open file.json | from json
let pkg_columns = $pkg | columns
let pkg_values = $pkg | values
$pkg_columns | zip $pkg_values | each { |items| {key: $items.0, value: $items.1 } }
That's a bit more manual way, but on my machine that only takes around 2 secs to complete, which much more tolerable for a transpose operation.
I could narrow this down: transpose is quadratic in the number of columns of a record.
On 0.117 with a record of 40k keys, $r | transpose key value takes about 2.6 s, while $r | items {|k, v| $k } takes 16 ms.
So items is the workaround for now, and i changed the title to the actual cause.
changed the title [-]nu freezes with 100% CPU usage when dealing with a huge structure[/-][+]`transpose` is quadratic in the number of record columns[/+]on Nov 13, 2026
Activity
fdncred commentedon Nov 2, 2022
Is it possible to upload this json file to this issue?
rapenne-s commentedon Nov 5, 2022
I included a file to the issue
fdncred commentedon Nov 5, 2022
thanks, this will make testing easier.
fdncred commentedon Nov 5, 2022
ok, here's what I've found.
open file.json | from json | first 10, the problem is that there isn't 10 anything. There's just 1. You can see this by doing... | firstwhich lists the entire file. So, what it's trying to do is take all the json records and make columns, one column for each record, which is kind of a non-starter when you have a zillion records. I'm sure we could probably handle this better and may be a bug here.open small-file.json | from jsonis actually like doingopen -r file.json | from json | from json- maybe this is a bug where we should handle escaped quotes in json?{ "legacyPackages.x86_64-linux.AMB-plugins": { "pname": "AMB-plugins", "version": "0.8.1", "description": "A set of ambisonics ladspa plugins" }, "legacyPackages.x86_64-linux.ArchiSteamFarm": { "pname": "archisteamfarm", "version": "5.3.1.2", "description": "Application with primary purpose of idling Steam cards from multiple accounts simultaneously" }, "legacyPackages.x86_64-linux.AusweisApp2": { "pname": "AusweisApp2", "version": "1.24.3", "description": "Authentication software for the German ID card" } }Which makes me realize that this file wants to be a "one json per line" aka
jsonlorjson objectstype of json but without the '\n'. So, I can do things likeopen ~/Downloads/small-file.json | from json -o | get "legacyPackages.x86_64-linux.AMB-plugins"and have it return a tablefrom json -oit still takes forever. I don't actually think it's hanging but it's just such a large file that maybe it just takes a long time. (but it could be hanging too, I'm not ruling that out.)I also noticed that there is some embedded html which may or may not be an issue, but I wanted to call it out.

I also tried to open this with our dataframes features, that is build to handle really large files, and got this error.
On the positive side, you can still do something like this
open file.json | from json | get "legacyPackages.x86_64-linux.zzuf"and it works just fine without converting it to a table.I'm not sure how to make this json processing more robust on big files. Any input is welcome.
rapenne-s commentedon Nov 5, 2022
Thanks for the explanations, it's very valuable.
Kixunil commentedon Nov 5, 2022
@fdncred one thing I noticed when working on #7010 is that json parser currently converts all input to a single huge string and processes that even when
-ois used. It also makes a few more allocations than what would be strictly required. I plan to send a PR processing external stream on-the-fly.fdncred commentedon Nov 5, 2022
@Kixunil streaming json for this type of thing would be great! I haven't tried out #7010 yet on this. Does it make any difference by itself?
Kixunil commentedon Nov 5, 2022
#7010 can't make a noticable performance difference because the only thing it does is enforcing the input type to be either a string or a raw stream.
I started working on streaming but unfortunately made a mistake and got stuck - see #7017
Veetaha commentedon Dec 9, 2022
I've noticed this problem with running the following script my repository.
It hangs on the
whereinside part of the pipeline inmainfor 4 seconds.Note that removing the
cachedwrapper fixes this performance bug.rapenne-s commentedon Jul 4, 2023
follow up, still reproducible using 0.82.0
sophiajt commentedon Jul 4, 2023
@rapenne-s - tracked it down to - I think - some inefficiency in
transpose. My hunch is that other parts of Nushell can handle processing of the various parts a bit better and should be reused (basically, the goal would be to rewritetransposeusing what we learned from more recent commands)Here's a script that shows that it's likely
transpose:That's a bit more manual way, but on my machine that only takes around 2 secs to complete, which much more tolerable for a transpose operation.
kronberger-droid commentedon Nov 13, 2026
I could narrow this down:
transposeis quadratic in the number of columns of a record.On 0.117 with a record of 40k keys,
$r | transpose key valuetakes about 2.6 s, while$r | items {|k, v| $k }takes 16 ms.So
itemsis the workaround for now, and i changed the title to the actual cause.[-]nu freezes with 100% CPU usage when dealing with a huge structure[/-][+]`transpose` is quadratic in the number of record columns[/+]Make `transpose` linear instead of quadratic (nushell#19534)