Skip to content

Commit 3ac0522

Browse files
committed
perf: convert JS string arguments through a stack buffer
ConvertToJavaString copied every JS string into a heap-allocated v8::String::Value before NewString. Strings up to 256 UTF-16 units are now written into a stack buffer; longer ones use one vector.
1 parent cde7d06 commit 3ac0522

1 file changed

Lines changed: 21 additions & 2 deletions

File tree

‎test-app/runtime/src/main/cpp/ArgConverter.h‎

Lines changed: 21 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -131,8 +131,27 @@ class ArgConverter {
131131
inline static jstring ConvertToJavaString(const v8::Local<v8::Value>& jsValue) {
132132
JEnv env;
133133
auto isolate = v8::Isolate::GetCurrent();
134-
v8::String::Value stringValue(isolate, jsValue);
135-
return env.NewString((const jchar*) *stringValue, stringValue.length());
134+
v8::Local<v8::String> str;
135+
if (jsValue->IsString()) {
136+
str = jsValue.As<v8::String>();
137+
} else if (!jsValue->ToString(isolate->GetCurrentContext()).ToLocal(&str)) {
138+
// ToString threw and left a V8 exception pending; running it
139+
// again would re-enter user code.
140+
static const jchar kNoUnits[1] = {0};
141+
return env.NewString(kNoUnits, 0);
142+
}
143+
144+
constexpr uint32_t kInlineUnits = 256;
145+
uint32_t length = str->Length();
146+
uint16_t inlineUnits[kInlineUnits];
147+
std::vector<uint16_t> heapUnits;
148+
uint16_t* units = inlineUnits;
149+
if (length > kInlineUnits) {
150+
heapUnits.resize(length);
151+
units = heapUnits.data();
152+
}
153+
str->WriteV2(isolate, 0, length, units);
154+
return env.NewString(reinterpret_cast<const jchar*>(units), static_cast<jsize>(length));
136155
}
137156

138157
inline static v8::Local<v8::String> ConvertToV8String(v8::Isolate* isolate, const jchar* data, int length) {

0 commit comments

Comments
 (0)