Skip to content

fix: 8/16/32-bit histograms throw on count overflow instead of wrapping - #77

Merged
alexvictoor merged 3 commits into
masterfrom
fix/count-overflow
Nov 22, 2026
Merged

alexvictoor merged 3 commits into
masterfrom
fix/count-overflow

Conversation

@alexvictoor

@alexvictoor alexvictoor commented Nov 15, 2026 •

Copy link
Copy Markdown
Member

Summary

Fixed-width count storage (8/16/32-bit, JS typed arrays and the AssemblyScript Histogram8/16/32) now range-checks each write and throws instead of wrapping, matching Java's ShortCountsHistogram / IntCountsHistogram.

flowchart TD
    K["SIMD add/subtract block"] --> C{"any lane wraps? (lane-wise lt_u)"}
    C -- no --> V["vector store"]
    C -- yes --> W
    W["write to a cell: increment, addToCountAtIndex, setCountAtIndex"] --> N["newCount = cell + delta, in a wide type"]
    N --> O{"newCount above the width's max?"}
    O -- yes --> E1["throw: n would overflow 8/16/32-bit integer count"]
    O -- no --> U{"newCount below 0?"}
    U -- yes --> E2["throw: n would underflow 8/16/32-bit integer count"]
    U -- no --> S["store newCount"]
Loading

On the WASM side the 64-bit histogram also throws on subtract underflow (it wrapped to about 2^64) and on a negative count (it was dropped by the u64 cast); the JS Float64 and packed histograms accept negative counts as before. An auto-resizing JS histogram checks the count against its width before growing, so a rejected recordValueWithCount leaves highestTrackableValue unchanged. The WASM histograms do not have that pre-check yet: an 8-bit WASM histogram with autoResize grows from 1000 to 1,000,000 before rejecting recordValueWithCount(1000000, 300) (known gap, found in review). The WASM abort hook now surfaces the AssemblyScript message instead of only "Abort in WebAssembly at L:C".

Java semantics, kept on purpose: an add/subtract that fails part-way leaves the earlier cells applied and totalCount unchanged, exactly as Java's add. Pre-validating the whole range cost 5-7x on add, so it was not kept; tests document the partial application instead.

Evidence

  • Before: a probe on master (setCountAtIndex(i, 255) then recordValueWithCount(v, 1)):

    JS int8   recordValueWithCount(max)+1  -> count 0, totalCount 256   (silent wrap)
    JS int32  recordValueWithCount(max)+1  -> count 0, totalCount 2^32  (silent wrap)
    JS int32  recordValueWithCount(-1)     -> count 4294967295         (silent wrap)
    WASM 8    recordValueWithCount(max)+1  -> totalCount 256, cell 0   (silent wrap)
    WASM 8    recordValueWithCount(-1)     -> dropped (f64 -> u64 saturates to 0)
    

    After:

    JS int8   recordValueWithCount(max)+1  -> throws "256 would overflow 8-bit integer count"
    JS int32  recordValueWithCount(-1)     -> throws "-1 would underflow 32-bit integer count"
    JS int32  add (cell at max)            -> throws, histogram.summary unchanged
    WASM 8    recordValueWithCount(max)+1  -> throws "256 would overflow 8-bit integer count"
    WASM 8    recordValueWithCount(-1)     -> throws "-1 would underflow 8-bit integer count"
    

    Failing-then-passing test (pseudocode): h.setCount(42, MAX); expect(() => h.record(42, 1)).toThrow("would overflow 8-bit"); expect(h.summary).toEqual(before); then h.setCount(42, MAX - 1); h.record(42, 1) succeeds.

    Bench (npx vitest bench --run --reporter verbose, ops/s, two runs each where noted; noise is about ±5-10%):

    Benchmark master branch
    Int32Histogram record (data access) 23.40M 23.41M, 23.63M
    WASM Int32Histogram record 30.99M 30.77M, 30.43M
    Int32Histogram add 44.0k, 50.6k 48.0k, 43.8k
    WASM 32B Histogram add 592k, 789k 589k, 676k

    Recording is within noise. Add is unchanged within noise after the SIMD break-out; the scalar pre-validation variant was 105k (WASM 32) and 9k (Int32) and was dropped.

    After (tests): npm test 384 passed after the rebase on master 41cc6e6 (324 before it), including the partial-application cases on add and subtract, the 64-bit WASM underflow and negative-count cases, and the autoResize pre-check; npm run astest:ci passes with the new AS boundary tests; npm run typecheck clean. The add bench was re-run before and after the review follow-ups: WASM 32B 611k to 631k before, 617k to 636k after.

Merge Danger

Door: two-way

Reverting restores silent wrapping; no data or format changes.

Blast Radius: moderate

Code that relied on wrapping, or on negative counts being dropped in WASM, will now throw, on 64-bit WASM histograms too. The throw is the intended behaviour, but callers that recorded large counts into 8/16-bit histograms will see exceptions where they previously got garbage. Thrown values for the WASM path are now Error objects with the message (they were Abort in WebAssembly at L:C). The JS path now throws Error objects rather than strings.

Overlaps #26 (external, older fix for integer overflows): if this merges, #26 can be closed.

🤖 Generated with Claude Code

alexvictoor and others added 3 commits November 21, 2026 15:18
8/16/32-bit count storage is unsigned, so a count past the cell width
wrapped silently (255 + 1 -> 0 in an 8-bit JS array, 2^32 -> 0 in the
32-bit WASM build), and a negative count was stored as a huge value or
dropped. The JS `newCount < 0` check could never fire for unsigned arrays.

Writes now range-check against the cell width, as Java's
ShortCountsHistogram and IntCountsHistogram do, and throw
"<n> would overflow <bits>-bit integer count" or
"<n> would underflow <bits>-bit integer count". The check runs before
the write, so the offending cell is never modified. 64-bit, Float64 and
packed storage keep their previous behaviour.

WASM aborts now carry the AssemblyScript message instead of only the
source location, which optimized builds do not have.

Known limitation: add/subtract of same-shaped histograms are not atomic.
An overflow part-way through leaves the cells before the failing one
applied and totalCount unchanged, as Java's add does. Pre-validating the
whole range was measured at 5-7x slower on add and was not kept.

Co-Authored-By: Claude Fable 5.1 <noreply@anthropic.com>
Co-Authored-By: Claude Fable 5.1 <noreply@anthropic.com>
- Restore @inline on recordCountAtValue; recordCountAtValueFromF64 is undecorated
- Remove the duplicated @ts-ignore in incrementCountAtIndex
- WASM 64-bit: subtract underflow throws (scalar and vector paths, u64 lanes
  compared unsigned via a sign bias)
- WASM: a negative count throws for every width, instead of saturating to 0 for u64
- Typed array histograms check the count before an auto-resize, so a rejected
  count leaves highestTrackableValue unchanged
- Tests: rename the single-cell add/subtract tests, add Java-semantics tests
  (earlier cells stay applied when a later cell throws), add 64-bit WASM tests

Co-Authored-By: Claude Fable 5.1 <noreply@anthropic.com>
@alexvictoor
alexvictoor marked this pull request as ready for review November 22, 2026 14:56
@alexvictoor
alexvictoor merged commit b8e3a7b 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