Conversation
Collaborator
|
Review requested:
|
jasnell
force-pushed
the
jasnell/perf-hooks-histogram-snapshot
branch
from
September 18, 2026 03:35
ebdc07b to
2c4057d
Compare
Cloning a histogram with `structuredClone()` or `postMessage()` shares the native histogram instead of copying it. Capturing the state of a histogram at a point in time, while other code keeps recording into it, requires serializing it with `export()` and parsing the result with `importHistogram()`. Add `histogram.snapshot()`, which returns a new, independent `Histogram` containing a copy of the histogram's configuration, recorded values, `exceeds` count, and EWMA state, without the serialization round trip. Values cannot be recorded into the returned histogram. The method is available on all histograms, including `RecordableHistogram` and `ELDHistogram` instances. Signed-off-by: James M Snell <jasnell@gmail.com> Assisted-by: OpenCode
The native object behind histograms created by `createHistogram()`, `importHistogram()`, and `snapshot()` did not report the memory of its HDR histogram to V8. That memory is about 352 KiB with the default options, and V8 did not take it into account when scheduling garbage collection. Short-lived histograms could therefore hold on to a large amount of native memory until an unrelated garbage collection: in a loop taking 2,000 snapshots, RSS grew by 477 MiB. Report the size of the native histogram while the object is alive, as `SlidingWindowHistogram` already does for its chunks. Objects that share a native histogram after cloning each report its full size. With this change, RSS grows by 8 MiB in the same loop. Signed-off-by: James M Snell <jasnell@gmail.com> Assisted-by: OpenCode
Getting the values recorded during an interval requires calling `reset()`, which removes them for every other user of the histogram, or copying and subtracting histograms, which silently produces a wrong result when the source was reset in between. Add `histogram.diff(other)`, which returns a new read-only `Histogram` containing the values recorded after `other`, an earlier snapshot of the histogram, was taken. Neither histogram is changed. Unlike `subtract()`, it verifies that both histograms have the same layout, and it throws instead of clamping when `other` contains values that the histogram does not. Add `histogram.resetCount`, the number of calls to `reset()` and `subtract()`, which `snapshot()` copies. `diff()` throws when the counts differ, so a reset between two snapshots is detected even when every bucket has since grown past its previous count. Signed-off-by: James M Snell <jasnell@gmail.com> Assisted-by: OpenCode
jasnell
force-pushed
the
jasnell/perf-hooks-histogram-snapshot
branch
from
September 18, 2026 03:45
2c4057d to
cbb0180
Compare
Codecov Report❌ Patch coverage is
Additional details and impacted files@@ Coverage Diff @@
## main #66099 +/- ##
==========================================
+ Coverage 90.27% 90.28% +0.01%
==========================================
Files 790 790
Lines 271591 271738 +147
Branches 51829 51861 +32
==========================================
+ Hits 245185 245352 +167
+ Misses 16914 16878 -36
- Partials 9492 9508 +16
🚀 New features to boost your workflow:
|
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.
Cloning a histogram with
structuredClone()orpostMessage()sharesthe native histogram instead of copying it. Capturing the state of a
histogram at a point in time, while other code keeps recording into
it, requires serializing it with
export()and parsing the resultwith
importHistogram().This adds
histogram.snapshot()and `histogram.diff().snapshot()returns a new, independentHistogramcontaining acopy of the histogram's configuration, recorded values,
exceedscount, and EWMA state, without the serialization round trip. Values
cannot be recorded into the returned histogram. The method is
available on all histograms, including
RecordableHistogramandELDHistograminstances.diff()returns a new, independentHistogramcontaining thedelta between snapshots. This is useful for identifying what has
changed in an histogram between points in time.