Skip to content

Commit ff36f6e

Browse files
authored
perf(runtime): cache where a masking canary's native property writes land (#600)
* perf(runtime): cache the native setter a canary's property write runs The masking canary setter looked a native property's name up along the prototype chain on every write before running its setter. It now caches the setter by the name written and the receiver's prototype. A hit rechecks that the receiver and every prototype below the setter's owner still lack the name, that each prototype up to the owner is the one the entry was made with, and that the owner still has an accessor whose setter was not forgotten. Native class prototypes get the per-name recheck too: a [[Set]] that starts elsewhere with such a prototype as its receiver defines a data property there without reaching its handlers. A write of a name with no entry reads one line of name tags and makes no V8 call. TrySetNativeProperty splits into FindNativeSetter and RunNativeSetter, so the setter can be cached. The handlers read the lifetime counters through the isolate's canary state, StateForWrite takes the canary its caller already has, and the flag that lets copies through the handlers is per isolate instead of per thread. getWrapperLifetimeStats() counts the hits as canaryCachedNativeWrites. * fix(runtime): count a cached native write only when its setter ran A cache entry whose NativeSetter was forgotten (JS assigned the name on the prototype that declares it) still passes NativeWriteCache::Check, since the owner keeps its accessor. RunNativeSetter then declines and the write takes the slow path, but canaryCachedNativeWrites had already been incremented. Increment it only after RunNativeSetter runs the setter, and cover the forgotten-setter sequence with a spec.
1 parent 77e9a79 commit ff36f6e

13 files changed

Lines changed: 839 additions & 79 deletions

‎NativeScript/runtime/MetadataBuilder.h‎

Lines changed: 17 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,7 @@
1212

