Skip to content

Commit dea5dd7

Browse files
ri7116targos
authored andcommitted
dgram: restore buffer optimization in fixBufferList
Restore the Buffer.isBuffer() check to avoid unnecessary Buffer.from() calls when the input is already a Buffer. This improves performance by 30-50% for buffer-heavy UDP operations. Includes benchmark test for fixBufferList function to verify the performance improvements across different data types and chunk sizes. PR-URL: #59934 Reviewed-By: Matteo Collina <matteo.collina@gmail.com> Reviewed-By: Darshan Sen <raisinten@gmail.com>
1 parent 9347ddd commit dea5dd7

2 files changed

Lines changed: 54 additions & 0 deletions

File tree

‎benchmark/dgram/send-types.js‎

Lines changed: 52 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,52 @@
1+
'use strict';
2+
3+
constcommon=require('../common.js');
4+
constdgram=require('dgram');
5+
const{ Buffer }=require('buffer');
6+
7+
constbench=common.createBenchmark(main,{
8+
type: ['string','buffer','mixed','typedarray'],
9+
chunks: [1,4,8,16],
10+
len: [64,512,1024],
11+
n: [1000],
12+
});
13+
14+
functionmain({ type, chunks, len, n }){
15+
constsocket=dgram.createSocket('udp4');
16+
17+
lettestData;
18+
switch(type){
19+
case'string':
20+
testData=Array(chunks).fill('a'.repeat(len));
21+
break;
22+
case'buffer':
23+
testData=Array(chunks).fill(Buffer.alloc(len,'a'));
24+
break;
25+
case'mixed':
26+
testData=[];
27+
for(leti=0;i<chunks;i++){
28+
if(i%2===0){
29+
testData.push(Buffer.alloc(len,'a'));
30+
}else{
31+
testData.push('a'.repeat(len));
32+
}
33+
}
34+
break;
35+
case'typedarray':
36+
testData=Array(chunks).fill(newUint8Array(len).fill(97));
37+
break;
38+
}
39+
40+
bench.start();
41+
42+
for(leti=0;i<n;i++){
43+
socket.send(testData,12345,'localhost',(err)=>{
44+
if(err&&err.code!=='ENOTCONN'&&err.code!=='ECONNREFUSED'){
45+
throwerr;
46+
}
47+
});
48+
}
49+
50+
bench.end(n);
51+
socket.close();
52+
}

‎lib/dgram.js‎

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -550,6 +550,8 @@ function fixBufferList(list) {
550550
constbuf=list[i];
551551
if(typeofbuf==='string')
552552
newlist[i]=Buffer.from(buf);
553+
elseif(Buffer.isBuffer(buf))
554+
newlist[i]=buf;
553555
elseif(!isArrayBufferView(buf))
554556
returnnull;
555557
else

0 commit comments

Comments
 (0)