Skip to content

Commit

Permalink
buffer: refactor create buffer
Browse files Browse the repository at this point in the history
Use createBuffer to reduce new Uint8Array()
and setPrototypeOf.

PR-URL: nodejs#4340
Reviewed-By: Michaël Zasso <[email protected]>
Reviewed-By: Сковорода Никита Андреевич <[email protected]>
Reviewed-By: Trevor Norris <[email protected]>
Reviewed-By: Ben Noordhuis <[email protected]>
  • Loading branch information
JacksonTian authored and Fishrock123 committed Jan 6, 2016
1 parent 474a0f0 commit 8781c59
Showing 1 changed file with 9 additions and 11 deletions.
20 changes: 9 additions & 11 deletions lib/buffer.js
Original file line number Diff line number Diff line change
Expand Up @@ -19,13 +19,17 @@ binding.setupBufferJS(Buffer.prototype, bindingObj);
const flags = bindingObj.flags;
const kNoZeroFill = 0;

function createBuffer(size) {
const ui8 = new Uint8Array(size);
Object.setPrototypeOf(ui8, Buffer.prototype);
return ui8;
}

function createPool() {
poolSize = Buffer.poolSize;
if (poolSize > 0)
flags[kNoZeroFill] = 1;
allocPool = new Uint8Array(poolSize);
Object.setPrototypeOf(allocPool, Buffer.prototype);
allocPool = createBuffer(poolSize);
poolOffset = 0;
}
createPool();
Expand Down Expand Up @@ -67,9 +71,7 @@ function SlowBuffer(length) {
length = 0;
if (length > 0)
flags[kNoZeroFill] = 1;
const ui8 = new Uint8Array(+length);
Object.setPrototypeOf(ui8, Buffer.prototype);
return ui8;
return createBuffer(+length);
}

Object.setPrototypeOf(SlowBuffer.prototype, Uint8Array.prototype);
Expand All @@ -78,9 +80,7 @@ Object.setPrototypeOf(SlowBuffer, Uint8Array);

function allocate(size) {
if (size === 0) {
const ui8 = new Uint8Array(size);
Object.setPrototypeOf(ui8, Buffer.prototype);
return ui8;
return createBuffer(size);
}
if (size < (Buffer.poolSize >>> 1)) {
if (size > (poolSize - poolOffset))
Expand All @@ -95,9 +95,7 @@ function allocate(size) {
// being zero filled.
if (size > 0)
flags[kNoZeroFill] = 1;
const ui8 = new Uint8Array(size);
Object.setPrototypeOf(ui8, Buffer.prototype);
return ui8;
return createBuffer(size);
}
}

Expand Down

0 comments on commit 8781c59

Please sign in to comment.