From 56b4005ac10ae8764e466f380321b7e9de004146 Mon Sep 17 00:00:00 2001 From: Eduardo Speroni Date: Sat, 21 Nov 2026 19:19:54 -0300 Subject: [PATCH 1/2] perf(runtime): weak collections skip native calls for plain keys and live WeakRef targets In canary mode a WeakMap, WeakSet or FinalizationRegistry miss on a plain JS object asked native code for a lifetime key, and that native lookup read a private property twice to find the object was no wrapper. unwrap now returns at once for an object without internal fields, which no canary or -dealloc receiver is. WeakRef.prototype.deref asked native code for the wrapper on every call. A WeakRef now keeps a NativeWeakRef to the canary it was made with or last dereferenced to, and hands it back while it lives and the epoch native code bumps is unchanged: __releaseNativeCounterpart bumps it, as does a record dying under a live canary (its associations removed). A WeakRef whose target is not a lifetime key returns it without a native call. Weak collection lookups of canaries still ask native code every time. A JS-side canary -> key cache was measured: hits got about 30 ns faster, but caching a freshly built canary cost about 150 ns, more than a short-lived wrapper recovers. --- NativeScript/runtime/WrapperLifetimeCanary.h | 6 +- NativeScript/runtime/WrapperLifetimeCanary.mm | 78 ++++++++-- NativeScript/runtime/js/weak-collections.js | 53 ++++++- .../WrapperLifetimeWeakCollectionsTests.js | 141 ++++++++++++++++++ 4 files changed, 257 insertions(+), 21 deletions(-) diff --git a/NativeScript/runtime/WrapperLifetimeCanary.h b/NativeScript/runtime/WrapperLifetimeCanary.h index 150fbcee..8a068e31 100644 --- a/NativeScript/runtime/WrapperLifetimeCanary.h +++ b/NativeScript/runtime/WrapperLifetimeCanary.h @@ -101,9 +101,11 @@ class WrapperLifetimeCanary { // anew, as it does any result whose kind differs from the template's. static CanaryKind NativeInstancesKind(); - // Adds `unwrap` (a canary's native object -> its weak-collection key) and + // Adds `unwrap` (a canary's native object -> its weak-collection key), // `wrap` (key -> live or new canary, or undefined once the object is gone) - // to `binding`, for the weak-collections builtin. Canary mode only. + // and `cache` (the Uint32Array that tells the builtin when a wrapper it + // cached from wrap is stale) to `binding`, for the weak-collections builtin. + // Canary mode only. static bool RegisterWrapFunctions(v8::Local context, v8::Local binding); diff --git a/NativeScript/runtime/WrapperLifetimeCanary.mm b/NativeScript/runtime/WrapperLifetimeCanary.mm index aabc3fed..79c62508 100644 --- a/NativeScript/runtime/WrapperLifetimeCanary.mm +++ b/NativeScript/runtime/WrapperLifetimeCanary.mm @@ -89,6 +89,22 @@ bool ObjectAlive() const { namespace { +// What the weak-collections builtin reads through a Uint32Array over `slots` +// (RegisterWrapFunctions). It drops every wrapper it cached from wrap once +// kEpoch changes, which can happen on any thread. After an unwrap that asked +// (UnwrapCallback), kLiveCanary is 1 when the value is a live canary standing +// for the key it answered: the wrapper wrap answers with for that key. +struct WeakCollectionCache { + enum Slot { kEpoch, kLiveCanary, kSlotCount }; + std::atomic slots[kSlotCount]; + + // A wrapper wrap answered may no longer be the one it answers. + void Invalidate() { slots[kEpoch].fetch_add(1, std::memory_order_release); } + void SetLiveCanary(bool live) { + slots[kLiveCanary].store(live ? 1 : 0, std::memory_order_relaxed); + } +}; + // Per-isolate. Shared with the tokens associated with native objects, which // can be deallocated on any thread and after the isolate is gone. struct CanaryRegistry { @@ -105,6 +121,9 @@ bool ObjectAlive() const { bool sweepPosted = false; // Lets wrapper creation skip the lookup while no object has state. std::atomic count{0}; + // Outlives the registry while the builtin's array does. + std::shared_ptr weakCollectionCache = + std::make_shared(); Isolate* isolate = nullptr; std::weak_ptr loop; #if NS_CANARY_DEATH_SIGNAL == NS_CANARY_DEATH_ASSOCIATION @@ -197,6 +216,12 @@ void NativeDied(const std::shared_ptr& registry, void* object, // Erased now, inside the object's -dealloc: an object later allocated at // this address must not find this record. it->second->died.store(true, std::memory_order_release); + // A record held outside the registry and not dying is a live canary's (its + // associations were removed): wrap no longer answers with that canary. + // A transient copy elsewhere bumps it spuriously, which only costs a refill. + if (!it->second->dying.load(std::memory_order_acquire) && it->second.use_count() > 1) { + registry->weakCollectionCache->Invalidate(); + } registry->dead.push_back(std::move(it->second)); registry->records.erase(it); registry->count.fetch_sub(1, std::memory_order_relaxed); @@ -989,21 +1014,30 @@ void PostSweepAfterGC(Isolate* isolate, GCType, GCCallbackFlags) { } #endif -// unwrap(value, create): the weak collections' key for a canary's native -// object, a frozen, null-prototype object that lives as long as the object's -// state, that is, until the native object is deallocated, or as long as a -// released canary of the object holding a copy. Lookups pass -// `create` false and never make one: with no key yet, no entry can be keyed by -// it. The value itself for anything that is not a canary, a Proxy included (it -// is a key of its own, as in every other mode). +// unwrap(value, create, reportLive): the weak collections' key for a canary's +// native object, a frozen, null-prototype object that lives as long as the +// object's state, that is, until the native object is deallocated, or as long +// as a released canary of the object holding a copy. Lookups pass `create` +// false and never make one: with no key yet, no entry can be keyed by it. The +// value itself for anything that is not a canary, a Proxy included (it is a key +// of its own, as in every other mode). With `reportLive`, also sets +// WeakCollectionCache::kLiveCanary. void UnwrapCallback(const FunctionCallbackInfo& info) { Isolate* isolate = info.GetIsolate(); Local value = info[0]; bool create = info[1]->IsTrue(); + bool reportLive = info[2]->IsTrue(); info.GetReturnValue().Set(value); CanaryState* canaryState = Caches::StateFor(isolate); Local state; - if (canaryState == nullptr || !value->IsObject() || value->IsProxy()) { + if (reportLive && canaryState != nullptr) { + canaryState->registry->weakCollectionCache->SetLiveCanary(false); + } + // Canaries and -dealloc receivers have internal fields; for any other object + // the wrapper lookups below would each read a private property to find no + // canary. + if (canaryState == nullptr || !value->IsObject() || value->IsProxy() || + value.As()->InternalFieldCount() == 0) { return; } Local context = isolate->GetCurrentContext(); @@ -1013,8 +1047,7 @@ void UnwrapCallback(const FunctionCallbackInfo& info) { : !ExistingState(isolate, value.As(), &state)) { // A released canary keeps its key itself, or null when it had none // (KeepWeakCollectionKey). - if (value.As()->InternalFieldCount() == 0 || - !value.As()->GetPrivate(context, keyOfState).ToLocal(&key)) { + if (!value.As()->GetPrivate(context, keyOfState).ToLocal(&key)) { return; } if (key->IsObject()) { @@ -1032,6 +1065,10 @@ void UnwrapCallback(const FunctionCallbackInfo& info) { return; } if (state->GetPrivate(context, keyOfState).ToLocal(&key) && key->IsObject()) { + if (reportLive) { + canaryState->registry->weakCollectionCache->SetLiveCanary( + CanaryOf(isolate, value.As()) != nullptr); + } info.GetReturnValue().Set(key); return; } @@ -1060,6 +1097,9 @@ void UnwrapCallback(const FunctionCallbackInfo& info) { !state->SetPrivate(context, keyOfState, newKey).FromMaybe(false)) { return; } + if (reportLive) { + canaryState->registry->weakCollectionCache->SetLiveCanary(true); + } info.GetReturnValue().Set(newKey); } @@ -1158,10 +1198,24 @@ void WrapCallback(const FunctionCallbackInfo& info) { Isolate* isolate = v8::Isolate::GetCurrent(); Local unwrap; Local wrap; + CanaryState* state = Caches::StateFor(isolate); + if (state == nullptr) { + return false; + } + auto* owner = new std::shared_ptr(state->registry->weakCollectionCache); + std::unique_ptr store = ArrayBuffer::NewBackingStore( + (*owner)->slots, sizeof((*owner)->slots), + [](void*, size_t, void* data) { + delete static_cast*>(data); + }, + owner); + Local cache = v8::Uint32Array::New(ArrayBuffer::New(isolate, std::move(store)), + 0, WeakCollectionCache::kSlotCount); return v8::Function::New(context, UnwrapCallback).ToLocal(&unwrap) && v8::Function::New(context, WrapCallback).ToLocal(&wrap) && binding->Set(context, tns::ToV8String(isolate, "unwrap"), unwrap).FromMaybe(false) && - binding->Set(context, tns::ToV8String(isolate, "wrap"), wrap).FromMaybe(false); + binding->Set(context, tns::ToV8String(isolate, "wrap"), wrap).FromMaybe(false) && + binding->Set(context, tns::ToV8String(isolate, "cache"), cache).FromMaybe(false); } void WrapperLifetimeCanary::KeepWeakCollectionKey(Isolate* isolate, Local canary, @@ -1185,6 +1239,8 @@ void WrapCallback(const FunctionCallbackInfo& info) { copy = key; } } + // wrap hands out no wrapper of the object now. + canaryState->registry->weakCollectionCache->Invalidate(); // Private symbols bypass the canary's handlers. (void)canary->SetPrivate(context, keyOfState, copy); } diff --git a/NativeScript/runtime/js/weak-collections.js b/NativeScript/runtime/js/weak-collections.js index 4eb595c5..b2d1904d 100644 --- a/NativeScript/runtime/js/weak-collections.js +++ b/NativeScript/runtime/js/weak-collections.js @@ -11,7 +11,7 @@ // left as it is: Object.getPrototypeOf(WeakMap.prototype) is the native // prototype, and WeakMap.toString() is not native code. -const { unwrap, wrap } = binding; +const { unwrap, wrap, cache } = binding; const { FinalizationRegistry: NativeFinalizationRegistry, FinalizationRegistryPrototypeRegister, @@ -54,11 +54,18 @@ function canBeHeldWeakly(value) { // key (a class forbidding associated objects or refusing weak references, a // -dealloc receiver, a released wrapper that never had one), and unwrap then // returns the canary too. So every method first tries the value as is and -// unwraps it only when that misses: plain JS keys never reach native code. +// unwraps it only when that misses. + +// Slots of `cache` (WeakCollectionCache in WrapperLifetimeCanary.mm): EPOCH +// changes whenever a wrapper wrap answered may no longer be the one it +// answers; after unwrap(value, true, true), LIVE_CANARY says whether value is +// the wrapper wrap answers with for the key unwrap returned. +const EPOCH = 0; +const LIVE_CANARY = 1; // The lifetime key value stands for, or undefined when that is value itself. function otherKey(value) { - if (!isObject(value)) { + if ((typeof value !== "object" || value === null) && typeof value !== "function") { return undefined; } const key = unwrap(value, false); @@ -169,12 +176,26 @@ class WeakSet extends NativeWeakSet { } class WeakRef extends NativeWeakRef { + // Undefined when the target is not a lifetime key. Otherwise null, or a + // NativeWeakRef to the wrapper the WeakRef was made with or deref last + // returned, which wrap still answers with while the epoch is #epoch. + #wrapper = undefined; + #epoch = 0; + constructor(target) { - const key = isObject(target) ? unwrap(target, true) : target; + const now = cache[EPOCH]; + const key = isObject(target) ? unwrap(target, true, true) : target; + const live = key !== target && cache[LIVE_CANARY] !== 0; super(key); // The target itself, not only its key, stays alive until the job ends. if (key !== target) { - new NativeWeakRef(target); + const kept = new NativeWeakRef(target); + if (live) { + this.#wrapper = kept; + this.#epoch = now; + } else { + this.#wrapper = null; + } } } @@ -187,10 +208,26 @@ class WeakRef extends NativeWeakRef { if (!isObject(target)) { return target; } + if (!(#wrapper in this)) { + return wrap(target); + } + const last = this.#wrapper; + if (last === undefined) { + return target; + } + const now = cache[EPOCH]; + if (last !== null && this.#epoch === now) { + const live = WeakRefPrototypeDeref(last); + if (live !== undefined) { + return live; + } + } const result = wrap(target); - // The native deref already keeps a target it returns as is. - if (result !== target && result !== undefined) { - new NativeWeakRef(result); + if (result !== undefined) { + this.#wrapper = new NativeWeakRef(result); + this.#epoch = now; + } else { + this.#wrapper = null; } return result; } diff --git a/TestRunner/app/tests/WrapperLifetimeWeakCollectionsTests.js b/TestRunner/app/tests/WrapperLifetimeWeakCollectionsTests.js index 134b7ea5..f7e9386e 100644 --- a/TestRunner/app/tests/WrapperLifetimeWeakCollectionsTests.js +++ b/TestRunner/app/tests/WrapperLifetimeWeakCollectionsTests.js @@ -186,6 +186,147 @@ describe("Weak collections of native objects", function () { }); }); + // Uses every collection, and a deref, on a canary in an earlier job, so + // whatever they keep for it would still be there. + function keyEverywhere(obj, collections) { + collections.map.set(obj, "value"); + collections.set.add(obj); + collections.registry.register(obj, "held", obj); + var ref = new WeakRef(obj); + ref.deref(); + collections.map.get(obj); + collections.set.has(obj); + collections.refs.push(ref); + collections.wrappers.push(new WrapperWeakRef(obj)); + } + + function collections() { + return { + map: new WeakMap(), + set: new WeakSet(), + registry: new FinalizationRegistry(function () {}), + refs: [], + wrappers: [] + }; + } + + it("lets a keyed wrapper nothing references die and its JS-owned native object go", function (done) { + var watch = NSHashTable.weakObjectsHashTable(); + var keyed = collections(); + (function () { + var obj = Extended.new(); + watch.addObject(obj); + keyEverywhere(obj, keyed); + })(); + var attempts = 15; + (function poll() { + settle(1, function () { + var gone = watch.allObjects.count === 0; + if (gone || --attempts === 0) { + expect(gone).toBe(true); + if (canary) { + expect(keyed.wrappers[0].deref() === undefined).toBe(true); + expect(keyed.refs[0].deref() === undefined).toBe(true); + } + done(); + return; + } + poll(); + }); + })(); + }); + + it("lets a keyed canary nothing references die while native code owns its object", function (done) { + if (!canary) { + done(); + return; + } + var owner = NSMutableArray.new(); + var keyed = collections(); + (function () { + var obj = Extended.new(); + owner.addObject(obj); + keyEverywhere(obj, keyed); + })(); + settle(2, function () { + expect(keyed.wrappers[0].deref() === undefined).toBe(true); + var later = owner.objectAtIndex(0); + expect(keyed.map.get(later)).toBe("value"); + expect(keyed.set.has(later)).toBe(true); + expect(keyed.refs[0].deref() === later).toBe(true); + expect(keyed.registry.unregister(later)).toBe(true); + done(); + }); + }); + + it("dereferences no wrapper of a released object a WeakRef returned before the release", function () { + if (!canary) { + return; + } + var owner = NSMutableArray.new(); + var obj = Extended.new(); + owner.addObject(obj); + var ref = new WeakRef(obj); + expect(ref.deref() === obj).toBe(true); + __releaseNativeCounterpart(obj); + expect(ref.deref() === undefined).toBe(true); + var later = owner.objectAtIndex(0); + expect(ref.deref() === later).toBe(true); + }); + + // Only objects outside JS-extended classes carry the token the removal + // releases, and they are canaries only under scope "all". + it("keys a live object again after its associations are removed", function (done) { + if (!canary || runtime.getConfig("wrapperLifetimeScope") !== "all" || + typeof objc_removeAssociatedObjects !== "function") { + done(); + return; + } + var owner = NSMutableArray.new(); + var before = new WeakMap(); + var after = new WeakMap(); + (function () { + var obj = TNSAllocLog.new(); + owner.addObject(obj); + before.set(obj, "before"); + expect(before.get(obj)).toBe("before"); + objc_removeAssociatedObjects(obj); + after.set(obj, "after"); + expect(after.get(obj)).toBe("after"); + })(); + settle(2, function () { + expect(after.get(owner.objectAtIndex(0))).toBe("after"); + done(); + }); + }); + + it("finds what was keyed on an object through its JS dealloc's receiver", function (done) { + var map = new WeakMap(); + var seen = []; + var Keyed = TNSAllocLog.extend({ + dealloc: function () { + seen.push(map.get(this)); + this.super.dealloc(); + } + }, { name: "WrapperLifetimeWeakCollectionsDeallocGet" }); + var owner = NSMutableArray.new(); + (function () { + var obj = Keyed.new(); + map.set(obj, "value"); + owner.addObject(obj); + })(); + settle(2, function () { + owner.removeAllObjects(); + settle(2, function () { + expect(seen.length).toBe(1); + if (canary) { + expect(seen[0]).toBe("value"); + } + done(); + }); + }); + }); + it("keys a Proxy around a native object by the Proxy", function () { var obj = Extended.new(); var proxy = new Proxy(obj, {}); From 0f2d443c08a13de79ef0862dbf884c4ddf854c39 Mon Sep 17 00:00:00 2001 From: Eduardo Speroni Date: Sun, 22 Nov 2026 15:23:43 -0300 Subject: [PATCH 2/2] test(runtime): let the keyed-wrapper spec's probe drain before collecting The spec polled a weak NSHashTable through allObjects and collected right after, in the same callback. The array allObjects returns retains its objects until the autorelease pool drains, and under the "rooted" mode the major-GC prologue counts that retain as native ownership: each round's probe re-rooted the wrapper it was waiting for, so the object never went under rooted/all. The probe now releases the array's wrapper, and the next collection waits for the run loop to drain the pool. --- .../tests/WrapperLifetimeWeakCollectionsTests.js | 16 ++++++++++++++-- 1 file changed, 14 insertions(+), 2 deletions(-) diff --git a/TestRunner/app/tests/WrapperLifetimeWeakCollectionsTests.js b/TestRunner/app/tests/WrapperLifetimeWeakCollectionsTests.js index f7e9386e..a1ef0c1d 100644 --- a/TestRunner/app/tests/WrapperLifetimeWeakCollectionsTests.js +++ b/TestRunner/app/tests/WrapperLifetimeWeakCollectionsTests.js @@ -210,6 +210,17 @@ describe("Weak collections of native objects", function () { }; } + // The objects a weak table still holds. The array allObjects returns + // retains them until its wrapper is released and the autorelease pool + // drains; a "rooted" collection before that counts the retain as native + // ownership and roots their wrappers. + function watchedCount(table) { + var all = table.allObjects; + var count = all.count; + __releaseNativeCounterpart(all); + return count; + } + it("lets a keyed wrapper nothing references die and its JS-owned native object go", function (done) { var watch = NSHashTable.weakObjectsHashTable(); var keyed = collections(); @@ -221,7 +232,7 @@ describe("Weak collections of native objects", function () { var attempts = 15; (function poll() { settle(1, function () { - var gone = watch.allObjects.count === 0; + var gone = watchedCount(watch) === 0; if (gone || --attempts === 0) { expect(gone).toBe(true); if (canary) { @@ -231,7 +242,8 @@ describe("Weak collections of native objects", function () { done(); return; } - poll(); + // Collects only after the run loop drained the probe's array. + setTimeout(poll, 10); }); })(); });