Skip to content

feat: experimental bitBucketSize 'auto' (counts widen 8 -> 16 -> 32 -> 64 on demand) - #78

Draft
alexvictoor wants to merge 1 commit into
fix/count-overflowfrom
perf/counter-promotion
Draft

alexvictoor wants to merge 1 commit into
fix/count-overflowfrom
perf/counter-promotion

Conversation

@alexvictoor

Copy link
Copy Markdown
Member

Summary

Stacked on #77 (fixed-width histograms throw on count overflow). Retarget to master once #77 merges.

New opt-in bitBucketSize: "auto" for JS histograms: counts start at 8 bits and widen when a count does not fit, instead of throwing.

build({ bitBucketSize: "auto" })  ->  AutoCountsHistogram (extends TypedArrayHistogram)

incrementCountAtIndex / addToCountAtIndex / setCountAtIndex
  newCount = counts[i] + delta
  if newCount > maxCount of the current width
    promoteFor(newCount)          # once per width change
      pick the narrowest width that fits: Uint8 -> Uint16 -> Uint32 -> Float64
      copy counts into the wider array
  if newCount < 0 -> throw (every width)
  counts[i] = newCount
 TypedArrayHistogram.handleRecordException(count, value)   # out-of-range value, before auto-resize
-  throw if count does not fit the width
+  checkRecordedCount(count)       # protected hook
   super.handleRecordException(...)

 AutoCountsHistogram.checkRecordedCount(count)
+  throw only if count < 0 or count > MAX_SAFE_INTEGER   # a wider width is promoted to later
  • Never narrows: clearCounts and subtract keep the current width.
  • Encoding is unchanged: an auto histogram encodes like the fixed width it has reached and decodes into any bitBucketSize, including "auto".
  • useWebAssembly: true with "auto" throws not supported with WebAssembly.

Evidence

  • Before: on fix: 8/16/32-bit histograms throw on count overflow instead of wrapping #77, an 8-bit histogram throws at count 256; there is no width that adapts.
    After: src/AutoCountsHistogram.spec.ts and src/AutoCountsHistogram.fc.spec.ts: promotion at 256, 65536 and 2^32 without wrapping, a direct jump to the needed width, counts preserved across promotions, same counts, total, extremes and percentiles as a Float64 twin under random record/add/subtract sequences (fast-check), add/subtract/clear/copy-with-CO-correction, encoding round trips into 8/16/32/64/packed/auto.
  • Before: recordValueWithCount(123456, 300) on an auto-resizing auto histogram threw 300 would overflow 8-bit integer count, because fix: 8/16/32-bit histograms throw on count overflow instead of wrapping #77's pre-resize check used the initial 8-bit limit.
    After: it promotes to 16 bits and records 300. A negative count still throws before the resize.
npm test            422 passed, 3 skipped
npm run typecheck   clean

Speed, src/bench/counts-promotion.bench.ts, one variant per process (COUNTS_BENCH_ONLY), ops/s, three runs:

Bench Int32 auto
record 1024 samples, fresh histogram 181k to 186k 214k to 218k +18%
record 1024 samples, long-lived histogram (promoted to 32 bits) 398k to 418k 282k to 287k -30%
getValueAtPercentile(99) 123k to 192k 126k to 188k noise
summary 97k to 102k 97k to 101k same

A fresh histogram is faster because its counts array is 4x smaller. A long-lived one is slower once promoted: the likely cause is that the counts load and store in incrementCountAtIndex have seen three typed array kinds, so V8 compiles them polymorphic (not confirmed with a V8 trace). One width-specific function per kind, installed on the instance, was tried and was slower (180k), probably because the call is no longer inlined.

Merge Danger

Door: two-way

Opt-in only: no existing bitBucketSize changes behaviour. Removing "auto" later would be a breaking change for anyone who adopted it, so it is marked experimental.

Blast Radius: contained

TypedArrayHistogram gets a protected checkRecordedCount hook and a protected arrayCtr; the fixed-width classes behave as in #77. BitBucketSize gains "auto", which TypeScript users switching exhaustively over it will notice.

  • Decision for review: merge as experimental with the steady-state cost documented, or keep it as a draft until the polymorphism is solved.

🤖 Generated with Claude Code

…> 64 on demand)

AutoCountsHistogram starts with 8-bit counts and copies them into the
next wider typed array the first time a count does not fit, instead of
throwing (the fixed-width behaviour from the count overflow fix). It is
JS only; build() refuses 'auto' with useWebAssembly.

The range check run before an auto-resize is a protected hook so the
auto histogram promotes there too, and only refuses negative or unsafe
counts.

Co-Authored-By: Claude Haiku 5.5 <noreply@anthropic.com>
Co-Authored-By: Claude Opus 5.5 <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