From f36b3f2a19d49f7506f48aba892be42cd57136c6 Mon Sep 17 00:00:00 2001 From: Robert Nagy Date: Mon, 12 Aug 2024 08:04:21 +0200 Subject: [PATCH] buffer: rm createFromString and use fast Buffer.write --- lib/buffer.js | 27 ++++++++++++++++++++------- src/node_buffer.cc | 13 ------------- 2 files changed, 20 insertions(+), 20 deletions(-) diff --git a/lib/buffer.js b/lib/buffer.js index 4e6031afdb3919..aa9908ad91977e 100644 --- a/lib/buffer.js +++ b/lib/buffer.js @@ -59,7 +59,6 @@ const { compare: _compare, compareOffset, copy: _copy, - createFromString, fill: bindingFill, isAscii: bindingIsAscii, isUtf8: bindingIsUtf8, @@ -442,22 +441,36 @@ function allocate(size) { } function fromStringFast(string, ops) { - const length = ops.byteLength(string); + const maxLength = string.length * 4; // max utf8 length - if (length >= (Buffer.poolSize >>> 1)) - return createFromString(string, ops.encodingVal); + if (maxLength >= (Buffer.poolSize >>> 1)) + return fromStringSlow(string, ops); - if (length > (poolSize - poolOffset)) + if (maxLength > (poolSize - poolOffset)) createPool(); - let b = new FastBuffer(allocPool, poolOffset, length); + + let b = new FastBuffer(allocPool, poolOffset, maxLength); + const actual = ops.write(b, string, 0, maxLength); + + poolOffset += actual; + alignPool(); + + return new FastBuffer(allocPool, poolOffset, actual); +} + +function fromStringSlow(string, ops) { + const length = ops.byteLength(string); + + let b = createUnsafeBuffer(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) { diff --git a/src/node_buffer.cc b/src/node_buffer.cc index 6e141b974131cc..d8fce1a8e8106a 100644 --- a/src/node_buffer.cc +++ b/src/node_buffer.cc @@ -530,17 +530,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); @@ -1436,7 +1425,6 @@ void Initialize(Local target, SetMethodNoSideEffect(context, target, "btoa", Btoa); SetMethod(context, target, "setBufferPrototype", SetBufferPrototype); - SetMethodNoSideEffect(context, target, "createFromString", CreateFromString); SetFastMethodNoSideEffect(context, target, @@ -1501,7 +1489,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());