Skip to content

Add JSUniqueRef for bridging to non-copyable types - #829

Open
MaxDesiatov wants to merge 1 commit into
mainfrom
maxd/jsuniqueref
Open

MaxDesiatov wants to merge 1 commit into
mainfrom
maxd/jsuniqueref

Conversation

@MaxDesiatov

@MaxDesiatov MaxDesiatov commented Nov 13, 2026 •

Copy link
Copy Markdown
Member

A ~Copyable @JSClass struct stores a noncopyable JSUniqueRef, and generated bindings take these wrappers borrowing. In the new Imported object benchmarks, creating or returning a noncopyable wrapper takes 15-26% less time than a copyable wrapper on main.

Median ns per iteration:

┌──────────────────────────┬──────────────────┬─────────────┬────────┐
│           Case           │ Copyable on main │ Noncopyable │ Change │
├──────────────────────────┼──────────────────┼─────────────┼────────┤
│ create and drop          │ 71.1             │ 52.5        │ -26.2% │
├──────────────────────────┼──────────────────┼─────────────┼────────┤
│ create, call and drop    │ 80.5             │ 62.8        │ -22.0% │
├──────────────────────────┼──────────────────┼─────────────┼────────┤
│ return and drop          │ 71.3             │ 58.4        │ -18.1% │
├──────────────────────────┼──────────────────┼─────────────┼────────┤
│ return and pass          │ 82.8             │ 70.7        │ -14.7% │
├──────────────────────────┼──────────────────┼─────────────┼────────┤
│ pass an existing wrapper │ 16.4             │ 16.4        │ -0.3%  │
└──────────────────────────┴──────────────────┴─────────────┴────────┘

Arrays of JSUniqueRef-values are bridged as UniqueArray, which is made conditionally available when using Swift 6.4. No dictionary counterpart is available, those either require users to manually add and bridge using swift-collections dependency that has UniqueDictionary, or bridging those as usual copyable values backed by copyable Dictionary.

@MaxDesiatov MaxDesiatov added the enhancement New feature or request label Nov 13, 2026
@MaxDesiatov

Copy link
Copy Markdown
Member Author

cc @sliemeobn

@kateinoigakukun kateinoigakukun left a comment

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Can you split PRs into series, one for adding JSUniqueRef, one for supporting @JSClass struct C: ~Copyable? I haven't checked the details of BridgeJS and TS2Swift changes but it seems like containing a lot of the new things.

Also allowing the JSObject -> JSUniqueRef conversion looks like a soundness hole? The opposite conversion (JSUniqueRef -> JSObject) should be allowed, but the "takeover" from JSObject to JSUniqueRef looks wrong because we cannot ban the further JSObject usage after the takeover?

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

enhancement New feature or request

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants