Changes for version 0.3503 - 2026-11-30
- filter(): speed
- filter() on an AoH or HoH frame spent half its time in perl's hv_fetch_ent(), about 250 instructions a lookup to find each row's cells (callgrind, 100,000 rows x 5 columns, perl 5.44.0). A row of a hash that shares its keys, which is every hash perl makes unless told otherwise, holds the very key the column name points at, so the cell is now found by walking one bucket for that pointer. The same walk finds a row's columns when an AoH becomes a HoA (output_type => 'hoa'). A pointer that matches cannot belong to another key, so the walk is never wrong; a tied or magical row, a hash that does not share its keys, one rehashed before perl 5.18, and a key not in the row go on to hv_fetch_ent() as before, and a key deleted from a restricted hash is still absent. On 300,000 rows x 5 columns the median fell from 17 to 13 ms for an AoH, 110 to 89 ms for an AoH -> HoA and 131 to 115 ms for a HoH, and an AoH -> HoA of 50,000 rows x 40 columns from 84 to 59 ms (one process per variant, built side by side from the same tree).
- A string cell that filter() or merge() copies into its result now shares the input's buffer copy-on-write (perl 5.20 and later; earlier perls share only hash-key strings and copy the rest as before), where it used to get a malloc'd buffer of its own, at least 32 bytes for a one-letter string. The copy is still its own scalar: the first write to either side unshares it. On 300,000 rows of a CSV read by read_table() (id, x, y and two one-letter categories, all strings), perl 5.44.0, median of 9 calls, filter(col('x') > 0) fell from 45.0 to 40.2 ms and the memory its result holds from 60.3 to 50.7 MB; merge() of the frame with itself on id from 143.8 to 129.4 ms and 209.9 to 161.9 MB; and vals($df, 'cat1') from 11.4 to 7.0 ms and 24.1 to 14.5 MB. A frame whose strings are copies of literals, such as plot.scaling.pl's synthetic one, is unchanged. Up to 0.3502 the copy was deliberately not copy-on-write, after an older measurement had filter() slower with it (23.0 -> 28.3 ms).
- transpose(): speed
- transpose() of an array of arrays is 3 to 6 times faster when it is called repeatedly, and freeing its result 2 to 3 times faster; a single call after other work is about 5% slower. In plot.scaling.pl it was 50 to 200 times slower than R's t() and NumPy's .T.copy(), and grew faster than linearly. Walking the input was 5 of its 60 ms on 300,000 x 3; the rest was waiting on memory. Each scalar perl hands out comes off a free list, and once that list is out of address order every one is a cache miss that must finish before the next can start. transpose() copied each row's cells as it read them, so each output column's scalars were interleaved with the other columns', and freeing the result column by column scrambled the list for the next call: 14 ms in a fresh process, 55 to 70 ms once a few results had been freed. It now reads a block of rows (65,536 cells), keeping each plain number's value and a reference to any other cell, and copies the block one column at a time, so a column's scalars are allocated, and later freed, together, and the list stays in order. The result is the same as before: before anything that can run perl code (a tied array, get magic), the cells already read are copied, so a FETCH cannot change or free one waiting to be copied. On perl 5.44.0, medians of 8 to 10 fresh processes against 0.3502, a repeated call fell from 58 to 9.9 ms for 300,000 x 3 numbers, 155 to 39 ms with two one-letter string columns added, 30 to 11 ms for 950 x 950 and 6.1 to 2.1 ms for 3,000 x 100. The change does not help a list that something else scrambled: after building and freeing a 1,000,000-key hash, one call on 300,000 x 3 went from 73.1 to 77.8 ms (108.7 to 116.3 ms with the string columns, 7.4 to 8.1 ms at 30,000 rows), the cost of the second pass, and its free from 11.1 to 10.9 ms (28.0 to 22.8 ms). In a fresh process a single call is unchanged within the noise, except 3,000 x 100 at 4.0 -> 4.6 ms. R and NumPy copy one block of doubles and remain faster.
- filter() and transpose(): every data frame shape
- filter() takes an array of arrays, which it refused as "AoH element 0 is not a HASH reference". Its columns are positions, as agg(), csort() and colnames() name them: col(0) or col('0') is the first column, and every row is data. A col() predicate is compiled and run in C as on the other shapes; a code reference gets the row as an array reference and its 0-based index. The kept rows are shared with the input, as an AoH's are. With output_type => 'aoh' or 'hoa' the columns are keyed '0', '1', ..., as csort() keys them. A column named by anything but a position (col('x'), col('01'), col(-1)), or a position past the widest row, dies as a misspelt column does on the other shapes.
- transpose() takes an array of hashes and a hash of arrays, which it refused. Both have named columns and positional rows, so their transpose has named rows and positional columns, which only a hash of hashes holds: the result is out{col}{i}, with the row positions '0', '1', ... as the inner keys, as pandas' DataFrame.T makes a frame's row numbers its columns. Nothing is padded, as on a hash of hashes: a key an AoH row lacks, or a position past a short HoA column, is absent. A hash's first value decides whether it is a HoA or a HoH.
- Tests
- t/filter.compiled.t runs the compiled AoH, HoH and AoH -> HoA paths over the rows the pointer walk cannot answer and must hand to perl: a key that is absent, a key deleted from a restricted hash, a tied row, and keys perl stores as UTF-8 or downgraded from it, against perl's own lookups through grep. It also checks that a copy-on-write string cell and the input it came from each see only their own writes.
- t/transpose.t crosses transpose()'s block boundaries with every kind of cell (NV, IV, a UV above IV_MAX, strings, undef, references, holes), takes a frame wider than one block, checks that a tied cell's FETCH cannot change a cell read before it, and that one which empties the frame dies cleanly, with leak checks on those paths. Copying before each such FETCH, and holding a reference to each string read, are each caught by these tests when removed.
- t/filter.R.pandas.t runs each of its R subset() and pandas cases on an array of arrays as well as on the other three shapes, and t/filter.t covers the AoA's call forms, output shapes, holes, tied rows, every column-name error and leaks.
- t/frame_reshape.R.pandas.t takes pandas' test_transpose_mixed, test_transpose_float and test_transpose (GH#57315), with the frames' default row numbers, through transpose() as a HoA and as an AoH against pandas' .T and .T.T (generated by t/frame_reshape.R.pandas.py), and t/transpose.t covers both shapes' errors, tied and UTF-8 input, a FETCH that deletes the column being read, and leaks.
Modules
Get basic statistical functions, like in R, but with Perl using XS for performance