Skip to content

Add noncopyable JSUniqueRef with heap references skipping deduplication - #830

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

MaxDesiatov wants to merge 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 from the heap's new retainUnique, which allocates a slot without a _slotByValue entry.

Every heap slot can now hold at most 8,388,607 references (2^23-1), down from 16,777,215 (2^24-1). That applies to ordinary references too, because bit 23 is now the flag for references that skip deduplication.

CI's stale-runtime check now diffs the runtime templates, since diffing their symlinks passed with stale templates.

In benchmarks I compared main, #829, and this PR with following code:

@JSClass struct NoncopyableImportedObject: ~Copyable {
    @JSFunction init(_ start: Int) throws(JSException)
    @JSFunction func value() throws(JSException) -> Int
}
@JSClass struct NoncopyableImportedObjectRoundtrip: ~Copyable {
    @JSFunction init() throws(JSException)
    @JSFunction func make(_ start: Int) throws(JSException) -> NoncopyableImportedObject
    @JSFunction func take(_ object: borrowing NoncopyableImportedObject) throws(JSException) -> Int
}

Both the copyable and the noncopyable wrappers are backed by the same JavaScript classes (run.js:8-15):

class ImportedObject {
    constructor(start) { this.start = start; }
    value() { return this.start; }
}
class ImportedObjectRoundtrip {
    make(start) { return new ImportedObject(start); }
    take(object) { return object.value(); }
}

Operations measured with each loop body running 100,000 times, roundtrip is created once before the loop. Dropping a wrapper calls swjs_release

┌───────────────────────┬───────────────────────────────────────────────┐
│          Row          │                   Loop body                   │
├───────────────────────┼───────────────────────────────────────────────┤
│ create and drop       │ _ = try! NoncopyableImportedObject(i)         │
├───────────────────────┼───────────────────────────────────────────────┤
│ create, call and drop │ _ = try! NoncopyableImportedObject(i).value() │
├───────────────────────┼───────────────────────────────────────────────┤
│ return and drop       │ _ = try! roundtrip.make(i)                    │
├───────────────────────┼───────────────────────────────────────────────┤
│ pass                  │ _ = try! roundtrip.take(object)               │
├───────────────────────┼───────────────────────────────────────────────┤
│ return and pass       │ _ = try! roundtrip.take(roundtrip.make(i))    │
└───────────────────────┴───────────────────────────────────────────────┘

Results with median ns per iteration:

┌──────────────────────────┬──────────────────┬───────────────────┬──────────────────────┬─────────────────┐
│           Case           │ Copyable on main │ Noncopyable, #829 │ Noncopyable, this PR │ this PR vs main │
├──────────────────────────┼──────────────────┼───────────────────┼──────────────────────┼─────────────────┤
│ create and drop          │ 68.5             │ 50.0              │ 25.5                 │ -62.8%          │
├──────────────────────────┼──────────────────┼───────────────────┼──────────────────────┼─────────────────┤
│ create, call and drop    │ 78.1             │ 59.9              │ 34.1                 │ -56.3%          │
├──────────────────────────┼──────────────────┼───────────────────┼──────────────────────┼─────────────────┤
│ return and drop          │ 68.5             │ 55.7              │ 29.1                 │ -57.5%          │
├──────────────────────────┼──────────────────┼───────────────────┼──────────────────────┼─────────────────┤
│ return and pass          │ 79.2             │ 68.1              │ 40.5                 │ -48.9%          │
├──────────────────────────┼──────────────────┼───────────────────┼──────────────────────┼─────────────────┤
│ pass an existing wrapper │ 15.5             │ 15.3              │ 15.0                 │ -3.2%           │
└──────────────────────────┴──────────────────┴───────────────────┴──────────────────────┴─────────────────┘

…tion

`JSUniqueRef` owns one JavaScript object reference from the heap's new `retainUnique`, which allocates a slot without a `_slotByValue` entry. `JSObject(_:)` consumes one and swaps it for `retain`'s reference, `JSObject` ids stay equal for the same object.

CI's stale-runtime check now diffs the runtime templates, since diffing their symlinks passed with stale templates.
@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%)

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