Repository navigation
Make transpose linear instead of quadratic - #19534
Conversation
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.
There was a problem hiding this comment.
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
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.
|
Thanks |



Description
transposegot 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/-aare merged by index. Behaviour is unchanged: column order, nulls for missing columns, duplicate-header handling and--as-recordall 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:
transpose key valuetransposetransposetranspose -r -dAdded tests for missing columns and for interleaved repeated keys with
-r,-r -land-r -a.User-facing changes (Release notes)
Improved performance of
transposeon 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