Skip to content

Commit 61d89e5

Browse files
committed
buffer: optimize createFromString
PR-URL: #54324
1 parent 298ff4f commit 61d89e5

File tree

1 file changed

+47
-10
lines changed

1 file changed

+47
-10
lines changed

lib/buffer.js

Lines changed: 47 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -150,11 +150,12 @@ const constants = ObjectDefineProperties({}, {
150150
});
151151

152152
Buffer.poolSize = 8 * 1024;
153-
let poolSize, poolOffset, allocPool;
153+
let poolSize, poolOffset, allocPool, allocBuffer;
154154

155155
function createPool() {
156156
poolSize = Buffer.poolSize;
157-
allocPool = createUnsafeBuffer(poolSize).buffer;
157+
allocBuffer = createUnsafeBuffer(poolSize);
158+
allocPool = allocBuffer.buffer;
158159
markAsUntransferable(allocPool);
159160
poolOffset = 0;
160161
}
@@ -441,28 +442,64 @@ function allocate(size) {
441442
return createUnsafeBuffer(size);
442443
}
443444

445+
// function fromStringFast(string, ops) {
446+
// let length = string.length;
447+
448+
// if (length > Buffer.poolSize) {
449+
// return createFromString(string, ops.encodingVal);
450+
// }
451+
452+
// length <<= 2; // max utf8 byte length
453+
454+
// if (length > Buffer.poolSize) {
455+
// length = ops.byteLength(string);
456+
// if (length > (Buffer.poolSize >>> 1)) {
457+
// return createFromString(string, ops.encodingVal);
458+
// }
459+
// }
460+
461+
// if (length > (poolSize - poolOffset)) {
462+
// createPool();
463+
// }
464+
465+
// const actual = ops.write(allocBuffer, string, poolOffset, length);
466+
// const b = new FastBuffer(allocPool, poolOffset, actual);
467+
468+
// poolOffset += actual;
469+
// alignPool();
470+
471+
// return b;
472+
// }
473+
474+
444475
function fromStringFast(string, ops) {
445-
const length = ops.byteLength(string);
476+
let length = string.length;
477+
478+
if (length > Buffer.poolSize)
479+
return createFromString(string, ops.encodingVal);
480+
481+
length <<= 2; // max utf8 byte length
482+
483+
if (length >= (Buffer.poolSize >>> 1))
484+
length = ops.byteLength(string);
446485

447486
if (length >= (Buffer.poolSize >>> 1))
448487
return createFromString(string, ops.encodingVal);
449488

450489
if (length > (poolSize - poolOffset))
451490
createPool();
452-
let b = new FastBuffer(allocPool, poolOffset, length);
453-
const actual = ops.write(b, string, 0, length);
454-
if (actual !== length) {
455-
// byteLength() may overestimate. That's a rare case, though.
456-
b = new FastBuffer(allocPool, poolOffset, actual);
457-
}
491+
492+
const actual = ops.write(allocBuffer, string, poolOffset, length);
493+
const b = new FastBuffer(allocPool, poolOffset, actual);
494+
458495
poolOffset += actual;
459496
alignPool();
460497
return b;
461498
}
462499

463500
function fromString(string, encoding) {
464501
let ops;
465-
if (typeof encoding !== 'string' || encoding.length === 0) {
502+
if (!encoding || encoding === 'utf8' || typeof encoding !== 'string') {
466503
if (string.length === 0)
467504
return new FastBuffer();
468505
ops = encodingOps.utf8;

0 commit comments

Comments
 (0)