Repository navigation
perf: compute several percentiles in one counts scan - #72
Merged
Merged
Conversation
alexvictoor
marked this pull request as ready for review
November 15, 2026 19:20
Add getValuesAtPercentiles(percentiles) to every histogram implementation (JS, packed, Float64, WASM). One bounded scan of the counts fills all requested percentiles, in input order, instead of one scan per percentile. The summary getter uses it, so the eight summary percentiles take one pass. Co-Authored-By: Claude Haiku 5.5 <noreply@anthropic.com>
- Percentiles are clamped to [0, 100] in countAtPercentile on both sides, so negative and -Infinity percentiles give the lowest value in JS and WASM alike (JS used to return 0 because ulp(x <= 0) is NaN, and in the AS batch path a -Infinity entry produced a NaN threshold that blocked every later entry). - The AS getValueAtPercentile returns 0 for a NaN percentile, as the JS one does, instead of reporting the value at index 0. - The AS batch path allocates nothing per call: thresholds and the sorted positions live in module-level scratch state, the results overwrite the input array, and the positions are sorted with Array#sort rather than an insertion sort. - The wasm wrapper reuses a pinned scratch Float64Array instead of allocating an input array and a result array per call (the minimal runtime only collects on destroy), and throws on a destroyed histogram. - newUint8Array and the scratch array share newArrayBufferView; the 64-byte block sum and the percentile-to-value rule are shared between the single and batch methods. - Tests cover negative, infinite and NaN percentiles (also with a unit magnitude above 0) and the destroyed-histogram guard. Co-Authored-By: Claude Fable 5.1 <noreply@anthropic.com>
…onventions Co-Authored-By: Claude Fable 5.1 <noreply@anthropic.com>
`histogram.getValuesAtPercentiles(50, 99, 99.9)` reads like the single percentile call; a list is passed with the spread operator. The wasm boundary still receives one Float64Array, so the AS side is unchanged. Co-Authored-By: Claude Fable 5.1 <noreply@anthropic.com>
Master now overrides getValueAtPercentile in PackedHistogram to walk only the populated indexes. That override computed its own threshold without clamping negative percentiles to 0, and ulp() of a negative number is NaN, so a negative percentile returned 0 on a packed histogram while getValuesAtPercentiles returned the lowest recorded value. Both now share countAtPercentile and valueAtPercentileIndex, and getValuesAtPercentiles walks the counts through nextNonZeroIndexInRange so packed histograms skip their empty subtrees there too. Co-Authored-By: Claude Fable 5.1 <noreply@anthropic.com>
alexvictoor
force-pushed
the
perf/multi-percentile-scan
branch
from
November 15, 2026 21:17
7d64036 to
8e139f3
Compare
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
summary(and sotoJSON) asked for eight percentiles one at a time, and each call scanned the counts. One call now answers all of them with one scan.flowchart TD A["thresholds = countAtPercentile(p) for each p, shared with getValueAtPercentile"] --> B["positions sorted by threshold, NaN left out (result stays 0)"] B --> C["i = next index that may hold a count, up to highestNonZero (bounds from #70; packed storage skips empty subtrees, #69)"] C --> D{"flat storage and next threshold beyond this 64-byte block?"} D -- yes --> E["total += SIMD block sum, skip the block"] --> C D -- no --> F["total += count(i)"] F --> G{"total reaches the next threshold?"} G -- yes --> H["result[position] = value at i (same 0.0 / highestEquivalent rule)"] --> G G -- no --> C C -- all resolved --> R["results in input order"]getValuesAtPercentiles(...percentiles)takes the percentiles as varargs, sohistogram.getValuesAtPercentiles(50, 99, 99.9)reads like the single call and a list is passed with the spread operator. The wasm boundary still receives oneFloat64Array.countAtPercentileis shared withgetValueAtPercentileand now clamps to [0, 100] on both sides (see Merge Danger).Rebased on #69. The
getValueAtPercentileoverride that #69 added toPackedHistogramcomputed its own threshold without the clamp, andulp()of a negative number is NaN, so a negative percentile returned 0 on a JS packed histogram whilegetValuesAtPercentilesreturned the lowest recorded value. CI caught it on the merge commit. The override now uses the shared helpers, andgetValuesAtPercentileswalks the counts throughnextNonZeroIndexInRange, so packed histograms skip their empty subtrees there too.Evidence
npx vitest bench src/bench/histogram-json-percentile.bench.tson master, toSummary ops/s.After: same bench on this branch.
Measured after the rebase on #69 (master at bdfa37d, two runs; branch at the rebased head). The JS rows gain from the
nextNonZeroIndexInRangewalk: on a sparse Int32 histogram,getValuesAtPercentileswith eight percentiles went from 7.6k – 12k to 33.9k ops/s against the index-by-index scan of the first version, and from 1.8k – 3.2k to 6.0k on a JS packed one.The single-percentile bench (
histogram-percentile.bench.ts) moves by about 40% between runs on this machine, so no change is claimed forgetValueAtPercentile.After:
src/Histogram.valuesAtPercentiles.spec.ts, 47 tests over the five implementations. Fast-check property:getValuesAtPercentiles(...list)equalslist.map(getValueAtPercentile)for unsorted and duplicate percentiles in [-10, 110] plus 0 / 100 / 150 / -1 / ±Infinity, no argument, empty histogram, NaN (also with a unit magnitude above 0), input order, the destroyed-histogram guard on WASM, andsummaryagainst the per-percentile values.Merge Danger
Door: two-way
Additive method,
summaryreturns the same values as before. A revert restores the eight scans.Blast Radius: interface
Histogramgains a required member, so an external class that implements the interface by hand stops compiling until it addsgetValuesAtPercentiles. Observable change togetValueAtPercentileitself: a negative percentile now returns the lowest recorded value on JS (as on WASM and in Java) instead of 0, and NaN returns 0 on WASM instead of the value at index 0.🤖 Generated with Claude Code