buffer: avoid materializing ArrayBuffer for creation

Do not create an `ArrayBuffer` if the engine’s settings avoid it
and we don’t need it.

PR-URL: https://github.com/nodejs/node/pull/26301
Reviewed-By: Matteo Collina <matteo.collina@gmail.com>
Reviewed-By: James M Snell <jasnell@gmail.com>
This commit is contained in:
Anna Henningsen 2019-02-25 04:41:53 +01:00
parent 31975bbc88
commit 6a0f4636d9
No known key found for this signature in database
GPG Key ID: 9C63F3A6CD2AD8F9

View File

@ -105,13 +105,9 @@ let poolSize, poolOffset, allocPool;
const zeroFill = bindingZeroFill || [0];
function createUnsafeBuffer(size) {
return new FastBuffer(createUnsafeArrayBuffer(size));
}
function createUnsafeArrayBuffer(size) {
zeroFill[0] = 0;
try {
return new ArrayBuffer(size);
return new FastBuffer(size);
} finally {
zeroFill[0] = 1;
}
@ -119,7 +115,7 @@ function createUnsafeArrayBuffer(size) {
function createPool() {
poolSize = Buffer.poolSize;
allocPool = createUnsafeArrayBuffer(poolSize);
allocPool = createUnsafeBuffer(poolSize).buffer;
poolOffset = 0;
}
createPool();