Skip to content

perf(sort): precompute sort keys and fast-path numeric sorter#4906

Draft
lukecotter wants to merge 1 commit into
tabulator-tables:masterfrom
lukecotter:perf/sort
Draft

perf(sort): precompute sort keys and fast-path numeric sorter#4906
lukecotter wants to merge 1 commit into
tabulator-tables:masterfrom
lukecotter:perf/sort

Conversation

@lukecotter

Copy link
Copy Markdown
Contributor

Summary

Speed up multi-row sorting without changing ordering semantics.

Changes

  • _sortItems: decorate each row with its sort keys and component once, sort the decorated array, then write the rows back (decorate-sort-undecorate). Stock re-derived getData() / getFieldValue() / getComponent() inside every comparison (O(n log n) calls); this does it O(n) up front. Implemented with a positional array (a Map keyed by row was measurably slower).
  • number sorter: return a - b directly when both values are finite numbers, skipping String() / separator splitting / parseFloat.

Benchmarks (Node, 250k rows)

  • sort, 1 numeric column: +62% (2.6×)
  • sort, 3 columns: +19%
  • number sorter, numeric values: +80% (4.9×)
  • number sorter, separator strings: +58% (2.4×)

Tests

Adds _sortItems correctness tests (multi-column asc/desc, stability, and equality vs a reference implementation over 50 randomized trials) plus number-sorter cases. Full suite green; ordering identical to before.

_sortItems decorates each row with its sort keys and component once, sorts the decorated array, then writes rows back, instead of re-deriving values inside every comparison. The number sorter returns a-b directly for finite numbers, skipping String()/split. Adds Sort._sortItems and number-sorter unit tests.
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant