Skip to content

Runtime: Stop deduplicating JSObject references in the object heap - #835

Merged
kateinoigakukun merged 1 commit into
mainfrom
katei/remove-jsobject-dedup
Nov 27, 2026
Merged

kateinoigakukun merged 1 commit into
mainfrom
katei/remove-jsobject-dedup

Conversation

@kateinoigakukun

@kateinoigakukun kateinoigakukun commented Nov 26, 2026 •

Copy link
Copy Markdown
Member

Overview

The object heap keeps a Map from each JS value to its slot, so every JSObject for the same value shares one id. Only JSObject.== and the Hashable conformance rely on that, and both can live without it. This PR removes the map: retain now always allocates a new slot, as wasm-bindgen's heap does.

1. Equality uses JS ===. JSObject.== calls a new swjs_value_strict_equals import instead of comparing ids. The existing swjs_value_equals uses loose ==, which could run user valueOf code when a JSObject is compared with a JSBigInt.

2. JSObject is no longer Hashable. A hash consistent with JS identity would need a value-to-id map, which is exactly the cost removed here. wasm-bindgen doesn't provide Hash for JsValue or js-sys types either.

3. The heap gets simpler and faster. retain no longer looks up or inserts into the map, and release no longer deletes from it.

Benchmarks

js-framework-benchmark (ElementaryUI)

ElementaryUI main built against JavaScriptKit main and this PR, with ElementaryUI's Benchmarks/PerformanceBenchmarks runner. Each value is the mean of 2 rounds, each a median of 10 runs (3 for memory).

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

Benchmark Total: main → PR Script: main → PR
01 create 1k 27.63 → 27.10 ms (−1.9%) 8.51 → 8.11 ms (−4.8%)
02 replace 1k 32.36 → 30.99 ms (−4.2%) 12.84 → 11.73 ms (−8.7%)
03 partial update 46.00 → 43.90 ms (−4.6%) 3.21 → 3.03 ms (−5.8%)
04 select row 6.14 → 4.46 ms (−27.4%) 1.95 → 1.81 ms (−6.7%)
05 swap rows 14.42 → 13.71 ms (−5.0%) 1.99 → 1.98 ms (−0.8%)
06 remove row 10.88 → 10.77 ms (−0.9%) 0.88 → 0.93 ms (+6.3%)
07 create 10k 294.28 → 288.93 ms (−1.8%) 82.81 → 77.06 ms (−6.9%)
08 append 1k 31.37 → 32.05 ms (+2.2%) 8.98 → 8.57 ms (−4.6%)
09 clear 1k 14.82 → 13.18 ms (−11.0%) 12.73 → 11.16 ms (−12.3%)
Memory: ready 1.04 → 1.04 MB (+0.0%)
Memory: run 1k 2.07 → 1.85 MB (−10.6%)
Memory: run + clear 1.36 → 1.35 MB (−0.7%)

03, 05 and 06 spend only 1–3 ms in script and their direction differed between rounds, so treat those rows as noise.

Micro benchmarks

ms per 100k iterations, mean of 2 rounds × 10 runs, release build, Node 22. The Imported object cases are the copyable @JSClass benchmarks from #829.

Case main this PR Change
Imported object/copyable/create and drop 10.17 5.08 −50%
Imported object/copyable/create, call and drop 11.72 5.89 −50%
Imported object/copyable/return and drop 11.09 5.95 −46%
Imported object/copyable/pass 2.49 1.64 −34%
Imported object/copyable/return and pass 13.29 6.99 −47%
JSObject/create and drop 8.62 4.48 −48%
JSObject/read object property and drop 44.87 29.03 −35%
JSObject/read object property Swift also holds and drop 41.44 28.95 −30%
JSObject/read object property and drop, 100k JSObjects alive 38,566 29.11 −99.9%
JSObject/compare two JSObjects ~0 1.14 ~11 ns per compare

The 100k-alive case is not included in JSObjectBenchmarks.swift because it takes ~40 s per sample on main.

Source compatibility

  • JSObject no longer conforms to Hashable, so Set<JSObject> and [JSObject: T] stop compiling. Code that associates data with JS objects should keep that association on the JS side, e.g. in a WeakMap. For example, ElementaryUI's FLIP code keys Swift dictionaries by DOM.Node, which hashes its JSObject.
  • JSObject.== must be called on the thread that owns both objects, since it calls into JS.
  • Two JSObjects for the same value now have different ids. Code under @_spi(JSObject_id) / @_spi(BridgeJS) that compares or hashes id should compare the objects with == instead.
  • Each slot can still hold 2^24 − 1 references.
JSObject.== now compares with JS `===`, and the Hashable conformance that relied on deduplicated ids is removed.

@MaxDesiatov MaxDesiatov 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.

yesplz 👏

@kateinoigakukun
kateinoigakukun merged commit 0628da7 into main Nov 27, 2026
16 checks passed
@kateinoigakukun
kateinoigakukun deleted the katei/remove-jsobject-dedup branch November 27, 2026 08:55
kateinoigakukun added a commit that referenced this pull request Nov 27, 2026
`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` used to call `super.init(id: 0)` and
overwrite the id afterwards, because their host function key was
`ObjectIdentifier(self)`. They now take the key from a per-thread counter that
skips keys still registered, create the JavaScript function first, and pass
its reference to `super.init`.

Co-authored-by: Max Desiatov <m_desiatov@apple.com>
kateinoigakukun added a commit that referenced this pull request Nov 27, 2026
`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 added a commit that referenced this pull request Nov 28, 2026
…tion (#830)

Add noncopyable `JSUniqueRef` and make `JSObject` own one

`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: Yuta Saito <kateinoigakukun@gmail.com>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

2 participants