buffer: improve concat() performance

PR-URL: https://github.com/nodejs/node/pull/31522
Reviewed-By: Anna Henningsen <anna@addaleax.net>
Reviewed-By: James M Snell <jasnell@gmail.com>
Reviewed-By: Rich Trott <rtrott@gmail.com>
This commit is contained in:
Brian White authored and Rich Trott committed 2020-01-28 19:57:20 -08:00
1 parent 32f63fcf0e
commit 2170259940
3 files changed
+30 -3

No files matched your search

+6 -3
View File
@@ -238,6 +238,10 @@ function _copy(source, target, targetStart, sourceStart, sourceEnd) {
sourceStart);
}
return _copyActual(source, target, targetStart, sourceStart, sourceEnd);
}
function _copyActual(source, target, targetStart, sourceStart, sourceEnd) {
if (sourceEnd - sourceStart > target.length - targetStart)
sourceEnd = sourceStart + target.length - targetStart;
@@ -568,8 +572,7 @@ Buffer.concat = function concat(list, length) {
throw new ERR_INVALID_ARG_TYPE(
`list[${i}]`, ['Buffer', 'Uint8Array'], list[i]);
}
_copy(buf, buffer, pos);
pos += buf.length;
pos += _copyActual(buf, buffer, pos, 0, buf.length);
}
// Note: `length` is always equal to `buffer.length` at this point
@@ -577,7 +580,7 @@ Buffer.concat = function concat(list, length) {
// Zero-fill the remaining bytes if the specified `length` was more than
// the actual total length, i.e. if we have some remaining allocated bytes
// there were not initialized.
buffer.fill(0, pos, length);
TypedArrayFill.call(buffer, 0, pos, length);
}
return buffer;