From 9b240c19dd1dcbe230a404dcdb599d78ce655413 Mon Sep 17 00:00:00 2001 From: Robert Nagy Date: Mon, 12 Aug 2024 08:04:21 +0200 Subject: [PATCH] buffer: optimize createFromString PR-URL: https://github.com/nodejs/node/pull/54324 --- benchmark/buffers/buffer-from.js | 14 +++++++------- lib/buffer.js | 18 ++++++++++++++---- 2 files changed, 21 insertions(+), 11 deletions(-) diff --git a/benchmark/buffers/buffer-from.js b/benchmark/buffers/buffer-from.js index 3b88fe3893d327..59440bc891677f 100644 --- a/benchmark/buffers/buffer-from.js +++ b/benchmark/buffers/buffer-from.js @@ -4,16 +4,16 @@ const common = require('../common.js'); const assert = require('assert'); const bench = common.createBenchmark(main, { source: [ - 'array', - 'arraybuffer', - 'arraybuffer-middle', - 'buffer', + // 'array', + // 'arraybuffer', + // 'arraybuffer-middle', + // 'buffer', 'string', 'string-utf8', 'string-base64', - 'object', - 'uint8array', - 'uint16array', + // 'object', + // 'uint8array', + // 'uint16array', ], len: [100, 2048], n: [8e5], diff --git a/lib/buffer.js b/lib/buffer.js index 4e6031afdb3919..23588fecc0e435 100644 --- a/lib/buffer.js +++ b/lib/buffer.js @@ -442,27 +442,37 @@ function allocate(size) { } function fromStringFast(string, ops) { - const length = ops.byteLength(string); + const stringLength = string.length - if (length >= (Buffer.poolSize >>> 1)) + if (stringLength > Buffer.poolSize) + return createFromString(string, ops.encodingVal); + + let length = stringLength * 4; // max utf8 byte length + + if (length > Buffer.poolSize) + length = ops.byteLength(string); + + if (length * 2 > Buffer.poolSize) return createFromString(string, ops.encodingVal); if (length > (poolSize - poolOffset)) createPool(); + let b = new FastBuffer(allocPool, poolOffset, length); const actual = ops.write(b, string, 0, length); if (actual !== length) { - // byteLength() may overestimate. That's a rare case, though. b = new FastBuffer(allocPool, poolOffset, actual); } + poolOffset += actual; alignPool(); + return b; } function fromString(string, encoding) { let ops; - if (typeof encoding !== 'string' || encoding.length === 0) { + if (typeof encoding !== 'string' || encoding === 'utf8' || encoding.length === 0) { if (string.length === 0) return new FastBuffer(); ops = encodingOps.utf8;