1313
namespace tns {
1414

15+
struct NativeSetter;
1516
struct NativeSetters;
1617

1718
class MetadataBuilder {
@@ -32,18 +33,21 @@ class MetadataBuilder {
3233
void* data,
3334
StructStorage storage,
3435
v8::Local<v8::Object> parent);
35-
// Runs the setter of `owner`'s native property `property` on `receiver`, as
36-
// the property's accessor would, and returns true. Returns false, having
37-
// run nothing, unless `owner` is a native class's prototype, still holding
38-
// the accessor pair it was built with for `property`, which has a setter.
39-
// The caller establishes that `owner` is where `receiver`'s prototype chain
40-
// finds `property`. Only canary mode records the accessors this needs.
41-
static bool TrySetNativeProperty(v8::Isolate* isolate,
42-
v8::Local<v8::Context> context,
43-
v8::Local<v8::Object> owner,
44-
v8::Local<v8::Name> property,
45-
v8::Local<v8::Object> receiver,
46-
v8::Local<v8::Value> value);
36+
// Canary mode: the setter of `owner`'s native property `property`, when
37+
// `owner` is a native class's prototype still holding the accessor pair it
38+
// was built with for `property`, which has a setter; null otherwise. The
39+
// caller establishes that `owner` is where a receiver's prototype chain
40+
// finds `property`. The result stays valid for the isolate's lifetime.
41+
static NativeSetter* FindNativeSetter(v8::Isolate* isolate,
42+
v8::Local<v8::Context> context,
43+
v8::Local<v8::Object> owner,
44+
v8::Local<v8::Name> property);
45+
// Runs `setter` on `receiver`, as the property's accessor would, and
46+
// returns true; false, having run nothing, once JS has assigned or defined
47+
// the property on its prototype since FindNativeSetter.
48+
static bool RunNativeSetter(v8::Isolate* isolate, NativeSetter* setter,
49+
v8::Local<v8::Object> receiver,
50+
v8::Local<v8::Value> value);
4751
// Where the native member guard is on (GuardsNativeMembers): turns each
4852
// of `names`, and Symbol.iterator with `iterator`, an own method of
4953
// `prototype`, a prototype level of a JS-extended class, that native code
@@ -217,6 +221,7 @@ class MetadataBuilder {
217221
Class klass_ = nil;
218222
};
219223

224+
friend struct NativeSetter;
220225
friend struct NativeSetters;
221226

222227
static void SetNativeProperty(v8::Isolate* isolate,

‎NativeScript/runtime/MetadataBuilder.mm‎

Lines changed: 28 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -89,30 +89,34 @@ bool ResolveProxyReceiver(Isolate* isolate, Local<Object>& receiver, const char*
8989
constexpr int32_t kStructFieldIndexMask = (1 << kStructViewSlotShift) - 1;
9090
} // namespace
9191

92+
// One native property setter a native class's prototype was built with
93+
// (NativeSetters). Lives as long as the isolate; `item` is emptied for good
94+
// once JS changes the name on that prototype.
95+
struct NativeSetter {
96+
Global<v8::Name> name;
97+
MetadataBuilder::CacheItem<PropertyMeta>* item;
98+
};
99+
92100
// The native property setters a native class's prototype was built with, for
93-
// TrySetNativeProperty. JS can replace one only by defining the name on that
101+
// FindNativeSetter. JS can replace one only by defining the name on that
94102
// prototype, which its definer sees, and a name the prototype's own handlers
95103
// see defined or assigned is forgotten for good. A deleted accessor needs no
96104
// such care: the prototype then no longer has the property.
97105
struct NativeSetters {
98-
struct Entry {
99-
Global<v8::Name> name;
100-
MetadataBuilder::CacheItem<PropertyMeta>* item;
101-
};
102-
103106
// In the order RegisterInstanceProperties installed them: a later accessor
104107
// of a name replaces the earlier one on the prototype.
105108
std::vector<std::pair<std::string, MetadataBuilder::CacheItem<PropertyMeta>*>> installed;
106109
// By the name's identity hash. Names reaching an interceptor are internalized,
107-
// so a lookup compares identities.
108-
robin_hood::unordered_map<int, std::vector<Entry>> byHash;
110+
// so a lookup compares identities. Filled once by Index: entries never move
111+
// afterwards, as FindNativeSetter hands out pointers to them.
112+
robin_hood::unordered_map<int, std::vector<NativeSetter>> byHash;
109113

110-
Entry* Find(Local<v8::Name> name) {
114+
NativeSetter* Find(Local<v8::Name> name) {
111115
auto it = byHash.find(name->GetIdentityHash());
112116
if (it == byHash.end()) {
113117
return nullptr;
114118
}
115-
for (Entry& entry : it->second) {
119+
for (NativeSetter& entry : it->second) {
116120
if (entry.name == name) {
117121
return &entry;
118122
}
@@ -127,7 +131,7 @@ void Index(Isolate* isolate) {
127131
v8::String::NewFromUtf8(isolate, jsName.c_str(), NewStringType::kInternalized)
128132
.ToLocalChecked();
129133
MetadataBuilder::CacheItem<PropertyMeta>* setter = item->meta_->hasSetter() ? item : nullptr;
130-
if (Entry* entry = Find(name)) {
134+
if (NativeSetter* entry = Find(name)) {
131135
entry->item = setter;
132136
} else if (setter != nullptr) {
133137
byHash[name->GetIdentityHash()].push_back({Global<v8::Name>(isolate, name), setter});
@@ -142,7 +146,7 @@ void Forget(Isolate* isolate, Local<v8::Name> name) {
142146
if (it == byHash.end()) {
143147
return;
144148
}
145-
for (Entry& entry : it->second) {
149+
for (NativeSetter& entry : it->second) {
146150
if (entry.name.Get(isolate)->StrictEquals(name)) {
147151
entry.item = nullptr;
148152
}
@@ -1596,18 +1600,23 @@ void CollectInstanceMemberNames(const BaseClassMeta* meta, Class klass, bool own
15961600
return method;
15971601
}
15981602

1599-
bool MetadataBuilder::TrySetNativeProperty(Isolate* isolate, Local<Context> context,
1600-
Local<Object> owner, Local<v8::Name> property,
1601-
Local<Object> receiver, Local<Value> value) {
1603+
NativeSetter* MetadataBuilder::FindNativeSetter(Isolate* isolate, Local<Context> context,
1604+
Local<Object> owner, Local<v8::Name> property) {
16021605
NativeSetters* setters = NativeSettersOf(isolate, context, owner);
16031606
if (setters == nullptr) {
1604-
return false;
1607+
return nullptr;
16051608
}
1606-
NativeSetters::Entry* entry = setters->Find(property);
1607-
if (entry == nullptr || entry->item == nullptr) {
1609+
NativeSetter* entry = setters->Find(property);
1610+
return entry != nullptr && entry->item != nullptr ? entry : nullptr;
1611+
}
1612+
1613+
bool MetadataBuilder::RunNativeSetter(Isolate* isolate, NativeSetter* setter,
1614+
Local<Object> receiver, Local<Value> value) {
1615+
CacheItem<PropertyMeta>* item = setter->item;
1616+
if (item == nullptr) {
16081617
return false;
16091618
}
1610-
SetNativeProperty(isolate, entry->item, receiver, value);
1619+
SetNativeProperty(isolate, item, receiver, value);
16111620
return true;
16121621
}
16131622

‎NativeScript/runtime/NsBuiltinModules.cpp‎

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -268,6 +268,7 @@ void GetWrapperLifetimeStatsCallback(const FunctionCallbackInfo<Value>& info) {
268268
{"canaryStates", stats->canaryStates},
269269
{"canaryNativeDeaths", stats->canaryNativeDeaths},
270270
{"canaryInterceptedWrites", stats->canaryInterceptedWrites},
271+
{"canaryCachedNativeWrites", stats->canaryCachedNativeWrites},
271272
};
272273
for (const auto& [name, value] : fields) {
273274
result

‎NativeScript/runtime/ObjectManager.h‎

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -104,6 +104,9 @@ class ObjectManager {
104104
uint64_t canaryNativeDeaths = 0;
105105
// Canary mode: writes the canary handlers saw.
106106
uint64_t canaryInterceptedWrites = 0;
107+
// Canary mode: of those, native property writes whose setter the write
108+
// handler found in its cache rather than along the prototype chain.
109+
uint64_t canaryCachedNativeWrites = 0;
107110
};
108111
static LifetimeStats* GetLifetimeStats(v8::Isolate* isolate);
109112

0 commit comments

Comments
 (0)