Skip to content

transpose is quadratic in the number of record columns #6979

Closed

Description

@rapenne-s

Describe the bug

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:

  1. You need nix installed:
  2. nix search --json nixpkgs | from json | transpose key value

By using a pre-generated file:

  1. Get the file file.json.gz
  2. run gunzip on it
  3. open file.json | from json | transpose key value

Expected behavior

it gives a result instead of freezing

Screenshots

No response

Configuration

key value
version 0.70.0
branch
commit_hash
build_os linux-x86_64
build_target x86_64-unknown-linux-gnu
rust_version rustc 1.64.0
cargo_version cargo 1.64.0
pkg_version 0.70.0
build_time 1980-01-01 00:00:00 +00:00
build_rust_channel release
features database, dataframe, default, trash, which, zip
installed_plugins

Additional context

No response

Activity

  1. fdncred commented on Nov 2, 2022

    @fdncred
    Contributor

    Is it possible to upload this json file to this issue?

  2. added
    performance
    on Nov 2, 2022
  3. rapenne-s commented on Nov 5, 2022

    @rapenne-s
    Author

    I included a file to the issue

  4. fdncred commented on Nov 5, 2022

    @fdncred
    Contributor

    I included a file to the issue

    thanks, this will make testing easier.

  5. fdncred commented on Nov 5, 2022

    @fdncred
    Contributor

    ok, here's what I've found.

    1. I reproduced your issue.
    2. 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.
    3. I found this out by working on a smaller segment of the big file. small-file.json.zip
    4. 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?
    5. 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             │
    ╰───┴─────────────┴─────────┴────────────────────────────────────╯
    
    1. 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.)
    open ~/Downloads/small-file.json | from json -o | transpose key value 
    ╭────┬───────────────────────────────────────────────────┬───────────────────╮
    │  # │                        key                        │       value       │
    ├────┼───────────────────────────────────────────────────┼───────────────────┤
    │  0 │ legacyPackages.x86_64-linux.AMB-plugins           │ {record 3 fields} │
    │  1 │ legacyPackages.x86_64-linux.ArchiSteamFarm        │ {record 3 fields} │
    │  2 │ legacyPackages.x86_64-linux.AusweisApp2           │ {record 3 fields} │
    │  3 │ legacyPackages.x86_64-linux.CHOWTapeModel         │ {record 3 fields} │
    │  4 │ legacyPackages.x86_64-linux.ChowCentaur           │ {record 3 fields} │
    │  5 │ legacyPackages.x86_64-linux.ChowKick              │ {record 3 fields} │
    │  6 │ legacyPackages.x86_64-linux.ChowPhaser            │ {record 3 fields} │
    │  7 │ legacyPackages.x86_64-linux.CoinMP                │ {record 3 fields} │
    │  8 │ legacyPackages.x86_64-linux.CuboCore.coreaction   │ {record 3 fields} │
    │  9 │ legacyPackages.x86_64-linux.CuboCore.corearchiver │ {record 3 fields} │
    │ 10 │ legacyPackages.x86_64-linux.CuboCore.corefm       │ {record 3 fields} │
    │ 11 │ legacyPackages.x86_64-linux.CuboCore.coregarage   │ {record 3 fields} │
    │ 12 │ legacyPackages.x86_64-linux.CuboCore.corehunt     │ {record 3 fields} │
    │ 13 │ legacyPackages.x86_64-linux.CuboCore.coreimage    │ {record 3 fields} │
    │ 14 │ legacyPackages.x86_64-linux.CuboCore.coreinfo     │ {record 3 fields} │
    │ 15 │ legacyPackages.x86_64-linux.CuboCore.corekeyboard │ {record 3 fields} │
    │ 16 │ legacyPackages.x86_64-linux.CuboCore.corepad      │ {record 3 fields} │
    │ 17 │ legacyPackages.x86_64-linux.CuboCore.corepaint    │ {record 3 fields} │
    │ 18 │ legacyPackages.x86_64-linux.CuboCore.corepdf      │ {record 3 fields} │
    ├────┼───────────────────────────────────────────────────┼───────────────────┤
    │  # │                        key                        │       value       │
    ╰────┴───────────────────────────────────────────────────┴───────────────────╯
    

    I also noticed that there is some embedded html which may or may not be an issue, but I wanted to call it out.
    Screenshot 2022-11-05 at 7 59 42 AM

    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.

  6. rapenne-s commented on Nov 5, 2022

    @rapenne-s
    Author

    Thanks for the explanations, it's very valuable.

  7. Kixunil commented on Nov 5, 2022

    @Kixunil
    Contributor

    @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.

  8. fdncred commented on Nov 5, 2022

    @fdncred
    Contributor

    @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?

  9. Kixunil commented on Nov 5, 2022

    @Kixunil
    Contributor

    #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

  10. Veetaha commented on Dec 9, 2022

    @Veetaha

    I've noticed this problem with running the following script my repository.

    It hangs on the where inside part of the pipeline in main for 4 seconds.

    Note that removing the cached wrapper fixes this performance bug.

    nu ./main.nu
    
    # main.nu
    
    def-env cached [cache_id: string, imp: block] {
        let cache_id = $'__cache_($cache_id)'
    
        let-env $cache_id = if $cache_id in $env {
            $env | get $cache_id
        } else {
            do $imp
        }
    
        $env | get $cache_id
    }
    
    def-env cargo-metadata [] {
        cached cargo-metadata { cargo metadata --format-version 1 | from json }
    }
    
    def main [] {
        cargo-metadata | get packages | where name == snowpity-tg
    }
  11. rapenne-s commented on Jul 4, 2023

    @rapenne-s
    Author

    follow up, still reproducible using 0.82.0

  12. sophiajt commented on Jul 4, 2023

    @sophiajt
    Contributor

    @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.

  13. kronberger-droid commented on Nov 13, 2026

    @kronberger-droid
    Contributor

    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.

  14. 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
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Labels

    performance

    Type

    No type

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions