Skip to content

Conversation

@joyeecheung
Copy link
Member

@joyeecheung joyeecheung commented Oct 27, 2025

src: add COUNT_GENERIC_USAGE utility for tests

src: fix timing of snapshot serialize callback

Previously the addAfterUserSerailizeCallback() wasn't
ready to be used for building the built-in snapshot.
This patch initializes the callbacks at the time
lib/internal/v8/start_snapshot.js is loaded, so that
these callbacks get run correctly when building the
built-in snapshot.

Currently when building the built-in snapshot,
addAfterUserSerializeCallback() is only used by createUnsafeBuffer(),
other usages can only come from user-land snapshots,
which is covered by tests, but what gets run by the
built-in snapshot building process is less visible, and the
path used by createUnsafeBuffer() isn't reliably visible in user
land either. This adds an internal usage counter in debug builds
to verify this path when building the built-in snapshot.

Fixes: #60423

@nodejs-github-bot nodejs-github-bot added c++ Issues and PRs that require attention from people who are familiar with C++. lib / src Issues and PRs related to general changes in the lib or src directory. needs-ci PRs that need a full CI run. labels Oct 27, 2025
Previously the addAfterUserSerailizeCallback() wasn't
ready to be used for building the built-in snapshot.
This patch initializes the callbacks at the time
lib/internal/v8/start_snapshot.js is loaded, so that
these callbacks get run correctly when building the
built-in snapshot.

Currently when building the built-in snapshot,
addAfterUserSerializeCallback() is only used by createUnsafeBuffer(),
other usages can only come from user-land snapshots,
which is covered by tests, but what gets run by the
built-in snapshot building process is less visible, and the
path used by createUnsafeBuffer() isn't reliably visible in user
land either. This adds an internal usage counter in debug builds
to verify this path when building the built-in snapshot.
Copy link
Member

@ChALkeR ChALkeR left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Note: this effectively disables --zero-fill-buffers default being on since 24.0.0
If not for a significant perf impact, this should have been a semver-major change at this point

@codecov
Copy link

codecov bot commented Oct 27, 2025

Codecov Report

✅ All modified and coverable lines are covered by tests.
✅ Project coverage is 88.57%. Comparing base (3c8c1ef) to head (d6d4659).
⚠️ Report is 21 commits behind head on main.

Additional details and impacted files
@@            Coverage Diff             @@
##             main   #60434      +/-   ##
==========================================
+ Coverage   88.07%   88.57%   +0.50%     
==========================================
  Files         704      704              
  Lines      207778   207814      +36     
  Branches    39949    40038      +89     
==========================================
+ Hits       182998   184078    +1080     
+ Misses      16791    15775    -1016     
+ Partials     7989     7961      -28     
Files with missing lines Coverage Δ
lib/internal/main/mksnapshot.js 95.87% <ø> (-0.04%) ⬇️
lib/internal/v8/startup_snapshot.js 95.38% <100.00%> (+9.23%) ⬆️
src/api/environment.cc 76.88% <100.00%> (-0.14%) ⬇️

... and 103 files with indirect coverage changes

🚀 New features to boost your workflow:
  • ❄️ Test Analytics: Detect flaky tests, report on failures, and find test suite problems.
  • 📦 JS Bundle Analysis: Save yourself from yourself by tracking and limiting bundle sizes in JS merges.

Copy link
Member

@mcollina mcollina left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

RSLGTM

@mcollina mcollina added the request-ci Add this label to start a Jenkins CI on a PR. label Oct 27, 2025
@github-actions github-actions bot removed the request-ci Add this label to start a Jenkins CI on a PR. label Oct 27, 2025
@nodejs-github-bot
Copy link
Collaborator

@ChALkeR
Copy link
Member

ChALkeR commented Oct 27, 2025

Also this likely should be both a notable change and have a clear description that it disables default buffer zero-filling, which has been accidental

Copy link
Contributor

@aduh95 aduh95 left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Testing ./node -e 'for (let i = 0; i < 1e6; i++) {if(Buffer.allocUnsafe(4096).some(x=>x))process.exit(0)}process.exit(1)' is failing without this patch, and passing with it

@nodejs-github-bot
Copy link
Collaborator

@aduh95 aduh95 added fast-track PRs that do not need to wait for 48 hours to land. author ready PRs that have at least one approval, no pending requests for changes, and a CI started. labels Oct 27, 2025
@github-actions
Copy link
Contributor

Fast-track has been requested by @aduh95. Please 👍 to approve.

