Skip to content

Commit fa1b3e4

Browse files
committed
perf(runtime): cache per-isolate keys and names used per access
- The implementation-object, "supervalue" and "typemetadata" private keys were rebuilt from strings (and resolved through V8's private symbol registry) on every constructor call, `super` read and GetTypeMetadata. They are now created once per isolate in V8StringConstants next to the existing private keys; Private::ForApi on the same names yields the same symbols. - Kotlin property accessors copied the accessor method's name and created a fresh V8 string per get/set; PropertyCallbackData now holds the internalized names. - CreateArrayWrapper looked up the java/lang/Object node under the global node mutex for every Java array it wrapped; nodes are shared by all runtimes and never freed, so the pointer is resolved once. - MethodCallback decoded its External twice.
1 parent 49332f3 commit fa1b3e4

4 files changed

Lines changed: 51 additions & 20 deletions

File tree

‎test-app/runtime/src/main/cpp/MetadataNode.cpp‎

Lines changed: 14 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -389,7 +389,8 @@ void MetadataNode::ArrayLengthGetterCallack(Local<Name> property, const Property
389389
}
390390

391391
Local<Object> MetadataNode::CreateArrayWrapper(Isolate* isolate) {
392-
auto node = GetOrCreate("java/lang/Object");
392+
// Nodes are shared by every runtime and never freed.
393+
static MetadataNode* const node = GetOrCreate("java/lang/Object");
393394
auto ctorFunc = node->GetConstructorFunction(isolate);
394395

395396
auto arrayObjectTemplate = GetOrCreateArrayObjectTemplate(isolate);
@@ -656,12 +657,11 @@ void MetadataNode::PropertyAccessorGetterCallback(const FunctionCallbackInfo<Val
656657
auto thiz = info.This();
657658
auto propertyCallbackData = reinterpret_cast<PropertyCallbackData*>(info.Data().As<External>()->Value(v8::kExternalPointerTypeTagDefault));
658659

659-
std::string getterMethodName = propertyCallbackData->getterMethodName;
660-
if(getterMethodName == ""){
660+
if (propertyCallbackData->getterName.IsEmpty()) {
661661
throw NativeScriptException("Missing getter method for property: " + propertyCallbackData->propertyName);
662662
}
663663

664-
auto getter = thiz->Get(context, v8::String::NewFromUtf8(isolate, getterMethodName.c_str()).ToLocalChecked()).ToLocalChecked();
664+
auto getter = thiz->Get(context, propertyCallbackData->getterName.Get(isolate)).ToLocalChecked();
665665
auto value = getter.As<Function>()->Call(context, thiz, 0, nullptr).ToLocalChecked();
666666
info.GetReturnValue().Set(value);
667667
} catch (NativeScriptException& e) {
@@ -684,12 +684,11 @@ void MetadataNode::PropertyAccessorSetterCallback(const FunctionCallbackInfo<Val
684684
auto thiz = info.This();
685685
auto propertyCallbackData = reinterpret_cast<PropertyCallbackData*>(info.Data().As<External>()->Value(v8::kExternalPointerTypeTagDefault));
686686

687-
std::string setterMethodName = propertyCallbackData->setterMethodName;
688-
if(setterMethodName == ""){
687+
if (propertyCallbackData->setterName.IsEmpty()) {
689688
throw NativeScriptException("Missing setter method for property: " + propertyCallbackData->propertyName);
690689
}
691690

692-
auto setter = thiz->Get(context, v8::String::NewFromUtf8(isolate, setterMethodName.c_str()).ToLocalChecked()).ToLocalChecked();
691+
auto setter = thiz->Get(context, propertyCallbackData->setterName.Get(isolate)).ToLocalChecked();
693692
setter.As<Function>()->Call(context, thiz, 1, &value).ToLocalChecked();
694693
info.GetReturnValue().Set(value);
695694
} catch (NativeScriptException& e) {
@@ -709,7 +708,7 @@ void MetadataNode::SuperAccessorGetterCallback(const FunctionCallbackInfo<Value>
709708
try {
710709
auto isolate = info.GetIsolate();
711710
auto thiz = ResolveProxyReceiver(isolate, info.This(), "read", "super", true);
712-
auto key = ArgConverter::ConvertToV8String(isolate, "supervalue");
711+
auto key = V8StringConstants::GetSuperValuePrivate(isolate);
713712
Local<Value> hidenVal;
714713
V8GetPrivateValue(isolate, thiz, key, hidenVal);
715714
auto superValue = hidenVal.As<Object>();
@@ -980,7 +979,7 @@ void MetadataNode::SetInstanceMethodsFromStaticMetadata(
980979
setterMethodName = entry.getName();
981980
}
982981

983-
auto propertyInfo = cache->Own<PropertyCallbackData>(propertyName, getterMethodName, setterMethodName);
982+
auto propertyInfo = cache->Own<PropertyCallbackData>(isolate, propertyName, getterMethodName, setterMethodName);
984983
protoFiller.FillPrototypeProperty(isolate, propertyName, propertyInfo);
985984
}
986985
}
@@ -1444,7 +1443,7 @@ Persistent<Function>* MetadataNode::GetPersistentConstructorFunction(Isolate* is
14441443

14451444
MetadataNode::TypeMetadata* MetadataNode::GetTypeMetadata(Isolate* isolate, const Local<Function>& value) {
14461445
Local<Value> hiddenVal;
1447-
V8GetPrivateValue(isolate, value, String::NewFromUtf8(isolate, "typemetadata").ToLocalChecked(), hiddenVal);
1446+
V8GetPrivateValue(isolate, value, V8StringConstants::GetTypeMetadataPrivate(isolate), hiddenVal);
14481447
if (hiddenVal.IsEmpty() || !hiddenVal->IsExternal()) {
14491448
return nullptr;
14501449
}
@@ -1454,7 +1453,7 @@ MetadataNode::TypeMetadata* MetadataNode::GetTypeMetadata(Isolate* isolate, cons
14541453
}
14551454

14561455
void MetadataNode::SetTypeMetadata(Isolate* isolate, Local<Function> value, TypeMetadata* data) {
1457-
V8SetPrivateValue(isolate, value, String::NewFromUtf8(isolate, "typemetadata").ToLocalChecked(), External::New(isolate, data, v8::kExternalPointerTypeTagDefault));
1456+
V8SetPrivateValue(isolate, value, V8StringConstants::GetTypeMetadataPrivate(isolate), External::New(isolate, data, v8::kExternalPointerTypeTagDefault));
14581457
}
14591458

14601459
MetadataNode::InstanceData& MetadataNode::GetInstanceData(Isolate* isolate) {
@@ -1521,7 +1520,7 @@ void MetadataNode::ExtendedClassConstructorCallback(const v8::FunctionCallbackIn
15211520

15221521
SetInstanceMetadata(isolate, thiz, extData->node);
15231522
thiz->SetInternalField(static_cast<int>(ObjectManager::MetadataNodeKeys::CallSuper), True(isolate));
1524-
V8SetPrivateValue(isolate, thiz, V8StringConstants::GetImplementationObject(isolate), implementationObject);
1523+
V8SetPrivateValue(isolate, thiz, V8StringConstants::GetImplementationObjectPrivate(isolate), implementationObject);
15251524

15261525
ArgsWrapper argWrapper(info, ArgType::Class);
15271526

@@ -1591,7 +1590,7 @@ void MetadataNode::InterfaceConstructorCallback(const v8::FunctionCallbackInfo<v
15911590

15921591
implementationObject->SetPrototype(context, thiz->GetPrototype());
15931592
thiz->SetPrototype(context, implementationObject);
1594-
V8SetPrivateValue(isolate, thiz, V8StringConstants::GetImplementationObject(isolate), implementationObject);
1593+
V8SetPrivateValue(isolate, thiz, V8StringConstants::GetImplementationObjectPrivate(isolate), implementationObject);
15951594

15961595
ArgsWrapper argWrapper(info, ArgType::Interface);
15971596

@@ -1648,8 +1647,8 @@ void MetadataNode::MethodCallback(const v8::FunctionCallbackInfo<v8::Value>& inf
16481647

16491648
auto e = info.Data().As<External>();
16501649

1651-
auto callbackData = reinterpret_cast<MethodCallbackData*>(e->Value(v8::kExternalPointerTypeTagDefault));
16521650
auto initialCallbackData = reinterpret_cast<MethodCallbackData*>(e->Value(v8::kExternalPointerTypeTagDefault));
1651+
auto callbackData = initialCallbackData;
16531652

16541653
// Number of arguments the method is invoked with
16551654
int argLength = info.Length();
@@ -1772,7 +1771,7 @@ Local<Object> MetadataNode::GetImplementationObject(Isolate* isolate, const Loca
17721771
Local<Object> implementationObject;
17731772

17741773
Local<Value> hiddenVal;
1775-
V8GetPrivateValue(isolate, object, V8StringConstants::GetImplementationObject(isolate), hiddenVal);
1774+
V8GetPrivateValue(isolate, object, V8StringConstants::GetImplementationObjectPrivate(isolate), hiddenVal);
17761775
implementationObject = hiddenVal.As<Object>();
17771776

17781777
if (!implementationObject.IsEmpty()) {

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

Lines changed: 11 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -278,14 +278,20 @@ class MetadataNode {
278278
};
279279

280280
struct PropertyCallbackData {
281-
PropertyCallbackData(std::string _propertyName, std::string _getterMethodName, std::string _setterMethodName)
281+
PropertyCallbackData(v8::Isolate* isolate, const std::string& _propertyName, const std::string& getterMethodName, const std::string& setterMethodName)
282282
:
283-
propertyName(_propertyName), getterMethodName(_getterMethodName), setterMethodName(_setterMethodName) {
284-
283+
propertyName(_propertyName) {
284+
if (!getterMethodName.empty()) {
285+
getterName.Reset(isolate, v8::String::NewFromUtf8(isolate, getterMethodName.c_str(), v8::NewStringType::kInternalized).ToLocalChecked());
286+
}
287+
if (!setterMethodName.empty()) {
288+
setterName.Reset(isolate, v8::String::NewFromUtf8(isolate, setterMethodName.c_str(), v8::NewStringType::kInternalized).ToLocalChecked());
289+
}
285290
}
286291
std::string propertyName;
287-
std::string getterMethodName;
288-
std::string setterMethodName;
292+
// Empty when the property has no such accessor.
293+
v8::Global<v8::String> getterName;
294+
v8::Global<v8::String> setterName;
289295
};
290296

291297
struct ExtendedClassCallbackData {

‎test-app/runtime/src/main/cpp/V8StringConstants.cpp‎

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -52,6 +52,18 @@ Local<Private> V8StringConstants::GetNullObjectPrivate(Isolate* isolate) {
5252
return GetConstantsForIsolate(isolate)->NULL_OBJECT_PRIVATE.Get(isolate);
5353
}
5454

55+
Local<Private> V8StringConstants::GetImplementationObjectPrivate(Isolate* isolate) {
56+
return GetConstantsForIsolate(isolate)->IMPLEMENTATION_OBJECT_PRIVATE.Get(isolate);
57+
}
58+
59+
Local<Private> V8StringConstants::GetSuperValuePrivate(Isolate* isolate) {
60+
return GetConstantsForIsolate(isolate)->SUPER_VALUE_PRIVATE.Get(isolate);
61+
}
62+
63+
Local<Private> V8StringConstants::GetTypeMetadataPrivate(Isolate* isolate) {
64+
return GetConstantsForIsolate(isolate)->TYPE_METADATA_PRIVATE.Get(isolate);
65+
}
66+
5567
Local<String> V8StringConstants::GetIsPrototypeImplementationObject(Isolate* isolate) {
5668
auto consts = GetConstantsForIsolate(isolate);
5769

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

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -55,6 +55,12 @@ class V8StringConstants {
5555

5656
static v8::Local<v8::Private> GetNullObjectPrivate(v8::Isolate* isolate);
5757

58+
static v8::Local<v8::Private> GetImplementationObjectPrivate(v8::Isolate* isolate);
59+
60+
static v8::Local<v8::Private> GetSuperValuePrivate(v8::Isolate* isolate);
61+
62+
static v8::Local<v8::Private> GetTypeMetadataPrivate(v8::Isolate* isolate);
63+
5864
static const std::string CLASS_IMPLEMENTATION_OBJECT;
5965
static const std::string DEBUG_NAME;
6066
static const std::string EXTEND;
@@ -168,6 +174,11 @@ class V8StringConstants {
168174
CAST_MARKER_PRIVATE.Reset(isolate, Private::ForApi(isolate, str));
169175
str = String::NewFromUtf8(isolate, NULL_OBJECT_KEY.c_str(), NewStringType::kInternalized).ToLocalChecked();
170176
NULL_OBJECT_PRIVATE.Reset(isolate, Private::ForApi(isolate, str));
177+
IMPLEMENTATION_OBJECT_PRIVATE.Reset(isolate, Private::ForApi(isolate, Local<String>::New(isolate, *IMPLEMENTATION_OBJECT_PERSISTENT)));
178+
str = String::NewFromUtf8(isolate, "supervalue", NewStringType::kInternalized).ToLocalChecked();
179+
SUPER_VALUE_PRIVATE.Reset(isolate, Private::ForApi(isolate, str));
180+
str = String::NewFromUtf8(isolate, "typemetadata", NewStringType::kInternalized).ToLocalChecked();
181+
TYPE_METADATA_PRIVATE.Reset(isolate, Private::ForApi(isolate, str));
171182
};
172183

173184
~PerIsolateV8Constants() {
@@ -232,6 +243,9 @@ class V8StringConstants {
232243
v8::Global<v8::Private> NULL_NODE_PRIVATE;
233244
v8::Global<v8::Private> CAST_MARKER_PRIVATE;
234245
v8::Global<v8::Private> NULL_OBJECT_PRIVATE;
246+
v8::Global<v8::Private> IMPLEMENTATION_OBJECT_PRIVATE;
247+
v8::Global<v8::Private> SUPER_VALUE_PRIVATE;
248+
v8::Global<v8::Private> TYPE_METADATA_PRIVATE;
235249
};
236250

237251
private:

0 commit comments

Comments
 (0)