Skip to content

perf: popcount no longer loops on float division in the JS packed array - #76

Merged
alexvictoor merged 2 commits into
masterfrom
perf/packed-insert
Nov 22, 2026
Merged

alexvictoor merged 2 commits into
masterfrom
perf/packed-insert

Conversation

@alexvictoor

@alexvictoor alexvictoor commented Nov 15, 2026 •

Copy link
Copy Markdown
Member

Summary

bitCount in 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 getPackedIndex descent and expandEntry calls it, so random inserts paid about 34 float divisions per call. The AssemblyScript twin already uses the popcnt builtin, so it is unchanged. Profile of random inserts on master: bitCount32 48% and bitCount 34% of self time.

Evidence

  • Before: random recordValue of 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 bench data-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.
  • Tests: npm test 276 passed (22 files), including the new PackedArrayContext.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 typecheck clean. npm run asbuild && npm run astest:ci pass; AssemblyScript untouched.

Merge Danger

Door: two-way

Internal arithmetic only. No change to the encoding, the array layout or the public API. bitCount is exported as @internal for 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

alexvictoor and others added 2 commits November 15, 2026 09:17
…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
alexvictoor marked this pull request as ready for review November 22, 2026 14:44
@alexvictoor
alexvictoor merged commit a7e3e1c into master Nov 22, 2026
4 checks passed
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