buffer: improve byteLength performance

# Conflicts:
#	src/node_external_reference.h

PR-URL: https://github.com/nodejs/node/pull/58048
Reviewed-By: Robert Nagy <ronagy@icloud.com>
Reviewed-By: James M Snell <jasnell@gmail.com>
Reviewed-By: Matteo Collina <matteo.collina@gmail.com>
Reviewed-By: Vinícius Lourenço Claro Cardoso <contact@viniciusl.com.br>
Reviewed-By: Daniel Lemire <daniel@lemire.me>
This commit is contained in:
Yagiz Nizipli authored and RafaelGSS committed 2025-05-02 17:06:56 -03:00
1 parent 6682861d6f
commit dc22890dd8
2 files changed
+24 -6

No files matched your search

+18 -6
View File
@@ -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;
+6
View File
@@ -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&);
@@ -98,6 +103,7 @@ class ExternalReferenceRegistry {
ExternalReferenceRegistry();
#define ALLOWED_EXTERNAL_REFERENCE_TYPES(V) \
V(CFunctionA) \
V(CFunctionCallback) \
V(CFunctionCallbackWithalueAndOptions) \
V(CFunctionCallbackWithMultipleValueAndOptions) \