Repository navigation
Runtime: Stop deduplicating JSObject references in the object heap - #835
Merged
Merged
Conversation
JSObject.== now compares with JS `===`, and the Hashable conformance that relied on deduplicated ids is removed.
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>
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
Overview
The object heap keeps a
Mapfrom each JS value to its slot, so everyJSObjectfor the same value shares one id. OnlyJSObject.==and theHashableconformance rely on that, and both can live without it. This PR removes the map:retainnow always allocates a new slot, as wasm-bindgen's heap does.1. Equality uses JS
===.JSObject.==calls a newswjs_value_strict_equalsimport instead of comparing ids. The existingswjs_value_equalsuses loose==, which could run uservalueOfcode when aJSObjectis compared with aJSBigInt.2.
JSObjectis no longerHashable. A hash consistent with JS identity would need a value-to-id map, which is exactly the cost removed here. wasm-bindgen doesn't provideHashforJsValueor js-sys types either.3. The heap gets simpler and faster.
retainno longer looks up or inserts into the map, andreleaseno longer deletes from it.Benchmarks
js-framework-benchmark (ElementaryUI)
ElementaryUI
mainbuilt against JavaScriptKitmainand this PR, with ElementaryUI'sBenchmarks/PerformanceBenchmarksrunner. 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.
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 objectcases are the copyable@JSClassbenchmarks from #829.Imported object/copyable/create and dropImported object/copyable/create, call and dropImported object/copyable/return and dropImported object/copyable/passImported object/copyable/return and passJSObject/create and dropJSObject/read object property and dropJSObject/read object property Swift also holds and dropJSObject/read object property and drop, 100kJSObjects aliveJSObject/compare two JSObjectsThe 100k-alive case is not included in
JSObjectBenchmarks.swiftbecause it takes ~40 s per sample on main.Source compatibility
JSObjectno longer conforms toHashable, soSet<JSObject>and[JSObject: T]stop compiling. Code that associates data with JS objects should keep that association on the JS side, e.g. in aWeakMap. For example, ElementaryUI's FLIP code keys Swift dictionaries byDOM.Node, which hashes itsJSObject.JSObject.==must be called on the thread that owns both objects, since it calls into JS.JSObjects for the same value now have differentids. Code under@_spi(JSObject_id)/@_spi(BridgeJS)that compares or hashesidshould compare the objects with==instead.