From 5bf53933ebede741f283ca55b1fd331f15db6121 Mon Sep 17 00:00:00 2001 From: Yagiz Nizipli Date: Sat, 26 Apr 2025 22:19:13 -0400 Subject: [PATCH] buffer: improve byteLength performance # Conflicts: # src/node_external_reference.h --- src/node_buffer.cc | 24 ++++++++++++++++++------ src/node_external_reference.h | 6 ++++++ 2 files changed, 24 insertions(+), 6 deletions(-) diff --git a/src/node_buffer.cc b/src/node_buffer.cc index 3507f5e3e2943a..adacf470306d81 100644 --- a/src/node_buffer.cc +++ b/src/node_buffer.cc @@ -728,16 +728,28 @@ void SlowByteLengthUtf8(const FunctionCallbackInfo& args) { args.GetReturnValue().Set(args[0].As()->Utf8Length(env->isolate())); } -uint32_t FastByteLengthUtf8(Local receiver, - const v8::FastOneByteString& source) { +uint32_t FastByteLengthUtf8( + Local receiver, + Local sourceValue, + v8::FastApiCallbackOptions& options) { // NOLINT(runtime/references) + TRACK_V8_FAST_API_CALL("Buffer::FastByteLengthUtf8"); + auto isolate = options.isolate; + HandleScope handleScope(isolate); + CHECK(sourceValue->IsString()); + Local sourceStr = sourceValue.As(); + + if (!sourceStr->IsExternalOneByte()) { + return sourceStr->Utf8Length(isolate); + } + auto source = sourceStr->GetExternalOneByteStringResource(); // For short inputs, the function call overhead to simdutf is maybe // not worth it, reserve simdutf for long strings. - if (source.length > 128) { - return simdutf::utf8_length_from_latin1(source.data, source.length); + if (source->length() > 128) { + return simdutf::utf8_length_from_latin1(source->data(), source->length()); } - uint32_t length = source.length; - const auto input = reinterpret_cast(source.data); + uint32_t length = source->length(); + const auto input = reinterpret_cast(source->data()); uint32_t answer = length; uint32_t i = 0; diff --git a/src/node_external_reference.h b/src/node_external_reference.h index c652c6878c353d..b8f0b40b8dd8c5 100644 --- a/src/node_external_reference.h +++ b/src/node_external_reference.h @@ -17,6 +17,11 @@ using CFunctionCallbackWithMultipleValueAndOptions = v8::Local, v8::Local, v8::FastApiCallbackOptions&); +using CFunctionA = + uint32_t (*)(v8::Local receiver, + v8::Local sourceValue, + // NOLINTNEXTLINE(runtime/references) This is V8 api. + v8::FastApiCallbackOptions& options); using CFunctionCallbackWithOneByteString = uint32_t (*)(v8::Local, const v8::FastOneByteString&); @@ -98,6 +103,7 @@ class ExternalReferenceRegistry { ExternalReferenceRegistry(); #define ALLOWED_EXTERNAL_REFERENCE_TYPES(V) \ + V(CFunctionA) \ V(CFunctionCallback) \ V(CFunctionCallbackWithalueAndOptions) \ V(CFunctionCallbackWithMultipleValueAndOptions) \