Repository navigation
feat(runtime): weak collections key native objects by lifetime in canary mode - #563
Draft
edusperoni wants to merge 3 commits into
Draft
edusperoni wants to merge 3 commits into
edusperoni wants to merge 3 commits into
Conversation
…otype Arrays returned from native methods got a per-instance Symbol.iterator function. Installed once on NSArray's prototype template instead, every array wrapper inherits it, whichever path built it, and none carries an own property: under the canary wrapper lifetime mode that property was state, one state object and associated token per array.
…ary mode In canary mode a native object's wrapper dies with its JS references while the object lives on, so WeakMap/WeakSet entries, WeakRef targets and FinalizationRegistry registrations keyed on the wrapper were lost with it. A canary-only builtin replaces the four globals with subclasses that key a native object by an opaque per-object key (frozen, null-prototype), held by the object's state and so alive exactly until the object is deallocated; WeakRef.prototype.deref (and the legacy get) return its live wrapper, a new one, or undefined once the object is gone. Values that are not canaries pass through unchanged, and other modes keep the built-ins. The key and its reverse lookup (a private on the key naming the object, checked against the registry, then an ObjC weak load so a deallocating object is never retained) are internal to the builtin.
|
Important Draft PR not reviewedDraft PRs are not automatically reviewed by default.
To automatically review draft PRs, update your CodeRabbit configuration: reviews:
auto_review:
drafts: true
Thanks for using CodeRabbit! It's free for OSS, and your support helps us grow. If you like it, consider giving us a shout-out. |
This branch has not been deployed
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
Follow-up to #561, the canary wrapper lifetime mode. With #561 alone, a native object's canary dies with its JS references while the object lives on. A later wrapper for the same object is a new JS object, so
WeakMap/WeakSetentries,WeakReftargets andFinalizationRegistryregistrations keyed on the old wrapper are lost with it.Weak collections
In canary mode a builtin (
js/weak-collections.js, run fromWeakRef.cpp) replaces the four globals with subclasses:WeakRef.prototype.deref(and the legacygetalias) returns the live wrapper, a new one for the same object, orundefinedonce the object is gone.set/add. Other modes keep V8's built-ins.The key and its reverse lookup are internal to the builtin and not exported from
ns:interop. The reverse lookup is a private on the key naming the object, checked against the canary registry, then an ObjC weak load (objc_loadWeakRetained), so a deallocating object is never retained. Exposing them would commit every runtime to the API, so that waits for a concrete use case.Symbol.iteratoron NSArrayArrays returned from native methods used to get a per-instance
Symbol.iteratorfunction. Under canary that own property was state: one state object, plus an associated token or weak reference, per array. It's now installed once on NSArray's prototype template. Every array wrapper inherits it, whichever path built it, and none carries an own property. Measured in a Release simulator build, 5 alternating runs per mode:NSArray.arrayWithArray([{a: 1}])went from 4369 to 2622 ns inresurrecting(about 2450 ns in canary).canary/allwent from 4176 to 2362 ns per JS-only wrapper, now belowresurrecting's 2793.Testing
WrapperLifetimeWeakCollectionsTests.js, which holds in every mode:WeakMapandWeakSetacross a wrapper change;WeakRefderef while the object lives, including thegetalias;WeakRefcleared after native death;FinalizationRegistrycallback after native death;v8-14.9.207.39-10prebuilts, full suite, 0 failures (1905 specs), in each ofcanary/all,canary/extended,resurrecting/extendedandrooted/all; ASan,canary/all: 0 failures, no reports. Each run's log was checked for the mode it exercised.WeakMap.get.