Skip to content

Commit 22a37d6

Browse files
mscdexcodebytere
authored andcommitted
buffer: improve concat() performance
PR-URL: #31522 Reviewed-By: Anna Henningsen <anna@addaleax.net> Reviewed-By: James M Snell <jasnell@gmail.com> Reviewed-By: Rich Trott <rtrott@gmail.com>
1 parent 91dd101 commit 22a37d6

File tree

3 files changed

+30
-3
lines changed

3 files changed

+30
-3
lines changed
+23
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,23 @@
1+
'use strict';
2+
const common = require('../common.js');
3+
4+
const bench = common.createBenchmark(main, {
5+
extraSize: [1, 256, 4 * 256],
6+
n: [8e5]
7+
});
8+
9+
function main({ n, extraSize }) {
10+
const pieces = 4;
11+
const pieceSize = 256;
12+
13+
const list = Array.from({ length: pieces })
14+
.fill(Buffer.allocUnsafe(pieceSize));
15+
16+
const totalLength = (pieces * pieceSize) + extraSize;
17+
18+
bench.start();
19+
for (let i = 0; i < n; i++) {
20+
Buffer.concat(list, totalLength);
21+
}
22+
bench.end(n);
23+
}

lib/buffer.js

+6-3
Original file line numberDiff line numberDiff line change
@@ -238,6 +238,10 @@ function _copy(source, target, targetStart, sourceStart, sourceEnd) {
238238
sourceStart);
239239
}
240240

241+
return _copyActual(source, target, targetStart, sourceStart, sourceEnd);
242+
}
243+
244+
function _copyActual(source, target, targetStart, sourceStart, sourceEnd) {
241245
if (sourceEnd - sourceStart > target.length - targetStart)
242246
sourceEnd = sourceStart + target.length - targetStart;
243247

@@ -568,16 +572,15 @@ Buffer.concat = function concat(list, length) {
568572
throw new ERR_INVALID_ARG_TYPE(
569573
`list[${i}]`, ['Buffer', 'Uint8Array'], list[i]);
570574
}
571-
_copy(buf, buffer, pos);
572-
pos += buf.length;
575+
pos += _copyActual(buf, buffer, pos, 0, buf.length);
573576
}
574577

575578
// Note: `length` is always equal to `buffer.length` at this point
576579
if (pos < length) {
577580
// Zero-fill the remaining bytes if the specified `length` was more than
578581
// the actual total length, i.e. if we have some remaining allocated bytes
579582
// there were not initialized.
580-
buffer.fill(0, pos, length);
583+
TypedArrayFill.call(buffer, 0, pos, length);
581584
}
582585

583586
return buffer;

test/benchmark/test-benchmark-buffer.js

+1
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,7 @@ runBenchmark('buffers',
1515
'difflen=false',
1616
'encoding=utf8',
1717
'endian=BE',
18+
'extraSize=1',
1819
'len=256',
1920
'linesCount=1',
2021
'method=',

0 commit comments

Comments
 (0)