← Back to Pull requests

buffer: simplify Buffer.concat #64836

Open
gibson042 opened this pull request with 85 lines diff across 1 files
View on GitHub
gibson042 gibson042 commented on Jul 30, 2026 07:08 UTC
Author

Merge the auto-length and explicit-length paths of Buffer.concat while maintaining the branch-free status of the copy loop body from #61721 by discovering the the index of the first not-fully-copyable input inside the validation loop.

Also improve efficiency by relying upon TypedArrayPrototypeGetByteLength for input validation and capturing positions from it.

The end result is more unified code whose performance seems to be roughly equivalent and perhaps slightly skewed in favor of unspecified length:

$ node-benchmark-compare <(
  node benchmark/compare.js --old ./node-upstream --new ./out/Release/node --filter buffer-concat buffers
)
                                                                            confidence improvement accuracy (*)   (**)  (***)
buffers/buffer-concat-fill.js n=800000 extraSize=1                                         -2.84 %       ±2.91% ±3.87% ±5.03%
buffers/buffer-concat-fill.js n=800000 extraSize=1024                                *      3.27 %       ±3.06% ±4.08%  ±5.31%
buffers/buffer-concat-fill.js n=800000 extraSize=256                                       -1.75 %       ±1.94% ±2.59% ±3.37%
buffers/buffer-concat.js n=800000 withTotalLength=0 pieceSize=1 pieces=16          ***      7.32 %       ±1.51% ±2.03% ±2.69%
buffers/buffer-concat.js n=800000 withTotalLength=0 pieceSize=1 pieces=4                    1.71 %       ±3.48% ±4.68% ±6.18%
buffers/buffer-concat.js n=800000 withTotalLength=0 pieceSize=16 pieces=16         ***      6.54 %       ±0.56% ±0.75% ±0.98%
buffers/buffer-concat.js n=800000 withTotalLength=0 pieceSize=16 pieces=4          ***      6.03 %       ±1.11% ±1.48% ±1.95%
buffers/buffer-concat.js n=800000 withTotalLength=0 pieceSize=256 pieces=16                -2.18 %       ±2.93% ±3.90% ±5.08%
buffers/buffer-concat.js n=800000 withTotalLength=0 pieceSize=256 pieces=4         ***     11.32 %       ±3.71% ±4.98% ±6.58%
buffers/buffer-concat.js n=800000 withTotalLength=1 pieceSize=1 pieces=16          ***     -5.06 %       ±0.65% ±0.87% ±1.14%
buffers/buffer-concat.js n=800000 withTotalLength=1 pieceSize=1 pieces=4           ***     -7.62 %       ±1.33% ±1.77%  ±2.31%
buffers/buffer-concat.js n=800000 withTotalLength=1 pieceSize=16 pieces=16         ***     -5.84 %       ±0.70% ±0.93% ±1.22%
buffers/buffer-concat.js n=800000 withTotalLength=1 pieceSize=16 pieces=4          ***     -5.99 %       ±0.94% ±1.26% ±1.64%
buffers/buffer-concat.js n=800000 withTotalLength=1 pieceSize=256 pieces=16        ***     -8.78 %       ±2.66% ±3.55% ±4.64%
buffers/buffer-concat.js n=800000 withTotalLength=1 pieceSize=256 pieces=4         ***     -4.70 %       ±1.67% ±2.22% ±2.90%

Be aware that when doing many comparisons the risk of a false-positive result increases.
In this case, there are 15 comparisons, you can thus expect the following amount of false-positive results:
  0.75 false positives, when considering a   5% risk acceptance (*, **, ***),
  0.15 false positives, when considering a   1% risk acceptance (**, ***),
  0.01 false positives, when considering a 0.1% risk acceptance (***)

Diff: 85 lines (+35 / -50) across 1 files Reviewer: vedchaudhari Review duration: 183048m
All Vouch review confidence checks passed
Residual risk 0.04 = Change Risk (0.37) × (1 − Review Confidence (0.90)). Review depth and reviewer attention state are adequate.
Model 1 · Change Risk (XGBoost)
Score: 0.37 · 85 lines diff across 1 files
0.37
Model 2 · Review Depth (DistilBERT)
Depth Score: 0.96 · 1 substantive comment(s)
0.96
Model 3 · Review Timing & Attention
Session adequacy: 100% · Reviewer familiarity: 35%
89%
Bedrock Claude 3 · Risk Narration
(disconnected)
Groq
Groq Risk Narration
AWS Bedrock is currently disconnected. Generate high-confidence PR triage narration on-demand using Groq.
Model 1 · Change Risk Attribution
SHAP Weights
Diff Size diff_size
0.266
Real PR Conversation Timeline
3 items
vedchaudhari vedchaudhari commented on code on Nov 25, 2026 04:15 UTC
Code Review
lib/buffer.js:1

I think dropping the isUint8Array() check lets any TypedArray through now, since TypedArrayPrototypeGetByteLength() doesn't throw for them. And the result can end up with uninitialized bytes:

Buffer.concat([new Uint16Array([1, 2, 3, 4])]);
// main: throws ERR_INVALID_ARG_TYPE
// here: 8-byte buffer, but only the first 4 bytes get written

The length is counted in bytes (8), but TypedArrayPrototypeSet() copies per element (4 values), and allocate() doesn't zero memory, so the last 4 bytes are whatever was in the pool. I tried it with a bigger input (new Uint16Array(4096)) and the unwritten half sometimes had leftover heap data. BigInt64Array also changes, from ERR_INVALID_ARG_TYPE to a plain TypeError: Cannot mix BigInt....

Keeping the isUint8Array() check in this loop should fix it. A test with something like a Uint16Array in test-buffer-concat.js would be nice too, since only non-TypedArray inputs are checked there right now.

vedchaudhari vedchaudhari commented on Nov 25, 2026 04:17 UTC
Reviewer

Nice cleanup! The Uint8Array cases behave the same as main in my testing (with and without length). Left one comment about other TypedArrays slipping through.

Reviewers
Assignees
gibson042 gibson042
Labels
risk: low confidence: 89.9% reviewed
Vouch Risk Breakdown
AI-powered review risk analysis
Change Risk
Change Risk · Model 1 (XGBoost)
Predicted defect probability (0.00–1.00) based on diff churn, modified sensitive files, and author risk history.
Score: 36.6% (MEDIUM) · Higher = larger blast radius
36.6%
Review Confidence
Review Confidence · Models 2 & 3
Scrutiny score (0.00–1.00) combining DistilBERT semantic review depth with reviewer familiarity and session dwell time.
Score: 89.9% · Lower = rushed or shallow review
89.9%
Residual Risk
Residual Risk · Core Equation
Residual = Change × (1 − Confidence)
Unmitigated risk surviving review. ≥ 0.65 triggers an automated re-queue to protect production.
Score: 3.7% · Safe to merge
3.7%
Repository