Skip to content

Add noncopyable JSUniqueRef with heap references skipping deduplication - #830

Merged
kateinoigakukun merged 1 commit into
mainfrom
maxd/jsuniqueref-pt1
Nov 28, 2026
Merged

kateinoigakukun merged 1 commit into
mainfrom
maxd/jsuniqueref-pt1

Conversation

@MaxDesiatov

@MaxDesiatov MaxDesiatov commented Nov 18, 2026 •

Copy link
Copy Markdown
Member

JSUniqueRef owns one JavaScript object reference and releases it when destroyed. Since #835 removed deduplication from the object heap, it needs no runtime support: it holds a heap reference exactly like JSObject, but as a noncopyable value with no Swift heap allocation.

JSObject now stores a JSUniqueRef, and init(_ ref: consuming JSUniqueRef) is its only designated initializer:

  • Releasing a reference, including routing the release to the owner thread, happens only in JSUniqueRef.deinit; JSObject.deinit is removed.
  • init(id:) remains as an SPI convenience initializer that adopts id, so existing callers and BridgeJS-generated code are unchanged.
  • JSBigInt and JSSymbol override init(_ ref:) instead of init(id:).
  • JSClosure and JSOneshotClosure keep keying host functions by ObjectIdentifier(self). They start from a placeholder reference of 0 and hand over the real reference with _replacePlaceholderRef(with:), which never releases the placeholder.

@_spi(BridgeJS) API: init(adopting:), id, and consuming func unsafeTake(), all using JavaScriptObjectRef.

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

sliemeobn commented Nov 18, 2026 •

Copy link
Copy Markdown
Contributor

I ran a js-framework-benchmark with ElementaryUI locally comparing current main with a version adopting JSUniqueRef (both PRs combined):

Total is the time from the click to the end of the next paint (this is what the benchmark compares)
Script is the main-thread JS/Wasm time inside that window (including the DOM calls it makes), taken from the Chrome trace

Benchmark Total: main → branch Script: main → branch
01 create 1k 24.74 → 24.23 ms (−2.0%) 6.73 → 6.27 ms (−6.8%)
02 replace 1k 30.18 → 29.79 ms (−1.3%) 11.80 → 11.20 ms (−5.1%)
03 partial update 17.34 → 16.92 ms (−2.4%) 3.05 → 2.97 ms (−2.8%)
04 select row 5.73 → 5.32 ms (−7.1%) 2.02 → 1.95 ms (−3.6%)
05 swap rows 18.07 → 17.83 ms (−1.4%) 2.61 → 2.16 ms (−17%)
06 remove row 11.83 → 11.84 ms (+0.1%) 1.13 → 0.97 ms (−14%)
07 create 10k 260.85 → 255.07 ms (−2.2%) 66.84 → 60.33 ms (−9.7%)
08 append 1k 30.18 → 29.81 ms (−1.2%) 7.55 → 7.04 ms (−6.7%)
09 clear 1k 16.09 → 14.70 ms (−8.7%) 13.28 → 11.93 ms (−10.1%)
Memory: ready 0.86 → 0.87 MB (+1.6%)
Memory: run 1k 2.92 → 2.70 MB (−7.4%)
Memory: run + clear 1.14 → 1.16 MB (+1.6%)
Comment thread Runtime/test/object-heap.test.ts Resolved
Comment thread Runtime/test/object-heap.test.ts Resolved
Comment thread Runtime/test/object-heap.test.ts Resolved
@kateinoigakukun

Copy link
Copy Markdown
Member

I'd like to introduce #835 before this PR because it simplifies a lot of complexities this PR introduces (e.g. flag for deduplication, JSObject can be just a class holding unique ref, and conversion from unique ref to JSObject can be just a member init without no raw id takeover, etc...)

`JSUniqueRef` owns one JavaScript object reference and releases it in its
`deinit`. Since the object heap no longer deduplicates references (#835), it
needs no runtime support: it holds a reference exactly like `JSObject` does,
without a Swift heap allocation.

`JSObject` now stores a `JSUniqueRef`, and `init(_ ref: consuming JSUniqueRef)`
is its only designated initializer. Releasing, including routing the release
to the owner thread, happens in one place, `JSUniqueRef.deinit`, and
`JSObject.deinit` is gone. `init(id:)` remains as a convenience initializer
that adopts `id`.

`JSClosure` and `JSOneshotClosure` still key their host functions by
`ObjectIdentifier(self)`, so they still start from a placeholder reference of
0. They now hand the real reference over with `_replacePlaceholderRef(with:)`,
which swaps it in and takes the placeholder out without releasing it.

Co-authored-by: Max Desiatov <m_desiatov@apple.com>
@kateinoigakukun
kateinoigakukun merged commit 1edf6b3 into main Nov 28, 2026
16 checks passed
@kateinoigakukun
kateinoigakukun deleted the maxd/jsuniqueref-pt1 branch November 28, 2026 02:15
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

enhancement New feature or request

4 participants