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
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)
@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.64%. Comparing base (bcdf2e0) to head (2757712).
⚠️ Report is 383 commits behind head on main.

Additional details and impacted files
@@            Coverage Diff             @@
##             main   #62032      +/-   ##
==========================================
+ Coverage   88.53%   89.64%   +1.11%     
==========================================
  Files         704      676      -28     
  Lines      208759   206227    -2532     
  Branches    40281    39515     -766     
==========================================
+ Hits       184816   184882      +66     
+ Misses      15947    13462    -2485     
+ Partials     7996     7883     -113     
Files with missing lines Coverage Δ
lib/buffer.js 99.15% <100.00%> (-0.85%) ⬇️

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

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

buffer Issues and PRs related to the buffer subsystem. 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.

5 participants