lib: use FastBuffer for empty buffer allocation

PR-URL: https://github.com/nodejs/node/pull/60558
Reviewed-By: Luigi Pinca <luigipinca@gmail.com>
Reviewed-By: Juan José Arboleda <soyjuanarbol@gmail.com>
This commit is contained in:
Gürgün Dayıoğlu
2025-11-15 21:39:32 +01:00
committed by GitHub
parent 735ee27aa6
commit 2271d2dc10
6 changed files with 15 additions and 11 deletions

View File

@@ -352,13 +352,13 @@ Buffer.copyBytesFrom = function copyBytesFrom(view, offset, length) {
const viewLength = TypedArrayPrototypeGetLength(view);
if (viewLength === 0) {
return Buffer.alloc(0);
return new FastBuffer();
}
if (offset !== undefined || length !== undefined) {
if (offset !== undefined) {
validateInteger(offset, 'offset', 0);
if (offset >= viewLength) return Buffer.alloc(0);
if (offset >= viewLength) return new FastBuffer();
} else {
offset = 0;
}
@@ -1262,7 +1262,7 @@ if (internalBinding('config').hasIntl) {
throw new ERR_INVALID_ARG_TYPE('source',
['Buffer', 'Uint8Array'], source);
}
if (source.length === 0) return Buffer.alloc(0);
if (source.length === 0) return new FastBuffer();
fromEncoding = normalizeEncoding(fromEncoding) || fromEncoding;
toEncoding = normalizeEncoding(toEncoding) || toEncoding;

View File

@@ -73,6 +73,7 @@ const {
defaultTriggerAsyncIdScope,
symbols: { async_id_symbol, owner_symbol },
} = require('internal/async_hooks');
const { FastBuffer } = require('internal/buffer');
const { UV_UDP_REUSEADDR } = internalBinding('constants').os;
const {
@@ -688,7 +689,7 @@ Socket.prototype.send = function(buffer,
this.bind({ port: 0, exclusive: true }, null);
if (list.length === 0)
ArrayPrototypePush(list, Buffer.alloc(0));
ArrayPrototypePush(list, new FastBuffer());
// If the socket hasn't been bound yet, push the outbound packet onto the
// send queue and send after binding is complete.

View File

@@ -16,6 +16,7 @@ const { ERR_DEBUGGER_ERROR } = require('internal/errors').codes;
const { EventEmitter, once } = require('events');
const http = require('http');
const { URL } = require('internal/url');
const { FastBuffer } = require('internal/buffer');
const debuglog = require('internal/util/debuglog').debuglog('inspect');
@@ -79,7 +80,7 @@ function encodeFrameHybi17(payload) {
additionalLength[0] = (dataLength & 0xFF00) >> 8;
additionalLength[1] = dataLength & 0xFF;
} else {
additionalLength = Buffer.alloc(0);
additionalLength = new FastBuffer();
singleByteLength = dataLength;
}
@@ -233,7 +234,7 @@ class Client extends EventEmitter {
this._lastId = 0;
this._socket = null;
this._pending = {};
this._unprocessed = Buffer.alloc(0);
this._unprocessed = new FastBuffer();
}
callMethod(method, params) {

View File

@@ -1605,7 +1605,7 @@ function fromList(n, state) {
buf[idx++] = null;
}
} else if (len - idx === 0) {
ret = Buffer.alloc(0);
ret = new FastBuffer();
} else if (len - idx === 1) {
ret = buf[idx];
buf[idx++] = null;

View File

@@ -78,6 +78,7 @@ const {
kTestTimeoutFailure,
Test,
} = require('internal/test_runner/test');
const { FastBuffer } = require('internal/buffer');
const {
convertStringToRegExp,
@@ -324,7 +325,7 @@ class FileTest extends Test {
// This method is called when it is known that there is at least one message
let bufferHead = this.#rawBuffer[0];
let headerIndex = bufferHead.indexOf(v8Header);
let nonSerialized = Buffer.alloc(0);
let nonSerialized = new FastBuffer();
while (bufferHead && headerIndex !== 0) {
const nonSerializedData = headerIndex === -1 ?

View File

@@ -70,6 +70,7 @@ const {
validateUint32,
validateFiniteNumber,
} = require('internal/validators');
const { FastBuffer } = require('internal/buffer');
const kFlushFlag = Symbol('kFlushFlag');
const kError = Symbol('kError');
@@ -150,7 +151,7 @@ function zlibBufferOnError(err) {
function zlibBufferOnEnd() {
let buf;
if (this.nread === 0) {
buf = Buffer.alloc(0);
buf = new FastBuffer();
} else {
const bufs = this.buffers;
buf = (bufs.length === 1 ? bufs[0] : Buffer.concat(bufs, this.nread));
@@ -301,7 +302,7 @@ ZlibBase.prototype.reset = function() {
* @returns {void}
*/
ZlibBase.prototype._flush = function(callback) {
this._transform(Buffer.alloc(0), '', callback);
this._transform(new FastBuffer(), '', callback);
};
/**
@@ -476,7 +477,7 @@ function processChunkSync(self, chunk, flushFlag) {
_close(self);
if (nread === 0)
return Buffer.alloc(0);
return new FastBuffer();
return (buffers.length === 1 ? buffers[0] : Buffer.concat(buffers, nread));
}