Skip to content

perf(sort,filter): Schwartzian transform and remove recursive match#63

Open
rkaraivanov wants to merge 2 commits intomasterfrom
rkaraivanov/performance-improvements
Open

perf(sort,filter): Schwartzian transform and remove recursive match#63
rkaraivanov wants to merge 2 commits intomasterfrom
rkaraivanov/performance-improvements

Conversation

@rkaraivanov
Copy link
Copy Markdown
Member

sort:

  • Pre-compute sort keys once per item (O(n)) instead of on every comparison (O(n log n)) via Schwartzian transform
  • Store decorated rows as flat pre-allocated tuples [item, key0, key1, ...] to halve allocations and improve cache locality vs {item, keys:[]}
  • Pre-compute direction multipliers array to eliminate map lookups from the comparator hot path

filter:

  • Replace recursive match + per-record ands.filter() with matchTree, eliminating O(n x ors) array allocations
  • Pre-compute ors/ands arrays per expression tree once per apply() call instead of re-deriving them per record

Profiling the demo grid with templates and 100k records:

  • sort ~2x faster than the previous implementation
  • filtering ~3-5% faster but with less memory allocations/GC pressure during the operation

sort:
- Pre-compute sort keys once per item (O(n)) instead of on every
  comparison (O(n log n)) via Schwartzian transform
- Store decorated rows as flat pre-allocated tuples [item, key0, key1, ...]
  to halve allocations and improve cache locality vs {item, keys:[]}
- Pre-compute direction multipliers array to eliminate map lookups
  from the comparator hot path

filter:
- Replace recursive match + per-record ands.filter() with matchTree,
  eliminating O(n x ors) array allocations
- Pre-compute ors/ands arrays per expression tree once per apply() call
  instead of re-deriving them per record
@rkaraivanov rkaraivanov requested a review from damyanpetev April 22, 2026 11:52
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