Repository navigation
perf: 16 byte leaves for set 0 of the packed array - #74
Merged
Merged
Conversation
alexvictoor
force-pushed
the
perf/packed-wide-leaves
branch
from
November 15, 2026 21:17
4cbe522 to
06d6703
Compare
Set 0 (the dense low byte) gets 16 byte leaves, so its set tree is one level shallower for some virtual lengths. Sets 1..7 keep 8 byte leaves. Each set has its own leaf shift and top level shift; the leaf cache stores long indexes as before. Encoded bytes do not change. The leaf cache keeps set 0's entry across the two 8 index groups of a 16 index block, so sequential scans walk set 0 once per block. Measured with src/bench/packed-leaves.bench.ts, master vs branch, three alternating runs each (hz = iterations per second, 200k records each): - record centre band: ~49.7 -> ~64.3 (about +29%) - record sparse tails: ~42.9 -> ~58.3 (about +36%) - sequential get, sparse (N=4096): ~12.8k -> ~18.6k (about +45%) - sequential get, centre (N=16384): ~6.4k -> ~8.0k (about +26%) Memory cost: isolated values reach the unpacked fallback later at virtual lengths 2^13..2^15 and sooner at 2^16 and 2^17 (about 16-18% fewer distinct values before fallback). Co-Authored-By: Claude Fable 5.1 <noreply@anthropic.com>
alexvictoor
force-pushed
the
perf/packed-wide-leaves
branch
from
November 22, 2026 21:26
06d6703 to
692bfe4
Compare
alexvictoor
marked this pull request as ready for review
November 22, 2026 21:34
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
Summary
Set 0 of the packed array (the low byte of every count, the dense one) gets 16-byte leaves. Sets 1..7 keep 8-byte leaves. Encoded bytes do not change.
flowchart LR I["virtual index"] --> S0["set 0: 16-byte leaf, shift 4"] I --> Sk["sets 1..7: 8-byte leaf, shift 3"] S0 --> D0["one level fewer of 16-slot entries"] --> L0["byte = leaf[index & 15]"] Sk --> Dk["same depth as before"] --> Lk["byte = leaf[index & 7]"]Set 0 takes most accesses, and its descent is now one dependent memory read shorter. The leaf cache stores long indexes whatever the leaf width.
PackedArraykeeps a module-local copy of the mask table: reading it through the import binding madeget2.4x to 3.4x slower.Evidence
src/bench/packed-leaves.bench.tson master2b0e3ed.After: same bench on this branch. Three alternating runs each, iterations/s, 200k records per iteration.
32-byte set-0 leaves (tried in JS only) gave no further recording gain and a slower centre scan, so 16 is kept.
Wide leaves win up to 2^15 and lose above (2^16, 2^17, and 40000 in #75's probe): a lone value costs 16 bytes in set 0 instead of 8, so the 64 KB packed limit is reached sooner. #75, stacked on this PR, keeps 16-byte leaves only up to 2^15 and converts the layout on resize across that threshold.
After:
src/packedarray/PackedArray.wideLeaves.spec.ts(8 tests: resize across the set-0 depth change, sharing within a 16-index block, no leaf for zero writes, sparse iteration, unpacked fallback, alternating groups, random checks against a plain array),assembly/__tests__/PackedArrayWideLeaves.test.ts(AS mirror),src/PackedHistogram.encoding.spec.ts(packed vs Int32: identical encoded base64 and percentiles).Merge Danger
Door: two-way
Internal layout only; a revert restores 8-byte leaves everywhere. Encoding is unaffected, which the packed-vs-Int32 test checks.
Blast Radius: packed
Every packed-array access and resize, in JS and AS.
nextIndexWithLeaf) now takes set 0's own top level and leaf shifts, andnextNonZeroIndexreads the whole 16-index group a set-0 leaf vouches for. The iteration property tests from perf: packed histograms skip empty subtrees when scanning counts #69 run against the wide leaves.src/packedarray/PackedArrayContext.ts.🤖 Generated with Claude Code