Skip to content

buffer: always use _copy for copy#62032

Open
ronag wants to merge 1 commit intonodejs:mainfrom
ronag:copyActual
Open

buffer: always use _copy for copy#62032
ronag wants to merge 1 commit intonodejs:mainfrom
ronag:copyActual

Conversation

@ronag
Copy link
Member

@ronag ronag commented Feb 27, 2026

This fixes a performance regression for Buffer.copy(target, 0) and brings it back inline with Buffer.write.

V8 has a massive TypedArray.prototype.set penalty on SharedArrayBuffer

Buffer.set and Buffer.copy are up to 8.4x slower when writing to a SharedArrayBuffer vs a regular ArrayBuffer, while Buffer.write (string encoding) is completely unaffected.

256 bytes, varying offset (Apple M3 Pro, Node 25.6.1):
                  ArrayBuffer    SharedArrayBuffer    Slowdown
Buffer.set           13.6 ns             56.1 ns       4.1x
Buffer.copy          17.0 ns             65.1 ns       3.8x
Buffer.write         75.8 ns             74.1 ns       1.0x (unaffected)

4096 bytes, varying offset:
                  ArrayBuffer    SharedArrayBuffer    Slowdown
Buffer.set           80.3 ns            674.2 ns       8.4x
Buffer.copy          78.4 ns            677.7 ns       8.6x
Buffer.write        190.6 ns            186.1 ns       1.0x (unaffected)

@nodejs-github-bot nodejs-github-bot added buffer Issues and PRs related to the buffer subsystem. needs-ci PRs that need a full CI run. labels Feb 27, 2026
@ronag ronag changed the title buffer: always us _copy for copy buffer: always use _copy for copy Feb 27, 2026
@Renegade334
Copy link
Member

If the figures from #60399 still apply for ArrayBuffer-backed buffers, is there scope to add isArrayBuffer(TypedArrayPrototypeGetBuffer(target)) to the previous conditions, and get the best of both worlds?

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.

lgtm

@ronag
Copy link
Member Author

ronag commented Feb 27, 2026

If the figures from #60399 still apply for ArrayBuffer-backed buffers, is there scope to add isArrayBuffer(TypedArrayPrototypeGetBuffer(target)) to the previous conditions, and get the best of both worlds?

doing all of these type checks also adds overhead. Furthermore the benchmark from that PR apply to concat so it's unclear what it's actual effect on Buffer.copy is...

@ronag ronag added the request-ci Add this label to start a Jenkins CI on a PR. label Feb 27, 2026
@RafaelGSS RafaelGSS added the performance Issues and PRs related to the performance of Node.js. label Feb 27, 2026
@github-actions github-actions bot removed the request-ci Add this label to start a Jenkins CI on a PR. label Feb 27, 2026
@nodejs-github-bot
Copy link
Collaborator

@codecov
Copy link

codecov bot commented Feb 27, 2026

Codecov Report

✅ All modified and coverable lines are covered by tests.
✅ Project coverage is 89.66%. Comparing base (35d3bc8) to head (e88373d).
⚠️ Report is 5 commits behind head on main.

Additional details and impacted files
@@           Coverage Diff           @@
##             main   #62032   +/-   ##
=======================================
  Coverage   89.65%   89.66%           
=======================================
  Files         676      676           
  Lines      206231   206227    -4     
  Branches    39505    39504    -1     
=======================================
+ Hits       184898   184905    +7     
+ Misses      13463    13454    -9     
+ Partials     7870     7868    -2     
Files with missing lines Coverage Δ
lib/buffer.js 99.15% <100.00%> (-0.01%) ⬇️

... and 24 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

@Renegade334 Renegade334 left a comment

Choose a reason for hiding this comment

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

Think it's worth mentioning in the commit message that this is essentially a reversion of 24bebd0, sans tests.

This fixes a performance regression for Buffer.copy(target, 0) and brings it back inline with Buffer.write.

V8 has a massive TypedArray.prototype.set penalty on SharedArrayBuffer

Buffer.set and Buffer.copy are up to 8.4x slower when writing to a SharedArrayBuffer vs a regular ArrayBuffer, while Buffer.write (string encoding) is completely unaffected.

256 bytes, varying offset (Apple M3 Pro, Node 25.6.1):

                  ArrayBuffer    SharedArrayBuffer    Slowdown
Buffer.set           13.6 ns             56.1 ns       4.1x
Buffer.copy          17.0 ns             65.1 ns       3.8x
Buffer.write         75.8 ns             74.1 ns       1.0x (unaffected)
4096 bytes, varying offset:

                  ArrayBuffer    SharedArrayBuffer    Slowdown
