Skip to content

buffer: improve byteLength performance #58048

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Open
wants to merge 1 commit into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
24 changes: 18 additions & 6 deletions src/node_buffer.cc
Original file line number Diff line number Diff line change
Expand Up @@ -728,16 +728,28 @@ void SlowByteLengthUtf8(const FunctionCallbackInfo<Value>& args) {
args.GetReturnValue().Set(args[0].As<String>()->Utf8Length(env->isolate()));
}

uint32_t FastByteLengthUtf8(Local<Value> receiver,
const v8::FastOneByteString& source) {
uint32_t FastByteLengthUtf8(
Local<Value> receiver,
Local<Value> 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<String> sourceStr = sourceValue.As<String>();

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<const uint8_t*>(source.data);
uint32_t length = source->length();
const auto input = reinterpret_cast<const uint8_t*>(source->data());

uint32_t answer = length;
uint32_t i = 0;
Expand Down
6 changes: 6 additions & 0 deletions src/node_external_reference.h
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,11 @@ using CFunctionCallbackWithMultipleValueAndOptions =
v8::Local<v8::Value>,
v8::Local<v8::Value>,
v8::FastApiCallbackOptions&);
using CFunctionA =
uint32_t (*)(v8::Local<v8::Value> receiver,
v8::Local<v8::Value> sourceValue,
// NOLINTNEXTLINE(runtime/references) This is V8 api.
v8::FastApiCallbackOptions& options);
using CFunctionCallbackWithOneByteString =
uint32_t (*)(v8::Local<v8::Value>, const v8::FastOneByteString&);

Expand Down Expand Up @@ -98,6 +103,7 @@ class ExternalReferenceRegistry {
ExternalReferenceRegistry();

#define ALLOWED_EXTERNAL_REFERENCE_TYPES(V) \
V(CFunctionA) \
V(CFunctionCallback) \
V(CFunctionCallbackWithalueAndOptions) \
V(CFunctionCallbackWithMultipleValueAndOptions) \
Expand Down
Loading