Skip to content

perf(runtime): weak collections skip native calls for plain keys and live WeakRef targets - #597

Draft
edusperoni wants to merge 1 commit into
feat/canary-plain-forwardingfrom
perf/canary-weak-collections-cache
Draft

edusperoni wants to merge 1 commit into
feat/canary-plain-forwardingfrom
perf/canary-weak-collections-cache

Conversation

@edusperoni

Copy link
Copy Markdown
Collaborator

Stacked on #595. Canary-mode weak collections make fewer native calls: plain JS keys and live WeakRef targets no longer cross into C++. No user-visible behaviour change.

Changes

  • Plain JS keys: unwrap returns at once for an object without internal fields (or a Proxy). No canary or -dealloc receiver lacks them. A WeakMap/WeakSet/FinalizationRegistry miss on a plain object used to read a private property twice to find it was no wrapper.
  • WeakRef.prototype.deref: each WeakRef keeps a native WeakRef to the canary it was made with or last dereferenced to, and returns it while that canary is alive and an epoch is unchanged.
    • Native code bumps the epoch in __releaseNativeCounterpart, and when a record dies under a live canary (its associations removed).
    • The epoch lives in a shared Uint32Array with atomics, owned by the registry and the buffer's deleter. It holds no V8 handles.
    • A WeakRef whose target is not a lifetime key returns it without a native call.
  • Canary lookups in WeakMap/WeakSet still 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 native unwrap costs only about 30 ns, so a short-lived wrapper never recovers it.
  • Nothing new is pinned. There are no new ephemeron tables or Globals, the cached reference is a JSWeakRef cleared in minor and major GCs, and the only strong hold is WeakRef'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:

before after
canary ref.deref() (1 ref / 4000 refs) 155 / 259 90 / 136
deref again after a rebuild, same job 258 121
plain key get miss 186 71
plain key set + delete 311 137
plain new WeakRef(o).deref() 330 183
canary key WeakMap get / has / set 118 / 114 / 96 110 / 110 / 101
deref of a dead canary (rebuild) 881 1007

The dead-canary row is 12% slower: the cache now keeps a young native WeakRef on an older WeakRef instance.

Specs

  • A keyed, unreferenced wrapper dies, and its JS-owned native object deallocates (all modes).
  • A keyed canary dies while native code owns its object; entries and deref follow.
  • deref doesn't return a wrapper of a released object it returned before the release.
  • A live object is keyed again after objc_removeAssociatedObjects.
  • A JS -dealloc receiver 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, and canary/all under ASan.

…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.
@coderabbitai

coderabbitai Bot commented Nov 21, 2026

Copy link
Copy Markdown

Important

Draft PR not reviewed

Draft PRs are not automatically reviewed by default.

  • Trigger a manual review

To automatically review draft PRs, update your CodeRabbit configuration:

reviews:
  auto_review:
    drafts: true
  • Autofix · Keep fixing CodeRabbit findings and required CI, and resolving merge conflicts

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.

Share

This branch has not been deployed

No deployments
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant