From 5d1cc0ba207db51fae2b061b327c86538e2d9546 Mon Sep 17 00:00:00 2001 From: alexvictoor Date: Sun, 15 Nov 2026 09:17:55 +0100 Subject: [PATCH 1/2] perf: count bits in one pass for values below 2^32 in the JS packed array 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 --- src/packedarray/PackedArrayContext.fc.spec.ts | 60 +++++++++++++++++++ src/packedarray/PackedArrayContext.ts | 9 ++- 2 files changed, 67 insertions(+), 2 deletions(-) create mode 100644 src/packedarray/PackedArrayContext.fc.spec.ts diff --git a/src/packedarray/PackedArrayContext.fc.spec.ts b/src/packedarray/PackedArrayContext.fc.spec.ts new file mode 100644 index 0000000..82847cc --- /dev/null +++ b/src/packedarray/PackedArrayContext.fc.spec.ts @@ -0,0 +1,60 @@ +/* + * This is a TypeScript port of the original Java version, which was written by + * Gil Tene as described in + * https://github.com/HdrHistogram/HdrHistogram + * and released to the public domain, as explained at + * http://creativecommons.org/publicdomain/zero/1.0/ + */ +import * as fc from "fast-check"; +import { bitCount } from "./PackedArrayContext"; + +const runFromStryker = __dirname.includes("stryker"); + +const runnerOptions = { + numRuns: runFromStryker ? 10 : 1000, + verbose: true +}; + +// Reference population count, one bit at a time on the low 32 bits of each word +const referenceBitCount = (n: number) => { + let bits = 0; + let rest = n; + while (rest > 0) { + const word = rest % 0x100000000; + for (let bit = 0; bit < 32; bit++) { + bits += Math.floor(word / 2 ** bit) % 2; + } + rest = Math.floor(rest / 0x100000000); + } + return bits; +}; + +describe("Packed array context", () => { + it("should count the set bits of a value below 2^53", () => { + fc.assert( + fc.property( + fc.integer({ min: 0, max: Number.MAX_SAFE_INTEGER }), + n => bitCount(n) === referenceBitCount(n) + ), + runnerOptions + ); + }); + + it("should count the set bits of the 16 bit slot indicators", () => { + fc.assert( + fc.property( + fc.integer({ min: 0, max: 0xffff }), + n => bitCount(n) === referenceBitCount(n) + ), + runnerOptions + ); + }); + + it("should count the bits of values above 2^32 in every word", () => { + expect(bitCount(0)).toBe(0); + expect(bitCount(0xffffffff)).toBe(32); + expect(bitCount(0x100000000)).toBe(1); + expect(bitCount(0x1ffffffff)).toBe(33); + expect(bitCount(Number.MAX_SAFE_INTEGER)).toBe(53); + }); +}); diff --git a/src/packedarray/PackedArrayContext.ts b/src/packedarray/PackedArrayContext.ts index 7c3535f..b85a255 100644 --- a/src/packedarray/PackedArrayContext.ts +++ b/src/packedarray/PackedArrayContext.ts @@ -38,11 +38,16 @@ const PACKED_ARRAY_GROWTH_FRACTION_POW2 = 4; const { ceil, log2, max } = Math; -const bitCount = (n: number) => { +// Population count of a non-negative integer below 2^53. The high words are +// shifted down by whole 32 bit words, so a value below 2^32 takes one pass. +export const bitCount = (n: number) => { + if (n <= 0xffffffff) { + return bitCount32(n | 0); + } var bits = 0; while (n !== 0) { bits += bitCount32(n | 0); - n /= 0x100000000; + n = Math.floor(n / 0x100000000); } return bits; }; From 7fc265858a07e31c0c01215c5a37245503ace3c8 Mon Sep 17 00:00:00 2001 From: alexvictoor Date: Sun, 15 Nov 2026 20:28:06 +0100 Subject: [PATCH 2/2] fix: review follow-ups for the packed bitCount - 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 --- src/packedarray/PackedArrayContext.fc.spec.ts | 24 +++++++++++++++---- src/packedarray/PackedArrayContext.ts | 15 ++++++++---- 2 files changed, 30 insertions(+), 9 deletions(-) diff --git a/src/packedarray/PackedArrayContext.fc.spec.ts b/src/packedarray/PackedArrayContext.fc.spec.ts index 82847cc..9c6a35d 100644 --- a/src/packedarray/PackedArrayContext.fc.spec.ts +++ b/src/packedarray/PackedArrayContext.fc.spec.ts @@ -12,7 +12,7 @@ const runFromStryker = __dirname.includes("stryker"); const runnerOptions = { numRuns: runFromStryker ? 10 : 1000, - verbose: true + verbose: true, }; // Reference population count, one bit at a time on the low 32 bits of each word @@ -34,7 +34,7 @@ describe("Packed array context", () => { fc.assert( fc.property( fc.integer({ min: 0, max: Number.MAX_SAFE_INTEGER }), - n => bitCount(n) === referenceBitCount(n) + (n) => bitCount(n) === referenceBitCount(n) ), runnerOptions ); @@ -44,17 +44,33 @@ describe("Packed array context", () => { fc.assert( fc.property( fc.integer({ min: 0, max: 0xffff }), - n => bitCount(n) === referenceBitCount(n) + (n) => bitCount(n) === referenceBitCount(n) ), runnerOptions ); }); - it("should count the bits of values above 2^32 in every word", () => { + it("should count the set bits of a single word, including bit 31", () => { + fc.assert( + fc.property( + fc.integer({ min: 0, max: 0xffffffff }), + (n) => bitCount(n) === referenceBitCount(n) + ), + runnerOptions + ); + }); + + it("should count the bits at the 2^32 boundaries and above", () => { expect(bitCount(0)).toBe(0); expect(bitCount(0xffffffff)).toBe(32); expect(bitCount(0x100000000)).toBe(1); expect(bitCount(0x1ffffffff)).toBe(33); expect(bitCount(Number.MAX_SAFE_INTEGER)).toBe(53); }); + + it("should reject inputs that are not non-negative safe integers", () => { + [NaN, Infinity, -Infinity, -1, 1.5, 2 ** 53].forEach((n) => { + expect(() => bitCount(n)).toThrow(/non-negative safe integer/); + }); + }); }); diff --git a/src/packedarray/PackedArrayContext.ts b/src/packedarray/PackedArrayContext.ts index b85a255..b3abadf 100644 --- a/src/packedarray/PackedArrayContext.ts +++ b/src/packedarray/PackedArrayContext.ts @@ -38,13 +38,18 @@ const PACKED_ARRAY_GROWTH_FRACTION_POW2 = 4; const { ceil, log2, max } = Math; -// Population count of a non-negative integer below 2^53. The high words are -// shifted down by whole 32 bit words, so a value below 2^32 takes one pass. +/** + * Population count of a non-negative integer below 2^53. The high words are + * shifted down by whole 32 bit words, so a value below 2^32 takes one pass. + * Any other input (negative, non-integer, NaN, Infinity) would never reach 0 + * and is rejected instead. + * @internal + */ export const bitCount = (n: number) => { - if (n <= 0xffffffff) { - return bitCount32(n | 0); + if (!Number.isSafeInteger(n) || n < 0) { + throw new Error(`bitCount expects a non-negative safe integer, got ${n}`); } - var bits = 0; + let bits = 0; while (n !== 0) { bits += bitCount32(n | 0); n = Math.floor(n / 0x100000000);