buffer: refactor create buffer
Use createBuffer to reduce new Uint8Array() and setPrototypeOf. PR-URL: https://github.com/nodejs/node/pull/4340 Reviewed-By: Michaël Zasso <mic.besace@gmail.com> Reviewed-By: Сковорода Никита Андреевич <chalkerx@gmail.com> Reviewed-By: Trevor Norris <trev.norris@gmail.com> Reviewed-By: Ben Noordhuis <info@bnoordhuis.nl>
This commit is contained in:
parent
67b109a4b4
commit
26a82971b2
@ -19,13 +19,17 @@ binding.setupBufferJS(Buffer.prototype, bindingObj);
|
|||||||
const flags = bindingObj.flags;
|
const flags = bindingObj.flags;
|
||||||
const kNoZeroFill = 0;
|
const kNoZeroFill = 0;
|
||||||
|
|
||||||
|
function createBuffer(size) {
|
||||||
|
const ui8 = new Uint8Array(size);
|
||||||
|
Object.setPrototypeOf(ui8, Buffer.prototype);
|
||||||
|
return ui8;
|
||||||
|
}
|
||||||
|
|
||||||
function createPool() {
|
function createPool() {
|
||||||
poolSize = Buffer.poolSize;
|
poolSize = Buffer.poolSize;
|
||||||
if (poolSize > 0)
|
if (poolSize > 0)
|
||||||
flags[kNoZeroFill] = 1;
|
flags[kNoZeroFill] = 1;
|
||||||
allocPool = new Uint8Array(poolSize);
|
allocPool = createBuffer(poolSize);
|
||||||
Object.setPrototypeOf(allocPool, Buffer.prototype);
|
|
||||||
poolOffset = 0;
|
poolOffset = 0;
|
||||||
}
|
}
|
||||||
createPool();
|
createPool();
|
||||||
@ -67,9 +71,7 @@ function SlowBuffer(length) {
|
|||||||
length = 0;
|
length = 0;
|
||||||
if (length > 0)
|
if (length > 0)
|
||||||
flags[kNoZeroFill] = 1;
|
flags[kNoZeroFill] = 1;
|
||||||
const ui8 = new Uint8Array(+length);
|
return createBuffer(+length);
|
||||||
Object.setPrototypeOf(ui8, Buffer.prototype);
|
|
||||||
return ui8;
|
|
||||||
}
|
}
|
||||||
|
|
||||||
Object.setPrototypeOf(SlowBuffer.prototype, Uint8Array.prototype);
|
Object.setPrototypeOf(SlowBuffer.prototype, Uint8Array.prototype);
|
||||||
@ -78,9 +80,7 @@ Object.setPrototypeOf(SlowBuffer, Uint8Array);
|
|||||||
|
|
||||||
function allocate(size) {
|
function allocate(size) {
|
||||||
if (size === 0) {
|
if (size === 0) {
|
||||||
const ui8 = new Uint8Array(size);
|
return createBuffer(size);
|
||||||
Object.setPrototypeOf(ui8, Buffer.prototype);
|
|
||||||
return ui8;
|
|
||||||
}
|
}
|
||||||
if (size < (Buffer.poolSize >>> 1)) {
|
if (size < (Buffer.poolSize >>> 1)) {
|
||||||
if (size > (poolSize - poolOffset))
|
if (size > (poolSize - poolOffset))
|
||||||
@ -95,9 +95,7 @@ function allocate(size) {
|
|||||||
// being zero filled.
|
// being zero filled.
|
||||||
if (size > 0)
|
if (size > 0)
|
||||||
flags[kNoZeroFill] = 1;
|
flags[kNoZeroFill] = 1;
|
||||||
const ui8 = new Uint8Array(size);
|
return createBuffer(size);
|
||||||
Object.setPrototypeOf(ui8, Buffer.prototype);
|
|
||||||
return ui8;
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
Loading…
x
Reference in New Issue
Block a user