]> Piment Noir Git Repositories - poolifier.git/commit
perf(pools): make measurement statistics allocation free (#3412)
authorJérôme Benoit <jerome.benoit@piment-noir.org>
Wed, 18 Nov 2026 00:14:32 +0000 (01:14 +0100)
committerGitHub <noreply@github.com>
Wed, 18 Nov 2026 00:14:32 +0000 (01:14 +0100)
commit6c3bfec204e7fd075ca7ecc2e2793d5e93a2339d
tree8cccf32c1f7061583af2a310e20e7cc40d83b4a3
parentf95e2d2106b238e8964e01ce925f2f726e50b28c
perf(pools): make measurement statistics allocation free (#3412)

* perf(pools): make measurement statistics allocation free

Compute the average directly from the circular buffer and the median via
a guarded quickselect over a shared snapshot instead of
`toArray()`+`slice().sort()`:

- average: O(H) single pass over the buffer (`CircularBuffer.sum()`), no allocation
- median: O(H) quickselect over a shared snapshot (native-sort depth
  fallback keeps the worst case at O(H log H)), no allocation

Results are bit-for-bit identical to `average(history.toArray())` and
`median(history.toArray())`, fuzz-tested against reference
implementations with `Object.is`.

Measured: median path x7.2, average path x1.56 per task completion;
+10 % throughput on a 2-core fair-share pool.

* [autofix.ci] apply automated fixes

* test(pools): make statistics fuzz falsifiable and cover sum() directly

Feed the bit-for-bit fuzz with wide-dynamics values (1e-8 to 1e8) so
that the float64 summation order becomes observable: a mutation
reversing the accumulation order of `sum()` now fails the test (checked
by mutation). Add a direct `sum()` unit test (empty, full, wrap,
partial drain).

* fix(pools): preserve negative zero in single-value measurement average

`sum()` folds `- 0` into `+ 0` (`0 + (- 0)`), while the replaced
`average()` returned `dataSet[0]` as is. Read the single element from
the snapshot instead, and pin the behavior with an `Object.is` test.
Also document the remaining known difference of the quickselect median
(occurrence sign among equal-comparing `- 0`/`NaN` values, unreachable
from measurement producers).

* [autofix.ci] apply automated fixes

* refactor(pools): name the rank-selection tuning constants

Extract the insertion-sort cutoff and the quickselect depth factor of
`selectAtRank()` into documented module constants, following the
existing private-constant convention. No behavior change.

---------

Co-authored-by: autofix-ci[bot] <114827586+autofix-ci[bot]@users.noreply.github.com>
src/circular-buffer.ts
src/pools/utils.ts
tests/circular-buffer.test.mjs
tests/pools/utils.test.mjs