mirror of
https://github.com/nodejs/node.git
synced 2026-11-21 12:59:16 -05:00
buffer: use v8 fast API calls for Buffer.byteLength implementation
Use v8 fast API calls for Buffer.byteLength with sequential one-byte strings. PR-URL: https://github.com/nodejs/node/pull/46616 Reviewed-By: Anna Henningsen <anna@addaleax.net> Reviewed-By: Robert Nagy <ronagy@icloud.com> Reviewed-By: Yagiz Nizipli <yagiz@nizipli.com> Reviewed-By: James M Snell <jasnell@gmail.com>
This commit is contained in:
1 parent
43ddf7631e
commit
ee1ce1872f
2 files changed
+28
-3
No files matched your search
+25
-3
@@ -32,6 +32,7 @@
|
||||
#include "string_bytes.h"
|
||||
#include "string_search.h"
|
||||
#include "util-inl.h"
|
||||
#include "v8-fast-api-calls.h"
|
||||
#include "v8.h"
|
||||
|
||||
#include <cstring>
|
||||
@@ -786,7 +787,7 @@ void StringWrite(const FunctionCallbackInfo<Value>& args) {
|
||||
args.GetReturnValue().Set(written);
|
||||
}
|
||||
|
||||
void ByteLengthUtf8(const FunctionCallbackInfo<Value> &args) {
|
||||
void SlowByteLengthUtf8(const FunctionCallbackInfo<Value>& args) {
|
||||
Environment* env = Environment::GetCurrent(args);
|
||||
CHECK(args[0]->IsString());
|
||||
|
||||
@@ -794,6 +795,21 @@ void ByteLengthUtf8(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 result = 0;
|
||||
uint32_t length = source.length;
|
||||
const uint8_t* data = reinterpret_cast<const uint8_t*>(source.data);
|
||||
for (uint32_t i = 0; i < length; ++i) {
|
||||
result += (data[i] >> 7);
|
||||
}
|
||||
result += length;
|
||||
return result;
|
||||
}
|
||||
|
||||
static v8::CFunction fast_byte_length_utf8(
|
||||
v8::CFunction::Make(FastByteLengthUtf8));
|
||||
|
||||
// Normalize val to be an integer in the range of [1, -1] since
|
||||
// implementations of memcmp() can vary by platform.
|
||||
static int normalizeCompareVal(int val, size_t a_length, size_t b_length) {
|
||||
@@ -1368,7 +1384,11 @@ void Initialize(Local<Object> target,
|
||||
SetMethodNoSideEffect(context, target, "createFromString", CreateFromString);
|
||||
SetMethodNoSideEffect(context, target, "decodeUTF8", DecodeUTF8);
|
||||
|
||||
SetMethodNoSideEffect(context, target, "byteLengthUtf8", ByteLengthUtf8);
|
||||
SetFastMethodNoSideEffect(context,
|
||||
target,
|
||||
"byteLengthUtf8",
|
||||
SlowByteLengthUtf8,
|
||||
&fast_byte_length_utf8);
|
||||
SetMethod(context, target, "copy", Copy);
|
||||
SetMethodNoSideEffect(context, target, "compare", Compare);
|
||||
SetMethodNoSideEffect(context, target, "compareOffset", CompareOffset);
|
||||
@@ -1429,7 +1449,9 @@ void RegisterExternalReferences(ExternalReferenceRegistry* registry) {
|
||||
registry->Register(CreateFromString);
|
||||
registry->Register(DecodeUTF8);
|
||||
|
||||
registry->Register(ByteLengthUtf8);
|
||||
registry->Register(SlowByteLengthUtf8);
|
||||
registry->Register(fast_byte_length_utf8.GetTypeInfo());
|
||||
registry->Register(FastByteLengthUtf8);
|
||||
registry->Register(Copy);
|
||||
registry->Register(Compare);
|
||||
registry->Register(CompareOffset);
|
||||
|
||||
Reference in new issue
Block a user