Skip to content

Commit 1a0c3dd

Browse files
gurgundayRafaelGSS
authored andcommitted
lib: use FastBuffer for empty buffer allocation
PR-URL: #60558 Reviewed-By: Luigi Pinca <luigipinca@gmail.com> Reviewed-By: Juan José Arboleda <soyjuanarbol@gmail.com>
1 parent d90001a commit 1a0c3dd

6 files changed

Lines changed: 15 additions & 11 deletions

File tree

‎lib/buffer.js‎

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -352,13 +352,13 @@ Buffer.copyBytesFrom = function copyBytesFrom(view, offset, length) {
352352

353353
constviewLength=TypedArrayPrototypeGetLength(view);
354354
if(viewLength===0){
355-
returnBuffer.alloc(0);
355+
returnnewFastBuffer();
356356
}
357357

358358
if(offset!==undefined||length!==undefined){
359359
if(offset!==undefined){
360360
validateInteger(offset,'offset',0);
361-
if(offset>=viewLength)returnBuffer.alloc(0);
361+
if(offset>=viewLength)returnnewFastBuffer();
362362
}else{
363363
offset=0;
364364
}
@@ -1262,7 +1262,7 @@ if (internalBinding('config').hasIntl) {
12621262
thrownewERR_INVALID_ARG_TYPE('source',
12631263
['Buffer','Uint8Array'],source);
12641264
}
1265-
if(source.length===0)returnBuffer.alloc(0);
1265+
if(source.length===0)returnnewFastBuffer();
12661266

12671267
fromEncoding=normalizeEncoding(fromEncoding)||fromEncoding;
12681268
toEncoding=normalizeEncoding(toEncoding)||toEncoding;

‎lib/dgram.js‎

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -73,6 +73,7 @@ const {
7373
defaultTriggerAsyncIdScope,
7474
symbols: { async_id_symbol, owner_symbol },
7575
}=require('internal/async_hooks');
76+
const{ FastBuffer }=require('internal/buffer');
7677
const{UV_UDP_REUSEADDR}=internalBinding('constants').os;
7778

7879
const{
@@ -688,7 +689,7 @@ Socket.prototype.send = function(buffer,
688689
this.bind({port: 0,exclusive: true},null);
689690

690691
if(list.length===0)
691-
ArrayPrototypePush(list,Buffer.alloc(0));
692+
ArrayPrototypePush(list,newFastBuffer());
692693

693694
// If the socket hasn't been bound yet, push the outbound packet onto the
694695
// send queue and send after binding is complete.

‎lib/internal/debugger/inspect_client.js‎

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,7 @@ const { ERR_DEBUGGER_ERROR } = require('internal/errors').codes;
1616
const{ EventEmitter, once }=require('events');
1717
consthttp=require('http');
1818
const{URL}=require('internal/url');
19+
const{ FastBuffer }=require('internal/buffer');
1920

2021
constdebuglog=require('internal/util/debuglog').debuglog('inspect');
2122

@@ -79,7 +80,7 @@ function encodeFrameHybi17(payload) {
7980
additionalLength[0]=(dataLength&0xFF00)>>8;
8081
additionalLength[1]=dataLength&0xFF;
8182
}else{
82-
additionalLength=Buffer.alloc(0);
83+
additionalLength=newFastBuffer();
8384
singleByteLength=dataLength;
8485
}
8586

@@ -233,7 +234,7 @@ class Client extends EventEmitter {
233234
this._lastId=0;
234235
this._socket=null;
235236
this._pending={};
236-
this._unprocessed=Buffer.alloc(0);
237+
this._unprocessed=newFastBuffer();
237238
}
238239

239240
callMethod(method,params){

‎lib/internal/streams/readable.js‎

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1605,7 +1605,7 @@ function fromList(n, state) {
16051605
buf[idx++]=null;
16061606
}
16071607
}elseif(len-idx===0){
1608-
ret=Buffer.alloc(0);
1608+
ret=newFastBuffer();
16091609
}elseif(len-idx===1){
16101610
ret=buf[idx];
16111611
buf[idx++]=null;

‎lib/internal/test_runner/runner.js‎

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -78,6 +78,7 @@ const {
7878
kTestTimeoutFailure,
7979
Test,
8080
}=require('internal/test_runner/test');
81+
const{ FastBuffer }=require('internal/buffer');
8182

8283
const{
8384
convertStringToRegExp,
@@ -324,7 +325,7 @@ class FileTest extends Test {
324325
// This method is called when it is known that there is at least one message
325326
letbufferHead=this.#rawBuffer[0];
326327
letheaderIndex=bufferHead.indexOf(v8Header);
327-
letnonSerialized=Buffer.alloc(0);
328+
letnonSerialized=newFastBuffer();
328329

329330
while(bufferHead&&headerIndex!==0){
330331
constnonSerializedData=headerIndex===-1 ?

‎lib/zlib.js‎

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -70,6 +70,7 @@ const {
7070
validateUint32,
7171
validateFiniteNumber,
7272
}=require('internal/validators');
73+
const{ FastBuffer }=require('internal/buffer');
7374

7475
constkFlushFlag=Symbol('kFlushFlag');
7576
constkError=Symbol('kError');
@@ -150,7 +151,7 @@ function zlibBufferOnError(err) {
150151
functionzlibBufferOnEnd(){
151152
letbuf;
152153
if(this.nread===0){
153-
buf=Buffer.alloc(0);
154+
buf=newFastBuffer();
154155
}else{
155156
constbufs=this.buffers;
156157
buf=(bufs.length===1 ? bufs[0] : Buffer.concat(bufs,this.nread));
@@ -301,7 +302,7 @@ ZlibBase.prototype.reset = function() {
301302
* @returns {void}
302303
*/
303304
ZlibBase.prototype._flush=function(callback){
304-
this._transform(Buffer.alloc(0),'',callback);
305+
this._transform(newFastBuffer(),'',callback);
305306
};
306307

307308
/**
@@ -476,7 +477,7 @@ function processChunkSync(self, chunk, flushFlag) {
476477
_close(self);
477478

478479
if(nread===0)
479-
returnBuffer.alloc(0);
480+
returnnewFastBuffer();
480481

481482
return(buffers.length===1 ? buffers[0] : Buffer.concat(buffers,nread));
482483
}

0 commit comments

Comments
 (0)