-
-
Notifications
You must be signed in to change notification settings - Fork 34.7k
buffer: optimize buffer.concat performance #61721
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
base: main
Are you sure you want to change the base?
Changes from all commits
7ded458
a7fd75d
885c7a3
ba806ae
559f5e9
1898304
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -614,25 +614,55 @@ Buffer.concat = function concat(list, length) { | |
| if (length === undefined) { | ||
| length = 0; | ||
| for (let i = 0; i < list.length; i++) { | ||
| if (list[i].length) { | ||
| length += list[i].length; | ||
| const buf = list[i]; | ||
| if (!isUint8Array(buf)) { | ||
| // TODO(BridgeAR): This should not be of type ERR_INVALID_ARG_TYPE. | ||
| // Instead, find the proper error code for this. | ||
| throw new ERR_INVALID_ARG_TYPE( | ||
| `list[${i}]`, ['Buffer', 'Uint8Array'], buf); | ||
| } | ||
| length += TypedArrayPrototypeGetByteLength(buf); | ||
| } | ||
| } else { | ||
| validateOffset(length, 'length'); | ||
|
|
||
| const buffer = allocate(length); | ||
| let pos = 0; | ||
| for (let i = 0; i < list.length; i++) { | ||
| const buf = list[i]; | ||
| const bufLength = TypedArrayPrototypeGetByteLength(buf); | ||
| TypedArrayPrototypeSet(buffer, buf, pos); | ||
| pos += bufLength; | ||
| } | ||
|
|
||
| if (pos < length) { | ||
| TypedArrayPrototypeFill(buffer, 0, pos, length); | ||
| } | ||
|
Comment on lines
+636
to
+638
Member
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Can we even hit this part? I think this is unreachable because you moved type validation to the beginning and it will always fill after going through all of them?
Member
Author
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. It's still reachable if a buffer gets detached between the validation and copy loops buf.length becomes 0, pos won't advance, leaving uninitialized bytes without the zero-fill.
Member
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Not really sure how it can be detached unless a length getter is compromised. It's all synchronous code here? In any case, it would be nice to have coverage for it Other than coverage, the PR LGTM
Member
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. How about an assert here? (Attempting to read from a detached buffer should throw an error from the engine in any case.)
Member
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. If the buffer gets detached between these points should actually cause the Consider the example: const u8_1 = new Uint8Array([1,2,3,4]);
const u8_2 = new Uint8Array([5,6,7,8]);
let called = false;
Object.defineProperty(u8_1, 'length', {
get() {
// The first time this is called, return the actual length of the array
// The second time this is called, we'll also transfer the ArrayBuffer of the second
if (!called) {
called = true;
} else {
u8_2.buffer.transfer();
}
return 4;
},
});
const buf = Buffer.concat([u8_1, u8_2]);
console.log(buf);I'm all for being defensive here tho.
Member
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I think the bigger challenge here is that this does introduce a breaking security risk. Consider the following case: Then comparing the output between current node.js and this PR:
Member
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more.
Member
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Probably. Also just make sure that
Member
Author
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Thanks, switched to TypedArrayPrototypeGetByteLength to avoid the spoofed length getter. Zero-fill kept as defensive measure.
Member
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I would be sure to also add a test that covers the spoofed length test.
Comment on lines
+636
to
+638
Member
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Nit: my preference would still be for an assert here, since |
||
| return buffer; | ||
| } | ||
|
|
||
| const buffer = Buffer.allocUnsafe(length); | ||
| let pos = 0; | ||
| validateOffset(length, 'length'); | ||
| for (let i = 0; i < list.length; i++) { | ||
| const buf = list[i]; | ||
| if (!isUint8Array(buf)) { | ||
| if (!isUint8Array(list[i])) { | ||
| // TODO(BridgeAR): This should not be of type ERR_INVALID_ARG_TYPE. | ||
| // Instead, find the proper error code for this. | ||
| throw new ERR_INVALID_ARG_TYPE( | ||
| `list[${i}]`, ['Buffer', 'Uint8Array'], list[i]); | ||
| } | ||
| pos += _copyActual(buf, buffer, pos, 0, buf.length, true); | ||
| } | ||
|
|
||
| const buffer = allocate(length); | ||
| let pos = 0; | ||
| for (let i = 0; i < list.length; i++) { | ||
| const buf = list[i]; | ||
| const bufLength = TypedArrayPrototypeGetByteLength(buf); | ||
| if (pos + bufLength > length) { | ||
| TypedArrayPrototypeSet(buffer, | ||
| TypedArrayPrototypeSlice(buf, 0, length - pos), | ||
|
Member
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Nit: |
||
| pos); | ||
| pos = length; | ||
| break; | ||
| } | ||
| TypedArrayPrototypeSet(buffer, buf, pos); | ||
| pos += bufLength; | ||
| } | ||
|
|
||
| // Note: `length` is always equal to `buffer.length` at this point | ||
|
|
||
Uh oh!
There was an error while loading. Please reload this page.