From f36831f6944b26253c2dbd9b9262104cd5070ed5 Mon Sep 17 00:00:00 2001 From: Robert Nagy Date: Wed, 14 Aug 2024 15:34:07 +0200 Subject: [PATCH] buffer: optimize createFromString PR-URL: https://github.com/nodejs/node/pull/54324 Reviewed-By: Yagiz Nizipli Reviewed-By: Matteo Collina --- lib/buffer.js | 47 ++++++++++++++++++++++++++++------------------ src/node_buffer.cc | 13 ------------- 2 files changed, 29 insertions(+), 31 deletions(-) diff --git a/lib/buffer.js b/lib/buffer.js index 6faab3096542d4..074966dd55b59b 100644 --- a/lib/buffer.js +++ b/lib/buffer.js @@ -57,7 +57,6 @@ const { byteLengthUtf8, compare: _compare, compareOffset, - createFromString, fill: bindingFill, isAscii: bindingIsAscii, isUtf8: bindingIsUtf8, @@ -148,11 +147,12 @@ const constants = ObjectDefineProperties({}, { }); Buffer.poolSize = 8 * 1024; -let poolSize, poolOffset, allocPool; +let poolSize, poolOffset, allocPool, allocBuffer; function createPool() { poolSize = Buffer.poolSize; - allocPool = createUnsafeBuffer(poolSize).buffer; + allocBuffer = createUnsafeBuffer(poolSize); + allocPool = allocBuffer.buffer; markAsUntransferable(allocPool); poolOffset = 0; } @@ -440,38 +440,49 @@ function allocate(size) { } function fromStringFast(string, ops) { - const length = ops.byteLength(string); + const maxLength = Buffer.poolSize >>> 1; - if (length >= (Buffer.poolSize >>> 1)) - return createFromString(string, ops.encodingVal); + let length = string.length; // Min length + + if (length >= maxLength) + return createFromString(string, ops); + + length *= 4; // Max length (4 bytes per character) + + if (length >= maxLength) + length = ops.byteLength(string); // Actual length + + if (length >= maxLength) + return createFromString(string, ops, length); 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); - } + + const actual = ops.write(allocBuffer, string, poolOffset, length); + const b = new FastBuffer(allocPool, poolOffset, actual); + poolOffset += actual; alignPool(); return b; } +function createFromString(string, ops, length = ops.byteLength(string)) { + const buf = Buffer.allocUnsafeSlow(length); + const actual = ops.write(buf, string, 0, length); + return actual < length ? new FastBuffer(buf.buffer, 0, actual) : buf; +} + function fromString(string, encoding) { let ops; - if (typeof encoding !== 'string' || encoding.length === 0) { - if (string.length === 0) - return new FastBuffer(); + if (!encoding || encoding === 'utf8') { ops = encodingOps.utf8; } else { ops = getEncodingOps(encoding); if (ops === undefined) throw new ERR_UNKNOWN_ENCODING(encoding); - if (string.length === 0) - return new FastBuffer(); } - return fromStringFast(string, ops); + + return string.length === 0 ? new FastBuffer() : fromStringFast(string, ops); } function fromArrayBuffer(obj, byteOffset, length) { diff --git a/src/node_buffer.cc b/src/node_buffer.cc index 527a4cf4a078d6..e6da36324362e8 100644 --- a/src/node_buffer.cc +++ b/src/node_buffer.cc @@ -529,17 +529,6 @@ MaybeLocal New(Environment* env, namespace { -void CreateFromString(const FunctionCallbackInfo& args) { - CHECK(args[0]->IsString()); - CHECK(args[1]->IsInt32()); - - enum encoding enc = static_cast(args[1].As()->Value()); - Local buf; - if (New(args.GetIsolate(), args[0].As(), enc).ToLocal(&buf)) - args.GetReturnValue().Set(buf); -} - - template void StringSlice(const FunctionCallbackInfo& args) { Environment* env = Environment::GetCurrent(args); @@ -1422,7 +1411,6 @@ void Initialize(Local target, SetMethodNoSideEffect(context, target, "btoa", Btoa); SetMethod(context, target, "setBufferPrototype", SetBufferPrototype); - SetMethodNoSideEffect(context, target, "createFromString", CreateFromString); SetFastMethodNoSideEffect(context, target, @@ -1487,7 +1475,6 @@ void Initialize(Local target, void RegisterExternalReferences(ExternalReferenceRegistry* registry) { registry->Register(SetBufferPrototype); - registry->Register(CreateFromString); registry->Register(SlowByteLengthUtf8); registry->Register(fast_byte_length_utf8.GetTypeInfo());