buffer: avoid unnecessary copy in Buffer.concat for single FastBuffer#58266
buffer: avoid unnecessary copy in Buffer.concat for single FastBuffer#58266mertcanaltin wants to merge 3 commits intonodejs:mainfrom
Conversation
Codecov ReportAll modified and coverable lines are covered by tests ✅
Additional details and impacted files@@ Coverage Diff @@
## main #58266 +/- ##
=======================================
Coverage 90.18% 90.18%
=======================================
Files 629 629
Lines 186657 186640 -17
Branches 36658 36669 +11
=======================================
- Hits 168328 168324 -4
- Misses 11126 11128 +2
+ Partials 7203 7188 -15
🚀 New features to boost your workflow:
|
| list[0] instanceof FastBuffer && | ||
| list[0].length === length | ||
| ) { | ||
| return Buffer.from(list[0]); |
There was a problem hiding this comment.
If list[0] is a TypedArray (like another Buffer) then this still copies.
There was a problem hiding this comment.
I’m wondering if it makes sense to return list[0] directly. I understand this avoids copying, so I guess I should update the test accordingly?
https://github.com/nodejs/node/blob/main/test/parallel/test-buffer-concat.js#L43
There was a problem hiding this comment.
Buffer.concat should always return a new buffer object:
Lines 1033 to 1034 in 91d2400
There was a problem hiding this comment.
As I understand it, at the end of the day we should always create a buffer.
There was a problem hiding this comment.
I will try an optimization in the inner regions, I am now closing this pr, thank you very much for your comments
If there's only one FastBuffer and its length matches the total length, we don't need to manually allocate and copy — we can just return Buffer.from(buf) to keep the semantics (new buffer) but avoid extra work.
my local result: