Skip to content

perf(runtime): run native property setters from the canary write handler - #578

Draft
edusperoni wants to merge 1 commit into
feat/wrapper-lifetime-canaryfrom
perf/canary-native-setters
Draft

edusperoni wants to merge 1 commit into
feat/wrapper-lifetime-canaryfrom
perf/canary-native-setters

Conversation

@edusperoni

Copy link
Copy Markdown
Collaborator

Stacked on #561 (canary wrapper lifetime mode), as its own PR so it can be reverted on its own after the squash merge. It makes native ObjC property writes on canaries cheaper; nothing else changes.

Current behavior

In canary mode, a write to a native property on a canary (view.tag = 1 on an instance of a JS-extended class, or on any wrapper under "scope": "all") goes through the canary write handler first. The handler classifies the name with a prototype-chain walk, finds a native accessor, declines, and V8 then redoes the store lookup and calls the accessor's setter function from C++. That costs about 2.5–3× a native write in resurrecting.

New behavior

The write handler runs the native setter itself when the accessor its walk found is provably the one MetadataBuilder installed:

  • What it knows: in canary mode, RegisterInstanceProperties records each settable native property it installs. The native class prototype carries the table under a private key: internalized name → PropertyMeta, last install wins.
  • When it uses it: only when the owner the walk found (the first object on the chain with a real property of that name) is a native prototype with the name in its table, and the property is an accessor. A JS accessor or data property anywhere below it (on the instance, a JS class prototype, or added later) is found first and wins, exactly as today.
  • Invalidation: native prototypes already carry a masking setter and definer interceptor (the swizzle handlers). Both now forget a name for good, before anything else, once JS assigns or defines it on that prototype. defineProperty, defineProperties, Reflect.defineProperty and __defineSetter__ all reach the definer. A deleted accessor stops being found by the walk, re-adding the name by assignment makes it a data property (not an accessor), and re-adding an accessor needs a define, which the definer sees.
  • Same setter code: the native accessor's setter callback was split into SetNativeProperty (read-only error, proxy receiver resolution, InvokeMethod), and the fast path calls that same function. Dispatch, KVO, swizzle aliases and exceptions are identical by construction.

Read-only native properties aren't in the table, so V8 still rejects them; setter-less properties keep today's path.

V8 invariant to recheck on upgrades: this assumes the only JS route to a new accessor on a native prototype is a define that reaches that prototype's definer.

Performance

Release, simulator, 5 alternating rounds per configuration, medians in ns, against #561:

Case canary/extended before → after canary/all before → after
JS-extended: native property write, 2 prototype levels up 324 → 289 323 → 292
JS-extended UIButton: tag write, 4 levels up 396 → 324 392 → 334
Plain object: native property write 126 → 123 271 → 251
Plain UIButton: tag write 115 → 115 322 → 266

Every other row (native calls and reads, expando reads and writes, misses, first write of a new name, creation, re-reading after GC, full GC) is within noise. resurrecting is unchanged.

Not measured: the memory of the tables (one Global<Name> per settable native property per class touched, per isolate). An error thrown by a setter through the fast path may lack the native accessor's API frame in error.stack.

Testing

  • The specs pinning native property setters on canaries are in feat(runtime): canary wrapper lifetime mode #561 (write handler › native property setters). They pass with and without this change: sloppy and strict writes, an ObjC exception from a setter, read-only rejection, a JS override, an accessor added later on the JS prototype or on the instance, the four routes that redefine a member on the native prototype, delete and re-add there, a Proxy prototype, and a setter override exposed to native code.
  • With the invalidation turned into a no-op, canary/all fails 8 suites, including the existing swizzle and JS-accessor-encoding suites, so the specs do cover it.
  • Full suite, 0 failures (2107 specs), in each of resurrecting/extended, canary/all, canary/extended and rooted/all; ASan, canary/all: 0 failures, no reports.

A native property write on a canary went through the write handler, which
declined, and then through V8's own store lookup and its call of the
accessor's setter function. The handler now runs the native setter itself
when the accessor its prototype walk found is provably the one MetadataBuilder
installed:

- in canary mode each native class prototype carries, under a private key,
  the PropertyMeta setters it was built with;
- the prototype's own setter and definer interceptors forget a name for good
  once JS assigns or defines it on that prototype, so a redefined accessor is
  never mistaken for the native one;
- the owner must be the first object of the chain with the name, so a JS
  accessor or data property anywhere below it still wins.

The setter runs through the same code as the accessor's callback
(SetNativeProperty), so dispatch, proxy handling and exceptions are
unchanged.
@coderabbitai

coderabbitai Bot commented Nov 17, 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