From 76ab721fe766aca55fac4e6372fff9355de561d6 Mon Sep 17 00:00:00 2001 From: Daniel Lemire Date: Sun, 22 Nov 2026 15:49:03 -0500 Subject: [PATCH] src: reserve simdjson padding in parsePackageJSON MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit `parsePackageJSON()` copies its input into a `std::string` with no spare capacity. `simdjson::pad()` then appends `SIMDJSON_PADDING` spaces, so every call reallocated and copied the whole document. Reserve room for the padding when the input is copied so that the append happens in place. The padding stays within the string's size, so simdjson never reads past the end of the string. Add a benchmark that parses the package.json files under deps/. Assisted-by: an AI coding agent Signed-off-by: Daniel Lemire PR-URL: https://github.com/nodejs/node/pull/67623 Refs: https://github.com/nodejs/node/issues/55584 Refs: https://github.com/nodejs/node/pull/55591 Refs: https://github.com/nodejs/node/pull/67620 Reviewed-By: Yagiz Nizipli Reviewed-By: Gürgün Dayıoğlu Reviewed-By: James M Snell --- benchmark/module/parse-package-json.js | 59 ++++++++++++++++++++++++++ src/node_modules.cc | 9 +++- 2 files changed, 66 insertions(+), 2 deletions(-) create mode 100644 benchmark/module/parse-package-json.js diff --git a/benchmark/module/parse-package-json.js b/benchmark/module/parse-package-json.js new file mode 100644 index 00000000000..ec05340cc42 --- /dev/null +++ b/benchmark/module/parse-package-json.js @@ -0,0 +1,59 @@ +'use strict'; + +// Parses the package.json files found under deps/ with the internal +// parsePackageJSON() binding, grouped by file size. + +const common = require('../common.js'); +const fs = require('fs'); +const path = require('path'); + +const buckets = { + '0-512': [0, 512], + '512-1024': [512, 1024], + '1024-2048': [1024, 2048], + '2048+': [2048, Infinity], +}; + +const bench = common.createBenchmark(main, { + size: Object.keys(buckets), + n: [1e6], +}, { + flags: ['--expose-internals'], +}); + +function loadCorpus(parsePackageJSON) { + const deps = path.resolve(__dirname, '../../deps'); + const seen = new Set(); + const corpus = []; + for (const entry of fs.readdirSync(deps, { recursive: true })) { + if (path.basename(entry) !== 'package.json') continue; + const source = fs.readFileSync(path.join(deps, entry)); + const key = source.toString('latin1'); + if (seen.has(key)) continue; + seen.add(key); + try { + parsePackageJSON(source, '/benchmark/package.json'); + } catch { + continue; // Skip test fixtures that are not valid package configs. + } + corpus.push(source); + } + return corpus; +} + +function main({ size, n }) { + const { internalBinding } = require('internal/test/binding'); + const { parsePackageJSON } = internalBinding('modules'); + const [min, max] = buckets[size]; + const files = loadCorpus(parsePackageJSON) + .filter((source) => source.length >= min && source.length < max); + if (files.length === 0) { + throw new Error(`no package.json under deps/ in the ${size} bucket`); + } + + bench.start(); + for (let i = 0; i < n; i++) { + parsePackageJSON(files[i % files.length], '/benchmark/package.json'); + } + bench.end(n); +} diff --git a/src/node_modules.cc b/src/node_modules.cc index f32abb90d9d..6aaca483a0c 100644 --- a/src/node_modules.cc +++ b/src/node_modules.cc @@ -360,12 +360,17 @@ void BindingData::ParsePackageJSONBinding( PackageConfig package_config{}; package_config.file_path = json_path.ToStringView(); + // Reserve room for the padding that simdjson::pad() appends in + // ParsePackageJSON(), so that it does not reallocate and copy the input. if (args[0]->IsString()) { Utf8Value source(isolate, args[0]); - package_config.raw_json = source.ToString(); + package_config.raw_json.reserve(source.length() + + simdjson::SIMDJSON_PADDING); + package_config.raw_json.assign(*source, source.length()); } else { ArrayBufferViewContents view(args[0]); - package_config.raw_json = std::string(view.data(), view.length()); + package_config.raw_json.reserve(view.length() + simdjson::SIMDJSON_PADDING); + package_config.raw_json.assign(view.data(), view.length()); } if (!ParsePackageJSON(