@aduh95
Copy link
Contributor

aduh95 commented Oct 27, 2025

Fast track so it can get into #60436?

@nodejs-github-bot
Copy link
Collaborator

@aduh95 aduh95 added baking-for-lts PRs that need to wait before landing in a LTS release. commit-queue Add this label to land a pull request using GitHub Actions. labels Oct 28, 2025
@nodejs-github-bot nodejs-github-bot removed the commit-queue Add this label to land a pull request using GitHub Actions. label Oct 28, 2025
@aduh95
Copy link
Contributor

aduh95 commented Oct 28, 2025

Adding baking-for-lts PRs that need to wait before landing in a LTS release. as I don't think there's consensus regarding whether this should land on v24.x.

@nodejs-github-bot nodejs-github-bot added the commit-queue-failed An error occurred while landing this pull request using GitHub Actions. label Oct 28, 2025
@nodejs-github-bot
Copy link
Collaborator

Commit Queue failed
- Loading data for nodejs/node/pull/60434
✔  Done loading data for nodejs/node/pull/60434
----------------------------------- PR info ------------------------------------
Title      src: fix timing of snapshot serialize callback (#60434)
Author     Joyee Cheung <[email protected]> (@joyeecheung)
Branch     joyeecheung:buffer -> nodejs:main
Labels     c++, lib / src, baking-for-lts, fast-track, author ready, needs-ci
Commits    4
 - src: add COUNT_GENERIC_USAGE utility for tests
 - src: fix timing of snapshot serialize callback
 - fixup! src: fix timing of snapshot serialize callback
 - fixup! fixup! src: fix timing of snapshot serialize callback
Committers 1
 - Joyee Cheung <[email protected]>
PR-URL: https://github.com/nodejs/node/pull/60434
Fixes: https://github.com/nodejs/node/issues/60423
Reviewed-By: Matteo Collina <[email protected]>
Reviewed-By: Antoine du Hamel <[email protected]>
Reviewed-By: Richard Lau <[email protected]>
------------------------------ Generated metadata ------------------------------
PR-URL: https://github.com/nodejs/node/pull/60434
Fixes: https://github.com/nodejs/node/issues/60423
Reviewed-By: Matteo Collina <[email protected]>
Reviewed-By: Antoine du Hamel <[email protected]>
Reviewed-By: Richard Lau <[email protected]>
--------------------------------------------------------------------------------
   ℹ  This PR was created on Mon, 27 Oct 2025 14:31:11 GMT
   ✔  Approvals: 3
   ✔  - Matteo Collina (@mcollina) (TSC): https://github.com/nodejs/node/pull/60434#pullrequestreview-3384325240
   ✔  - Antoine du Hamel (@aduh95) (TSC): https://github.com/nodejs/node/pull/60434#pullrequestreview-3384634586
   ✔  - Richard Lau (@richardlau) (TSC): https://github.com/nodejs/node/pull/60434#pullrequestreview-3386023877
   ℹ  This PR is being fast-tracked
   ✔  Last GitHub CI successful
   ℹ  Last Full PR CI on 2025-10-27T22:09:50Z: https://ci.nodejs.org/job/node-test-pull-request/69909/
- Querying data for job/node-test-pull-request/69909/
   ✔  Last Jenkins CI successful
--------------------------------------------------------------------------------
   ✔  No git cherry-pick in progress
   ✔  No git am in progress
   ✔  No git rebase in progress
--------------------------------------------------------------------------------
- Bringing origin/main up to date...
From https://github.com/nodejs/node
 * branch                  main       -> FETCH_HEAD
✔  origin/main is now up-to-date
- Downloading patch for 60434
From https://github.com/nodejs/node
 * branch                  refs/pull/60434/merge -> FETCH_HEAD
✔  Fetched commits as 51a57f2b3629..d6d46594f8e8
--------------------------------------------------------------------------------
[main 74f6c4aa82] src: add COUNT_GENERIC_USAGE utility for tests
 Author: Joyee Cheung <[email protected]>
 Date: Mon Oct 27 13:42:30 2025 +0100
 3 files changed, 35 insertions(+), 2 deletions(-)
[main 7f70321948] src: fix timing of snapshot serialize callback
 Author: Joyee Cheung <[email protected]>
 Date: Mon Oct 27 13:43:09 2025 +0100
 3 files changed, 18 insertions(+), 3 deletions(-)
 create mode 100644 test/parallel/test-buffer-alloc-unsafe-is-uninitialized.js
[main 9f399a1b9a] fixup! src: fix timing of snapshot serialize callback
 Author: Joyee Cheung <[email protected]>
 Date: Mon Oct 27 17:42:51 2025 +0100
 2 files changed, 21 insertions(+)
 create mode 100644 test/parallel/test-buffer-alloc-unsafe-is-initialized-with-zero-fill-flag.js
[main d7be4afbbf] fixup! fixup! src: fix timing of snapshot serialize callback
 Author: Joyee Cheung <[email protected]>
 Date: Mon Oct 27 18:11:18 2025 +0100
 2 files changed, 14 insertions(+), 8 deletions(-)
   ✔  Patches applied
There are 4 commits in the PR. Attempting autorebase.
Rebasing (2/6)
Executing: git node land --amend --yes
--------------------------------- New Message ----------------------------------
src: add COUNT_GENERIC_USAGE utility for tests

PR-URL: #60434
Fixes: #60423
Reviewed-By: Matteo Collina <[email protected]>
Reviewed-By: Antoine du Hamel <[email protected]>
Reviewed-By: Richard Lau <[email protected]>

[detached HEAD 9fdcd00dfb] src: add COUNT_GENERIC_USAGE utility for tests
Author: Joyee Cheung <[email protected]>
Date: Mon Oct 27 13:42:30 2025 +0100
3 files changed, 35 insertions(+), 2 deletions(-)
Rebasing (3/6)
Rebasing (4/6)
Rebasing (5/6)
Rebasing (6/6)
Executing: git node land --amend --yes
--------------------------------- New Message ----------------------------------
src: fix timing of snapshot serialize callback

Previously the addAfterUserSerailizeCallback() wasn't
ready to be used for building the built-in snapshot.
This patch initializes the callbacks at the time
lib/internal/v8/start_snapshot.js is loaded, so that
these callbacks get run correctly when building the
built-in snapshot.

Currently when building the built-in snapshot,
addAfterUserSerializeCallback() is only used by createUnsafeBuffer(),
other usages can only come from user-land snapshots,
which is covered by tests, but what gets run by the
built-in snapshot building process is less visible, and the
path used by createUnsafeBuffer() isn't reliably visible in user
land either. This adds an internal usage counter in debug builds
to verify this path when building the built-in snapshot.

PR-URL: #60434
Fixes: #60423
Reviewed-By: Matteo Collina <[email protected]>
Reviewed-By: Antoine du Hamel <[email protected]>
Reviewed-By: Richard Lau <[email protected]>

[detached HEAD d0d715d66a] src: fix timing of snapshot serialize callback
Author: Joyee Cheung <[email protected]>
Date: Mon Oct 27 13:43:09 2025 +0100
4 files changed, 45 insertions(+), 3 deletions(-)
create mode 100644 test/parallel/test-buffer-alloc-unsafe-is-initialized-with-zero-fill-flag.js
create mode 100644 test/parallel/test-buffer-alloc-unsafe-is-uninitialized.js
Successfully rebased and updated refs/heads/main.

ℹ Add commit-queue-squash label to land the PR as one commit, or commit-queue-rebase to land as separate commits.

https://github.com/nodejs/node/actions/runs/18859727179

@aduh95 aduh95 added commit-queue Add this label to land a pull request using GitHub Actions. commit-queue-rebase Add this label to allow the Commit Queue to land a PR in several commits. and removed commit-queue-failed An error occurred while landing this pull request using GitHub Actions. labels Oct 28, 2025
@nodejs-github-bot nodejs-github-bot removed the commit-queue Add this label to land a pull request using GitHub Actions. label Oct 28, 2025
using v8::Value;

thread_local std::unordered_map<std::string, int> generic_usage_counters;
thread_local std::unordered_map<FastStringKey, int, FastStringKey::Hash>
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Let's make this use FastStringKey as well, that is useful here for the same reason as it is for fast API call count tracking

Comment on lines +32 to +33
if (generic_usage_counters.find(counter_name) == generic_usage_counters.end())
generic_usage_counters[counter_name] = 0;
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
if (generic_usage_counters.find(counter_name) == generic_usage_counters.end())
generic_usage_counters[counter_name] = 0;

... since this doesn't actually do anything, right? operator[] creates the element for you if it doesn't exist

Copy link
Member

@addaleax addaleax Oct 28, 2025

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

will do a separate PR – #60447

@nodejs-github-bot
Copy link
Collaborator

Landed in 51a57f2...1f6b681

nodejs-github-bot pushed a commit that referenced this pull request Oct 28, 2025
PR-URL: #60434
Fixes: #60423
Reviewed-By: Matteo Collina <[email protected]>
Reviewed-By: Antoine du Hamel <[email protected]>
Reviewed-By: Richard Lau <[email protected]>
nodejs-github-bot pushed a commit that referenced this pull request Oct 28, 2025
Previously the addAfterUserSerailizeCallback() wasn't
ready to be used for building the built-in snapshot.
This patch initializes the callbacks at the time
lib/internal/v8/start_snapshot.js is loaded, so that
these callbacks get run correctly when building the
built-in snapshot.

Currently when building the built-in snapshot,
addAfterUserSerializeCallback() is only used by createUnsafeBuffer(),
other usages can only come from user-land snapshots,
which is covered by tests, but what gets run by the
built-in snapshot building process is less visible, and the
path used by createUnsafeBuffer() isn't reliably visible in user
land either. This adds an internal usage counter in debug builds
to verify this path when building the built-in snapshot.

PR-URL: #60434
Fixes: #60423
Reviewed-By: Matteo Collina <[email protected]>
Reviewed-By: Antoine du Hamel <[email protected]>
Reviewed-By: Richard Lau <[email protected]>
aduh95 pushed a commit that referenced this pull request Oct 28, 2025
PR-URL: #60434
Fixes: #60423
Reviewed-By: Matteo Collina <[email protected]>
Reviewed-By: Antoine du Hamel <[email protected]>
Reviewed-By: Richard Lau <[email protected]>
aduh95 pushed a commit that referenced this pull request Oct 28, 2025
Previously the addAfterUserSerailizeCallback() wasn't
ready to be used for building the built-in snapshot.
This patch initializes the callbacks at the time
lib/internal/v8/start_snapshot.js is loaded, so that
these callbacks get run correctly when building the
built-in snapshot.

Currently when building the built-in snapshot,
addAfterUserSerializeCallback() is only used by createUnsafeBuffer(),
other usages can only come from user-land snapshots,
which is covered by tests, but what gets run by the
built-in snapshot building process is less visible, and the
path used by createUnsafeBuffer() isn't reliably visible in user
land either. This adds an internal usage counter in debug builds
to verify this path when building the built-in snapshot.

PR-URL: #60434
Fixes: #60423
Reviewed-By: Matteo Collina <[email protected]>
Reviewed-By: Antoine du Hamel <[email protected]>
Reviewed-By: Richard Lau <[email protected]>
addaleax added a commit to addaleax/node that referenced this pull request Oct 28, 2025
This addresses late review comments for the recently
landed cfbfc1b.

Refs: nodejs#60434 (review)
addaleax added a commit to addaleax/node that referenced this pull request Oct 28, 2025
This addresses late review comments for the recently
landed cfbfc1b and aligns the new code with the
pre-existing V8 fast call counters.

Refs: nodejs#60434 (review)
@ChALkeR
Copy link
Member

ChALkeR commented Oct 28, 2025

Given this landed, how fast can it get into 24?

@panva
Copy link
Member

panva commented Oct 28, 2025

Adding baking-for-lts PRs that need to wait before landing in a LTS release. as I don't think there's consensus regarding whether this should land on v24.x.

Why should restoring documented behaviour that's present on 22 and now 25 need wait to land on 24? I think baking-for-lts PRs that need to wait before landing in a LTS release. should be dropped here.

@panva
Copy link
Member

panva commented Oct 28, 2025

I'm removing baking-for-lts PRs that need to wait before landing in a LTS release. since the intent to restore the documented behaviour is already part of the blog post and will be updated in the release notes too.

@panva panva added lts-watch-v24.x PRs that may need to be released in v24.x and removed baking-for-lts PRs that need to wait before landing in a LTS release. labels Oct 28, 2025
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

author ready PRs that have at least one approval, no pending requests for changes, and a CI started. c++ Issues and PRs that require attention from people who are familiar with C++. commit-queue-rebase Add this label to allow the Commit Queue to land a PR in several commits. fast-track PRs that do not need to wait for 48 hours to land. lib / src Issues and PRs related to general changes in the lib or src directory. lts-watch-v24.x PRs that may need to be released in v24.x needs-ci PRs that need a full CI run.

Projects

None yet

Development

Successfully merging this pull request may close these issues.

allocUnsafe is useless since 24

9 participants