From e2ae016df8a34906eba735b8dcb4947d11e124db Mon Sep 17 00:00:00 2001 From: James M Snell Date: Fri, 18 Sep 2026 00:16:09 +0000 Subject: [PATCH] perf_hooks: add histogram export format version 2 Histograms exported by Node.js v26.9.0 use format version 1, which rejects unknown keys on import. Adding fields to version 1 data would therefore break importing it in v26.9.0, even though that release claims to support version 1. Introduce format version 2. It has the same layout as version 1, but unknown keys are ignored on import, so fields can be added to it later without changing the version again, while older releases reject it based on the version rather than on the new fields. `histogram.export()` now produces version 2 data. `importHistogram()` accepts versions 1 and 2, and imports version 1 data, as well as data without a version, with the original semantics. Assisted-by: OpenCode Signed-off-by: James M Snell PR-URL: https://github.com/nodejs/node/pull/66098 Reviewed-By: Xuguang Mei --- doc/api/perf_hooks.md | 32 +++- src/histogram.cc | 98 +++++++++++- .../test-perf-hooks-histogram-import.js | 144 ++++++++++++++++++ 3 files changed, 266 insertions(+), 8 deletions(-) diff --git a/doc/api/perf_hooks.md b/doc/api/perf_hooks.md index 545a5e4f9aa..0667757b704 100644 --- a/doc/api/perf_hooks.md +++ b/doc/api/perf_hooks.md @@ -1816,6 +1816,11 @@ console.log(snapshot.percentile(99)); * `data` {Uint8Array} A CBOR-encoded histogram previously produced by @@ -1826,6 +1831,9 @@ Reconstructs a histogram from a CBOR-encoded `Uint8Array`. The returned histogram is a full {RecordableHistogram} with all bucket data, configuration, and EWMA state restored. New values can be recorded into it. +Data in any format version produced by [`histogram.export()`][] can be +imported. See [histogram export format compatibility][] for details. + ```js const { createHistogram, importHistogram } = require('node:perf_hooks'); @@ -2205,6 +2213,10 @@ loop delay threshold. * Returns: {Uint8Array} @@ -2223,7 +2235,7 @@ The CBOR payload is a map with integer keys: | Key | Type | Field | | --- | ------- | --------------------------------------------- | -| 0 | uint | Format version (currently 1) | +| 0 | uint | Format version (currently 2) | | 1 | uint | Lowest discernible value | | 2 | uint | Highest trackable value | | 3 | uint | Significant figures | @@ -2238,6 +2250,23 @@ The CBOR payload is a map with integer keys: Any standard CBOR decoder can parse the output. +#### Histogram export format compatibility + +[`perf_hooks.importHistogram()`][] accepts every format version that +`histogram.export()` has produced: + +* Version 1 was produced by Node.js v26.9.0. Data with a version 1 key, or + without a version key, is imported with the original semantics: keys + that are not listed above are rejected. +* Version 2 has the same layout as version 1. Keys that are not recognized + are ignored, so later versions of Node.js can add fields to version 2 + data without changing the version, and the data remains importable. + +Data with any other version is rejected. + +When the total count, min, or max value is absent, it is derived from the +bucket counts. A total count that is present must match the bucket counts. + ### `histogram.ewmaMean`