Skip to content

Commit e88373d

Browse files
committed
buffer: always use _copy for copy
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)
1 parent c9d0ef8 commit e88373d

File tree

1 file changed

+2
-6
lines changed

1 file changed

+2
-6
lines changed

lib/buffer.js

Lines changed: 2 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -268,7 +268,7 @@ function copyImpl(source, target, targetStart, sourceStart, sourceEnd) {
268268
return _copyActual(source, target, targetStart, sourceStart, sourceEnd);
269269
}
270270

271-
function _copyActual(source, target, targetStart, sourceStart, sourceEnd, isUint8Copy = false) {
271+
function _copyActual(source, target, targetStart, sourceStart, sourceEnd) {
272272
if (sourceEnd - sourceStart > target.byteLength - targetStart)
273273
sourceEnd = sourceStart + target.byteLength - targetStart;
274274

@@ -280,11 +280,7 @@ function _copyActual(source, target, targetStart, sourceStart, sourceEnd, isUint
280280
if (nb <= 0)
281281
return 0;
282282

283-
if (sourceStart === 0 && nb === sourceLen && (isUint8Copy || isUint8Array(target))) {
284-
TypedArrayPrototypeSet(target, source, targetStart);
285-
} else {
286-
_copy(source, target, targetStart, sourceStart, nb);
287-
}
283+
_copy(source, target, targetStart, sourceStart, nb);
288284

289285
return nb;
290286
}

0 commit comments

Comments
 (0)