Buffer.set           80.3 ns            674.2 ns       8.4x
Buffer.copy          78.4 ns            677.7 ns       8.6x
Buffer.write        190.6 ns            186.1 ns       1.0x (unaffected)
@ronag ronag requested a review from RafaelGSS February 28, 2026 08:11
@ronag ronag added the request-ci Add this label to start a Jenkins CI on a PR. label Feb 28, 2026
@github-actions github-actions bot added request-ci-failed An error occurred while starting CI via request-ci label, and manual interventon is needed. and removed request-ci Add this label to start a Jenkins CI on a PR. labels Feb 28, 2026
@github-actions
Copy link
Contributor

Failed to start CI
   ⚠  Commits were pushed since the last approving review:
   ⚠  - buffer: always use _copy for copy
   ✘  Refusing to run CI on potentially unsafe PR
https://github.com/nodejs/node/actions/runs/22516891493

@ronag ronag added the author ready PRs that have at least one approval, no pending requests for changes, and a CI started. label Feb 28, 2026
@richardlau richardlau removed the request-ci-failed An error occurred while starting CI via request-ci label, and manual interventon is needed. label Feb 28, 2026
@richardlau richardlau added the request-ci Add this label to start a Jenkins CI on a PR. label Feb 28, 2026
@github-actions github-actions bot removed the request-ci Add this label to start a Jenkins CI on a PR. label Feb 28, 2026
@nodejs-github-bot
Copy link
Collaborator

@gurgunday gurgunday added the needs-benchmark-ci PR that need a benchmark CI run. label Feb 28, 2026
Copy link
Member

@gurgunday gurgunday left a comment

Choose a reason for hiding this comment

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

I am seeing a significant regression in non-partial copy performance:

node % ./node benchmark/buffers/buffer-copy.js
buffers/buffer-copy.js n=6000000 partial="true" bytes=8: 37,745,651.53108819
buffers/buffer-copy.js n=6000000 partial="false" bytes=8: 72,230,886.31703596
buffers/buffer-copy.js n=6000000 partial="true" bytes=128: 37,269,250.926878504
buffers/buffer-copy.js n=6000000 partial="false" bytes=128: 70,233,110.24800341
buffers/buffer-copy.js n=6000000 partial="true" bytes=1024: 26,739,253.757964805
buffers/buffer-copy.js n=6000000 partial="false" bytes=1024: 37,699,305.6259145

node % ./node-ronag benchmark/buffers/buffer-copy.js
buffers/buffer-copy.js n=6000000 partial="true" bytes=8: 36,820,630.5993248
buffers/buffer-copy.js n=6000000 partial="false" bytes=8: 37,401,995.084754474
buffers/buffer-copy.js n=6000000 partial="true" bytes=128: 37,360,956.762793645
buffers/buffer-copy.js n=6000000 partial="false" bytes=128: 35,075,651.428067446
buffers/buffer-copy.js n=6000000 partial="true" bytes=1024: 26,155,344.217952482
buffers/buffer-copy.js n=6000000 partial="false" bytes=1024: 22,480,048.984626204

Those checks are necessary to hit the V8 .set optimization, it is faster than our native copy, I think we should instead make those checks more strict if you found a case that's slower, as @Renegade334 suggested

You are right that .concat no longer hits that path, but copy still does

Cc @ChALkeR

@aduh95
Copy link
Contributor

aduh95 commented Feb 28, 2026

@ChALkeR
Copy link
Member

ChALkeR commented Feb 28, 2026

Let's hold off until we figure out what's going on

@ronag Which benchmark is that in the PR description?

If it's not in tree, could you add it (or modify buffer-copy.js) to test behavior on:

  • Buffer.alloc(x, 1)
  • Buffer.from(new ArrayBuffer(x)).fill(1)
  • Buffer.allocUnsafeSlow(x).fill(1)
  • Buffer.from(new SharedArrayBuffer(x)).fill(1)

All of those are important / could give different results

This should be tested with benchmarks code

Also the bech should have 64/128 bytes in sizes (along others lower/higher) as there is a sharp difference there in how the data is stored in some cases.

@ChALkeR
Copy link
Member

ChALkeR commented Feb 28, 2026

Also i'm curious why is v8 slower on SAB there
Are we sure that our _copy impl does not lack some logic around that / works correctly on SAB?

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. buffer Issues and PRs related to the buffer subsystem. needs-benchmark-ci PR that need a benchmark CI run. needs-ci PRs that need a full CI run. performance Issues and PRs related to the performance of Node.js.

Projects

None yet

Development

Successfully merging this pull request may close these issues.

10 participants