Skip to content

src: improve TextEncoder encodeInto performance #58080

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

Draft
wants to merge 2 commits into
base: main
Choose a base branch
from
Draft
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
44 changes: 43 additions & 1 deletion src/encoding_binding.cc
Original file line number Diff line number Diff line change
Expand Up @@ -2,10 +2,12 @@
#include "ada.h"
#include "env-inl.h"
#include "node_buffer.h"
#include "node_debug.h"
#include "node_errors.h"
#include "node_external_reference.h"
#include "simdutf.h"
#include "string_bytes.h"
#include "v8-fast-api-calls.h"
#include "v8.h"

#include <cstdint>
Expand All @@ -16,7 +18,9 @@ namespace encoding_binding {
using v8::ArrayBuffer;
using v8::BackingStore;
using v8::BackingStoreInitializationMode;
using v8::CFunction;
using v8::Context;
using v8::FastApiCallbackOptions;
using v8::FunctionCallbackInfo;
using v8::HandleScope;
using v8::Isolate;
Expand Down Expand Up @@ -112,6 +116,42 @@ void BindingData::EncodeInto(const FunctionCallbackInfo<Value>& args) {
binding_data->encode_into_results_buffer_[1] = written;
}

void BindingData::FastEncodeInto(
Local<Value> receiver,
Local<Value> source,
Local<Value> dest,
// NOLINTNEXTLINE(runtime/references) This is V8 api.
FastApiCallbackOptions& options) {
TRACK_V8_FAST_API_CALL("encoding_binding.encodeInto");
CHECK(source->IsString());
CHECK(dest->IsUint8Array());

HandleScope scope(options.isolate);
auto context = options.isolate->GetCurrentContext();
Realm* realm = Realm::GetCurrent(context);
BindingData* binding_data = realm->GetBindingData<BindingData>();

auto source_ = source.As<String>();
auto dest_ = dest.As<Uint8Array>();
Local<ArrayBuffer> buf = dest_->Buffer();
char* write_result = static_cast<char*>(buf->Data()) + dest_->ByteOffset();
size_t dest_length = dest_->ByteLength();

size_t nchars;
size_t written = source_->WriteUtf8V2(
options.isolate,
write_result,
dest_length,
String::WriteFlags::kReplaceInvalidUtf8,
&nchars);

binding_data->encode_into_results_buffer_[0] = nchars;
binding_data->encode_into_results_buffer_[1] = written;
}

static CFunction fast_encode_into_ =
CFunction::Make(BindingData::FastEncodeInto);

// Encode a single string to a UTF-8 Uint8Array (not Buffer).
// Used in TextEncoder.prototype.encode.
void BindingData::EncodeUtf8String(const FunctionCallbackInfo<Value>& args) {
Expand Down Expand Up @@ -217,7 +257,7 @@ void BindingData::ToUnicode(const FunctionCallbackInfo<Value>& args) {
void BindingData::CreatePerIsolateProperties(IsolateData* isolate_data,
Local<ObjectTemplate> target) {
Isolate* isolate = isolate_data->isolate();
SetMethod(isolate, target, "encodeInto", EncodeInto);
SetFastMethod(isolate, target, "encodeInto", EncodeInto, &fast_encode_into_);
SetMethodNoSideEffect(isolate, target, "encodeUtf8String", EncodeUtf8String);
SetMethodNoSideEffect(isolate, target, "decodeUTF8", DecodeUTF8);
SetMethodNoSideEffect(isolate, target, "toASCII", ToASCII);
Expand All @@ -236,6 +276,8 @@ void BindingData::CreatePerContextProperties(Local<Object> target,
void BindingData::RegisterTimerExternalReferences(
ExternalReferenceRegistry* registry) {
registry->Register(EncodeInto);
registry->Register(FastEncodeInto);
registry->Register(fast_encode_into_.GetTypeInfo());
registry->Register(EncodeUtf8String);
registry->Register(DecodeUTF8);
registry->Register(ToASCII);
Expand Down
4 changes: 4 additions & 0 deletions src/encoding_binding.h
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,10 @@ class BindingData : public SnapshotableObject {
SET_MEMORY_INFO_NAME(BindingData)

static void EncodeInto(const v8::FunctionCallbackInfo<v8::Value>& args);
static void FastEncodeInto(v8::Local<v8::Value> receiver,
v8::Local<v8::Value> source,
v8::Local<v8::Value> dest,
v8::FastApiCallbackOptions& options);
static void EncodeUtf8String(const v8::FunctionCallbackInfo<v8::Value>& args);
static void DecodeUTF8(const v8::FunctionCallbackInfo<v8::Value>& args);
static void DecodeLatin1(const v8::FunctionCallbackInfo<v8::Value>& args);
Expand Down
8 changes: 8 additions & 0 deletions src/node_external_reference.h
Original file line number Diff line number Diff line change
Expand Up @@ -92,6 +92,13 @@ using CFunctionBufferCopy = uint32_t (*)(v8::Local<v8::Value>,
uint32_t,
v8::FastApiCallbackOptions&);

using CFunctionEncodeInto =
void (*)(v8::Local<v8::Value> receiver,
v8::Local<v8::Value> source,
v8::Local<v8::Value> dest,
// NOLINTNEXTLINE(runtime/references) This is V8 api.
v8::FastApiCallbackOptions& options);

// This class manages the external references from the V8 heap
// to the C++ addresses in Node.js.
class ExternalReferenceRegistry {
Expand Down Expand Up @@ -121,6 +128,7 @@ class ExternalReferenceRegistry {
V(CFunctionWithBool) \
V(CFunctionBufferCopy) \
V(CFunctionWriteString) \
V(CFunctionEncodeInto) \
V(const v8::CFunctionInfo*) \
V(v8::FunctionCallback) \
V(v8::AccessorNameGetterCallback) \
Expand Down
Loading