Skip to content

Commit 2271d2d

Browse files
authored
lib: use FastBuffer for empty buffer allocation
PR-URL: #60558 Reviewed-By: Luigi Pinca <[email protected]> Reviewed-By: Juan José Arboleda <[email protected]>
1 parent 735ee27 commit 2271d2d

File tree

6 files changed

+15
-11
lines changed

6 files changed

+15
-11
lines changed

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
const viewLength = TypedArrayPrototypeGetLength(view);
354354
if (viewLength === 0) {
355-
return Buffer.alloc(0);
355+
return new FastBuffer();
356356
}
357357

358358
if (offset !== undefined || length !== undefined) {
359359
if (offset !== undefined) {
360360
validateInteger(offset, 'offset', 0);
361-
if (offset >= viewLength) return Buffer.alloc(0);
361+
if (offset >= viewLength) return new FastBuffer();
362362
} else {
363363
offset = 0;
364364
}
@@ -1262,7 +1262,7 @@ if (internalBinding('config').hasIntl) {
12621262
throw new ERR_INVALID_ARG_TYPE('source',
12631263
['Buffer', 'Uint8Array'], source);
12641264
}
1265-
if (source.length === 0) return Buffer.alloc(0);
1265+
if (source.length === 0) return new FastBuffer();
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, new FastBuffer());
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
const http = require('http');
1818
const { URL } = require('internal/url');
19+
const { FastBuffer } = require('internal/buffer');
1920

2021
const debuglog = 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 = new FastBuffer();
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 = new FastBuffer();
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
} else if (len - idx === 0) {
1608-
ret = Buffer.alloc(0);
1608+
ret = new FastBuffer();
16091609
} else if (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
let bufferHead = this.#rawBuffer[0];
326327
let headerIndex = bufferHead.indexOf(v8Header);
327-
let nonSerialized = Buffer.alloc(0);
328+
let nonSerialized = new FastBuffer();
328329

329330
while (bufferHead && headerIndex !== 0) {
330331
const nonSerializedData = 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
const kFlushFlag = Symbol('kFlushFlag');
7576
const kError = Symbol('kError');
@@ -150,7 +151,7 @@ function zlibBufferOnError(err) {
150151
function zlibBufferOnEnd() {
151152
let buf;
152153
if (this.nread === 0) {
153-
buf = Buffer.alloc(0);
154+
buf = new FastBuffer();
154155
} else {
155156
const bufs = 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(new FastBuffer(), '', callback);
305306
};
306307

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

478479
if (nread === 0)
479-
return Buffer.alloc(0);
480+
return new FastBuffer();
480481

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

0 commit comments

Comments
 (0)