Skip to content

buffer: directly use ArrayBuffer as the pool #8302

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Closed
wants to merge 1 commit into from
Closed
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
18 changes: 13 additions & 5 deletions lib/buffer.js
Original file line number Diff line number Diff line change
Expand Up @@ -30,17 +30,21 @@ binding.setupBufferJS(Buffer.prototype, bindingObj);
const zeroFill = bindingObj.zeroFill || [0];

function createUnsafeBuffer(size) {
return new FastBuffer(createUnsafeArrayBuffer(size));
}

function createUnsafeArrayBuffer(size) {
zeroFill[0] = 0;
try {
return new FastBuffer(size);
return new ArrayBuffer(size);
} finally {
zeroFill[0] = 1;
}
}

function createPool() {
poolSize = Buffer.poolSize;
allocPool = createUnsafeBuffer(poolSize);
allocPool = createUnsafeArrayBuffer(poolSize);
poolOffset = 0;
}
createPool();
Expand Down Expand Up @@ -183,7 +187,7 @@ function allocate(size) {
if (size < (Buffer.poolSize >>> 1)) {
if (size > (poolSize - poolOffset))
createPool();
var b = allocPool.slice(poolOffset, poolOffset + size);
var b = new FastBuffer(allocPool, poolOffset, size);
poolOffset += size;
alignPool();
return b;
Expand All @@ -210,8 +214,12 @@ function fromString(string, encoding) {

if (length > (poolSize - poolOffset))
createPool();
var actual = allocPool.write(string, poolOffset, encoding);
var b = allocPool.slice(poolOffset, poolOffset + actual);
var b = new FastBuffer(allocPool, poolOffset, length);
var actual = b.write(string, encoding);
if (actual !== length) {
// byteLength() may overestimate. That’s a rare case, though.
b = new FastBuffer(allocPool, poolOffset, actual);
}
poolOffset += actual;
alignPool();
return b;
Expand Down