Repository navigation
perf: popcount no longer loops on float division in the JS packed array - #76
Merged
Merged
Conversation
…rray bitCount divided by 2^32 until the value reached zero. For any non-zero value that takes about 34 float divisions, because n / 2^32 never becomes an integer zero early, and it runs on every getPackedIndex descent and entry expansion. Random inserts into a packed histogram spent about 80% of their time there. Values below 2^32 now take a single bitCount32 pass, and the loop shifts whole 32 bit words down with Math.floor for larger values. The AssemblyScript twin already uses the popcnt builtin, so it is unchanged. Co-Authored-By: Claude Fable 5.1 <noreply@anthropic.com>
- Drop the early-return branch: the floored loop already takes one pass below 2^32, so there is one code path to test. - Reject inputs that are not non-negative safe integers (NaN and Infinity never reached 0 and hung the loop). Random packed inserts measure the same with and without the guard. - Mark the export @internal: it exists for the property test only. - Tests: a property over the single-word range (bit 31 set), an accurate name for the boundary test, and the rejected inputs. Co-Authored-By: Claude Fable 5.1 <noreply@anthropic.com>
alexvictoor
marked this pull request as ready for review
November 22, 2026 14:44
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
bitCountin the JS packed array context divided by 2^32 until the value hit zero:const bitCount = (n: number) => { + if (!Number.isSafeInteger(n) || n < 0) throw ... // NaN / Infinity used to hang the loop - var bits = 0; + let bits = 0; while (n !== 0) { bits += bitCount32(n | 0); - n /= 0x100000000; // never reaches an integer 0 early: ~34 iterations per call + n = Math.floor(n / 0x100000000); // one pass for the 16 bit masks } return bits; };Every
getPackedIndexdescent andexpandEntrycalls it, so random inserts paid about 34 float divisions per call. The AssemblyScript twin already uses thepopcntbuiltin, so it is unchanged. Profile of random inserts on master:bitCount3248% andbitCount34% of self time.Evidence
recordValueof 1e6 values in 0..1e9 (lowestDiscernibleValue 1, highestTrackableValue 2, 3 digits, autoResize), JS packed: 148 / 153 / 154 ms; u32 typed array: 8 ms. Dense counts (values 1..2000): packed 102 / 103 / 105 ms. Ramp 1..1e6: packed 13 to 16 ms.After: same random run, JS packed: 34 / 25 / 21 ms (second run 37 / 25 / 21 ms); u32 unchanged at about 6 to 8 ms. Dense: packed 23 / 22 / 19 ms. Ramp: packed 12 to 15 ms (unchanged, within noise).
npm run benchdata-access (histogram-data-access.bench.ts): JS PackedHistogram 21.4M and 21.7M ops/s on master, 22.9M and 22.9M ops/s after. WASM rows unchanged. No regression.npm test276 passed (22 files), including the newPackedArrayContext.fc.spec.ts(fast-check against a bit-by-bit reference over 2^53, the single-word range with bit 31 set, and the 16 bit masks; the 2^32 boundaries; rejected inputs such as NaN, Infinity and negatives).npm run typecheckclean.npm run asbuild && npm run astest:cipass; AssemblyScript untouched.Merge Danger
Door: two-way
Internal arithmetic only. No change to the encoding, the array layout or the public API.
bitCountis exported as@internalfor the property test. A non-finite or negative input now throws instead of hanging the thread. The tests check the value of the result, not the speed.Blast Radius: low
Affects only JS packed histograms and only the speed of inserts that descend the set trees. WASM packed histograms are unaffected.
🤖 Generated with Claude Code