Skip to content

perf: wide set-0 leaves only below 2^15 virtual indexes - #75

Draft
alexvictoor wants to merge 4 commits into
perf/packed-wide-leavesfrom
perf/packed-wide-leaves-adaptive
Draft

alexvictoor wants to merge 4 commits into
perf/packed-wide-leavesfrom
perf/packed-wide-leaves-adaptive

Conversation

@alexvictoor

@alexvictoor alexvictoor commented Nov 15, 2026 •

Copy link
Copy Markdown
Member

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"]
Loading
  • leafLevelShiftFor(set, virtualLength) decides the leaf shift; the context keeps per-set shift, byte mask and leaf length (leafByteMaskBySet is read by the hot loops).
  • PackedArray.setVirtualLength uses hasLayoutFor instead of the top shift check alone, and unpacks if copyAndIncreaseSize returns null.
  • The set-0 tree walk from perf: packed histograms skip empty subtrees when scanning counts #69 (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.
  • The value copy must create set 0 leaves even for zero bytes: 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.

virtual length master wide-leaves adaptive
2^13 8192 (fills) 8192 8192
2^14 6957 16384 (never falls back) 16384
2^15 3484 3534 3534
40000 3356 3004 3356
2^16 3112 2536 3112
2^17 2692 2223 2692
  • Before: wide-leaves 2^16 2536, 2^17 2223 (master 3112 and 2692), 40000 3004 (master 3356)
    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):

workload master wide-leaves adaptive
record centre band 44.6 / 44.4 57.0 / 58.1 58.2 / 58.2
record sparse tails 37.9 / 37.7 48.7 / 52.3 52.2 / 52.3
sequential get, sparse (N=4096) 11,484 / 11,567 17,122 / 17,069 16,765 / 16,655
sequential get, centre (N=16384) 5,726 / 5,841 7,467 / 7,499 7,528 / 7,305

Above the threshold (2^16, same workloads, zz-above scratch bench, not committed): the adaptive branch matches master.

workload (2^16) master wide-leaves adaptive
record centre band 44.7 / 46.7 61.1 / 59.9 44.3 / 44.8
sequential get, centre 1,293 / 1,420 2,222 / 2,190 1,303 / 1,395

Tests: npm test 287 passed (23 files, 6 new in PackedArray.wideLeaves.spec.ts), npm run typecheck clean, npm run asbuild && npm run astest:ci 148150/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 PackedArray and PackedArrayContext internals 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

alexvictoor and others added 4 commits November 15, 2026 22:14
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>
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