Repository navigation
perf: wide set-0 leaves only below 2^15 virtual indexes - #75
Draft
alexvictoor wants to merge 4 commits into
Draft
alexvictoor wants to merge 4 commits into
alexvictoor wants to merge 4 commits into
Conversation
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>
The wide set 0 leaves of 4cbe522 cost 16 bytes per lone value instead of 8, so large sparse arrays reached the 16 bit packed addresses sooner and fell back to unpacked storage earlier (2^16: 2573 vs 3072 distinct values, 2^17: 2192 vs 2688). Below 2^15 they fell back later or never. Set 0 leaves are now 16 bytes while the virtual length is at most 2^15 (WIDE_SET_0_LEAVES_MAX_VIRTUAL_LENGTH), 8 bytes above. The leaf shift, byte mask and leaf length are per context, chosen from the virtual length. A resize that changes the set 0 layout copies set 0 value by value, creating a leaf wherever the source has one, since get() relies on a higher byte only existing where set 0 does. Sets 1..7 keep the tree copy. The leaf cache keeps set 0 across the two groups of a 16 index block only while set 0 leaves are wide. Encoded bytes do not change. Memory probe (distinct isolated values before unpacked fallback, mean of 5 seeds): 2^15 3534 (master 3484), 40000 3356 (3356), 2^16 3112 (3112), 2^17 2692 (2692): equal to master from 2^16 up, better below. Co-Authored-By: Claude Fable 5.1 <noreply@anthropic.com>
Co-Authored-By: Claude Fable 5.1 <noreply@anthropic.com>
The set-0 tree walk that master added for skipping empty subtrees took set 0's leaf shift and byte mask from the constant tables, which this branch replaces with per-context values that depend on the virtual length. The walk and its caller now read those from the context. Co-Authored-By: Claude Fable 5.1 <noreply@anthropic.com>
alexvictoor
force-pushed
the
perf/packed-wide-leaves
branch
from
November 15, 2026 21:17
4cbe522 to
06d6703
Compare
alexvictoor
force-pushed
the
perf/packed-wide-leaves-adaptive
branch
from
November 15, 2026 21:17
0cbb26a to
1d483a7
Compare
alexvictoor
force-pushed
the
perf/packed-wide-leaves
branch
from
November 22, 2026 21:26
06d6703 to
692bfe4
Compare
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
Stacked on
perf/packed-wide-leaves. The 16 byte set 0 leaves are kept only while the virtual length is at most 2^15, where they were measured to be at least as good as master for memory. Above 2^15 the set 0 layout is master's again. Encoded bytes do not change.flowchart TD C["new PackedArrayContext(virtualLength)"] --> Q{"virtualLength at most 2^15?"} Q -- yes --> W["set 0: 16-byte leaves, cache keeps set 0 across a 16-index block"] Q -- no --> M["set 0: 8-byte leaves, the master layout"] R["resize"] --> Q2{"set-0 layout changes?"} Q2 -- yes --> V["set 0 copied value by value, a leaf created wherever the source has one; sets 1..7 tree copy"] Q2 -- no --> T["tree copy for every set, as before"]leafLevelShiftFor(set, virtualLength)decides the leaf shift; the context keeps per-set shift, byte mask and leaf length (leafByteMaskBySetis read by the hot loops).PackedArray.setVirtualLengthuseshasLayoutForinstead of the top shift check alone, and unpacks ifcopyAndIncreaseSizereturns null.nextIndexWithLeaf/nextNonZeroIndex) reads the leaf shift and byte mask of set 0 from the context, so it follows the layout chosen for the virtual length.get()stops at the first set without a leaf, so a higher byte only exists where set 0 does. Found by the fast-check property on the first version.Evidence
Memory probe: distinct isolated values recorded before the context falls back to unpacked storage (seeded random indexes, one value each, mean of 5 seeds). Higher is better.
After: adaptive equals master from 2^16 up and at 40000, and keeps the wide-leaf gain below 2^15.
Speed:
src/bench/packed-leaves.bench.ts(hz, two runs each, same seeded inputs):Above the threshold (2^16, same workloads,
zz-abovescratch bench, not committed): the adaptive branch matches master.Tests:
npm test287 passed (23 files, 6 new inPackedArray.wideLeaves.spec.ts),npm run typecheckclean,npm run asbuild && npm run astest:ci148150/148150 (4 new AS tests). The new tests cover the threshold crossing on resize (counts, cached group, a value with a zero low byte), both widths, a near-full storage resize, and a fast-check property across resizes over the threshold (300 runs; also ran 3000 once).Merge Danger
Door: two-way
No persisted format changes: encoded bytes are identical, and reverting only changes the in-memory set 0 layout.
Blast Radius: contained
Only
PackedArrayandPackedArrayContextinternals in both languages change. The public histogram API is unchanged.Note for reviewers: the packed counts length is 23552 for 1..3.6e9 at 3 significant figures (wide leaves apply), 17408 for 1..6e7, and 45056 for 1..MAX_SAFE_INTEGER at 3 significant figures (master layout, so no wide-leaf gain there). The threshold is on the virtual length, so a histogram that grows past 2^15 loses the wide-leaf gain at the resize.
🤖 Generated with Claude Code