Skip to content

Nushell performance degrades when storing large tables in variables #9048

Closed

Description

@JosephTLyons

Describe the bug

Storing large tables into variables in nushell causes all other operations to slow down.

How to reproduce

  1. Run the python code to produce a very large fake data file
  2. Run let table = (open data.jsonl | lines | each {|row| $row | from json} | flatten)
  3. Run ls and observe slowdown

Expected behavior

I expect nushell to take time processing large files, but after a file has been processes and stored in memory, I wouldn't expect all other operations to slow down.

Screenshots

No response

Configuration

key value
version 0.78.0
branch
commit_hash
build_os macos-aarch64
build_target aarch64-apple-darwin
rust_version rustc 1.68.2 (9eb3afe9e 2023-03-27) (built from a source tarball)
cargo_version cargo 1.68.1 (6feb7c9cf 2023-03-26)
build_time 2023-04-04 18:47:00 +00:00
build_rust_channel release
features default, zip
installed_plugins dbg, periodic-table

Additional context

import json

fake_data = {
    "outer_thing": "text",
    "nested_thing":
        {
            "field a": 1234,
            "field b": "some text",
            "field c": "some text",
            "field d": "some text",
            "field e": 1234,
            "field f": "some text",
            "field g": "some text",
            "field h": "some text",
            "field i": "some text",
            "field j": False,
            "field k": 1234,
        }
}

with open("data.jsonl", "w") as jsonl_file:
    text = json.dumps(fake_data) + "\n"
    for _ in range(5_000_000):
        jsonl_file.write(text)

Activity

  1. added
    status:needs-triage
    on Apr 28, 2023
  2. JosephTLyons commented on Apr 28, 2023

    @JosephTLyons
    ContributorAuthor

    Btw, loving nushell for doing all sorts of manipulation of real-world data. Super cool to be putting nushell to work like this.

  3. added
    status:needs-core-team-attention
    on Apr 28, 2023
  4. fdncred commented on Apr 28, 2023

    @fdncred
    Contributor

    Thanks @JosephTLyons. This is a great sample case. I'm experiencing the slowdown now.

  5. JosephTLyons commented on Apr 28, 2023

    @JosephTLyons
    ContributorAuthor

    No problem, it probably can be reduced to just a flat file with no nesting, since running the commands to manipulate the data don't introduce the slowdown - its only storing the data the introduces it.

  6. added
    performance
    on Apr 28, 2023
  7. bobhy commented on Apr 29, 2023

    @bobhy
    Contributor

    Perfmon shows your system is not driven into swapping?

  8. amtoine commented on Apr 29, 2023

    @amtoine
    Member

    sheeeesh @JosephTLyons, the file is so big, even the let table = (...) crashes my computer 😆

    could the slowdonw be caused by the memory being full? 🤔

  9. fdncred commented on Apr 29, 2023

    @fdncred
    Contributor

    I have 32GB of memory on my mac, so running out of memory isn't the problem for me.

    It does take a lot of memory though.
    image

    Every command after loading that file takes about 10 seconds

  10. sophiajt commented on Apr 30, 2023

    @sophiajt
    Contributor

    I see a few things:

    • 5 million rows is enormous for our current setup. I'm sure if the dataframe stuff can handle jsonl currently (update: just tried it and seems like it doesn't support jsonl by default).
    • To work with data of the size, you should definitely be streaming it and pulling out the parts you need. If you try to hold this in memory, you'll likely grow your memory usage sky high. Rule of thumb: Each element of data has not only its data, but also the span it comes from. I'd estimate to full load this into a variable, would push Nushell into the gigabytes of memory usage?

    So let's stream this rather than actually consuming the whole thing. There are a few solutions, but here's the one that worked okay for me:

    open data.jsonl --raw | lines | each { from json } | group 10 | flatten | flatten

    The index on the left will repeat, but you should still be able to do things like | first 1000 and get a block of the first 1000 rows.

    But yeah, the short answer is that for something this large you should use dataframes. Nushell isn't memory-efficient enough to do large datasets using the standard data types.

  11. sophiajt commented on Apr 30, 2023

    @sophiajt
    Contributor

    Another way to think about the performance impact:

    Currently, in Nushell, we store a span (start and end location in the original source) with each value. So for this example, that's one for each cell, then also another for each row (which is a record). Assuming no other metadata that's nearly a 900 megabytes of just span information. Add to that the overhead of each value being a Value enum not just the data itself. It'll grow to be quite large.

    This is another reason we encourage folks to use dataframes for large amounts of data, as that will both a) not have the span info, and b) will use a more compact in-memory representation of the data it does have.

  12. removed
    status:needs-triage
    status:needs-core-team-attention
    on Jul 22, 2024
  13. kronberger-droid commented on Nov 14, 2026

    @kronberger-droid
    Contributor

    Holding a large table in a variable no longer slows down every later command since #11654.
    I checked with 500k rows of the sample data and the prompt stayed under 10 ms.

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