Skip to content

Make transpose linear instead of quadratic - #19534

Merged
fdncred merged 3 commits into
nushell:mainfrom
TheNetherWatcher:perf/transpose-linear
Nov 17, 2026
Merged

fdncred merged 3 commits into
nushell:mainfrom
TheNetherWatcher:perf/transpose-linear

Conversation

@TheNetherWatcher

Copy link
Copy Markdown
Contributor

Description

transpose got slow quadratically on wide records and long tables. For every (column, row) pair it looked the key up by scanning the row's record, and it also searched the output record for each header before inserting. A 40k-key record took about a second here (2.6s reported in #6979), and transposing a 10k-row table took ~700ms.

This collects the cells into a column-major grid in one pass over the input (a hash map from column name to index, values moved rather than cloned), and works out once which output field each header goes to, so repeated headers with -r/-l/-a are merged by index. Behaviour is unchanged: column order, nulls for missing columns, duplicate-header handling and --as-record all produce the same output. I checked this against the old binary on a set of edge cases and on large mixed inputs.

Release build, median of several runs:

input before after
record, 1k keys, transpose key value 0.93 ms 0.16 ms
record, 10k keys 78 ms 1.65 ms
record, 40k keys 1047 ms 7.0 ms
table 1000x10, transpose 7.5 ms 0.6 ms
table 10000x10, transpose 690 ms 6.1 ms
40k rows, transpose -r -d 1052 ms 7.0 ms

Added tests for missing columns and for interleaved repeated keys with -r, -r -l and -r -a.

User-facing changes (Release notes)

Improved performance of transpose on wide records and long tables. It now runs in linear time, so transposing a record with 40k keys takes milliseconds instead of seconds.

Additional notes

Fixes #6979

transpose looked up every (column, row) pair by key and searched the
output record for each header, so wide records and long tables took
quadratic time. Gather the cells into a grid in one pass and precompute
where each header lands in the output record instead.

@fdncred fdncred left a comment •

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Review overview

🟡 Changes recommended

The linear rewrite holds up. 4,400 random inputs across every flag combination give the same output as main, and the #6979 shape (a 40k-key record of records into transpose key value) takes 79 ms on a debug build of this branch against 1.4 s on main's release build. Two lows left: peak memory on sparse input, and a test for repeated column names.

Findings: 0 High severity · 0 Medium severity · 2 Low severity

Open (2)

Notes from my clanker, checked on 8e345bc. Built the branch (debug) and ran each case on it and on main (7bff3d1): 38 hand-written edge cases (empty input, duplicate and case-only keys, -r/-l/-a/-i/-d, error paths) and 4,400 random inputs, all identical. Peak RSS is from debug builds of both. I didn't run clippy or the tests locally, since CI's lint jobs and the Linux and macOS test jobs passed on this head.

Also add a test for repeated custom column names.
@fdncred fdncred added notes:ready Indicates Ready for Release notes notes:perf Performance related PRs labels Nov 17, 2026
@fdncred
fdncred merged commit ebeb7e8 into nushell:main Nov 17, 2026
14 checks passed
@fdncred

fdncred commented Nov 17, 2026

Copy link
Copy Markdown
Contributor

Thanks

@github-actions github-actions Bot added this to the v0.118.0 milestone Nov 17, 2026
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

notes:perf Performance related PRs notes:ready Indicates Ready for Release notes

Projects

None yet

Development

Successfully merging this pull request may close these issues.

transpose is quadratic in the number of record columns

2 participants