Skip to content

Commit 61ada9c

Browse files
committed
buffer: improve byteLength performance
1 parent 4cd8e19 commit 61ada9c

File tree

2 files changed

+21
-6
lines changed

2 files changed

+21
-6
lines changed

src/node_buffer.cc

Lines changed: 15 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -728,16 +728,25 @@ void SlowByteLengthUtf8(const FunctionCallbackInfo<Value>& args) {
728728
args.GetReturnValue().Set(args[0].As<String>()->Utf8Length(env->isolate()));
729729
}
730730

731-
uint32_t FastByteLengthUtf8(Local<Value> receiver,
732-
const v8::FastOneByteString& source) {
731+
uint32_t FastByteLengthUtf8(
732+
Local<Value> receiver,
733+
Local<Value> sourceValue,
734+
v8::FastApiCallbackOptions& options) { // NOLINT(runtime/references)
735+
TRACK_V8_FAST_API_CALL("Buffer::FastByteLengthUtf8");
736+
auto isolate = options.isolate;
737+
HandleScope handleScope(isolate);
738+
Local<String> sourceStr;
739+
CHECK(
740+
sourceValue->ToString(isolate->GetCurrentContext()).ToLocal(&sourceStr));
741+
Utf8Value source(isolate, sourceStr);
733742
// For short inputs, the function call overhead to simdutf is maybe
734743
// not worth it, reserve simdutf for long strings.
735-
if (source.length > 128) {
736-
return simdutf::utf8_length_from_latin1(source.data, source.length);
744+
if (source.length() > 128) {
745+
return simdutf::utf8_length_from_latin1(source.out(), source.length());
737746
}
738747

739-
uint32_t length = source.length;
740-
const auto input = reinterpret_cast<const uint8_t*>(source.data);
748+
uint32_t length = source.length();
749+
const auto input = reinterpret_cast<const uint8_t*>(source.out());
741750

742751
uint32_t answer = length;
743752
uint32_t i = 0;

src/node_external_reference.h

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,11 @@
1010

1111
namespace node {
1212

13+
using CFunctionA =
14+
uint32_t (*)(v8::Local<v8::Value> receiver,
15+
v8::Local<v8::Value> sourceValue,
16+
// NOLINTNEXTLINE(runtime/references) This is V8 api.
17+
v8::FastApiCallbackOptions& options);
1318
using CFunctionCallbackWithOneByteString =
1419
uint32_t (*)(v8::Local<v8::Value>, const v8::FastOneByteString&);
1520

@@ -91,6 +96,7 @@ class ExternalReferenceRegistry {
9196
ExternalReferenceRegistry();
9297

9398
#define ALLOWED_EXTERNAL_REFERENCE_TYPES(V) \
99+
V(CFunctionA) \
94100
V(CFunctionCallback) \
95101
V(CFunctionCallbackWithOneByteString) \
96102
V(CFunctionCallbackReturnBool) \

0 commit comments

Comments
 (0)