Repository navigation
perf(runtime): weak collections skip native calls for plain keys and live WeakRef targets - #597
Draft
edusperoni wants to merge 1 commit into
Draft
edusperoni wants to merge 1 commit into
edusperoni wants to merge 1 commit into
Conversation
…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.
|
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.
Stacked on #595. Canary-mode weak collections make fewer native calls: plain JS keys and live
WeakReftargets no longer cross into C++. No user-visible behaviour change.Changes
unwrapreturns at once for an object without internal fields (or a Proxy). No canary or-deallocreceiver lacks them. AWeakMap/WeakSet/FinalizationRegistrymiss on a plain object used to read a private property twice to find it was no wrapper.WeakRef.prototype.deref: eachWeakRefkeeps a nativeWeakRefto the canary it was made with or last dereferenced to, and returns it while that canary is alive and an epoch is unchanged.__releaseNativeCounterpart, and when a record dies under a live canary (its associations removed).Uint32Arraywith atomics, owned by the registry and the buffer's deleter. It holds no V8 handles.WeakRefwhose target is not a lifetime key returns it without a native call.WeakMap/WeakSetstill ask native code every time. A JS-side canary → key cache (Android's design) was built and measured. Hits got about 30 ns faster, but caching a freshly built canary cost about 150 ns. On jitless iOS a nativeunwrapcosts only about 30 ns, so a short-lived wrapper never recovers it.JSWeakRefcleared in minor and major GCs, and the only strong hold isWeakRef's standard end-of-job keep-alive.Measured
Release simulator build (iPhone 16 Pro, iOS 26.3), canary/all, ns/op, median of 3 interleaved runs:
ref.deref()(1 ref / 4000 refs)derefagain after a rebuild, same jobgetmissset+deletenew WeakRef(o).deref()WeakMapget / has / setderefof a dead canary (rebuild)The dead-canary row is 12% slower: the cache now keeps a young native
WeakRefon an olderWeakRefinstance.Specs
dereffollow.derefdoesn't return a wrapper of a released object it returned before the release.objc_removeAssociatedObjects.-deallocreceiver finds what was keyed on its object.Removing the release epoch bump fails three specs.
Testing
2209 specs, 0 failures in
canary/all,canary/extended,resurrecting/extended, andcanary/allunder ASan.