swiftwasm / swiftwasm/JavaScriptKit

[BridgeJS] Pass String parameters unretained by default

Open
#677 1 comment 0 reactions 0 assignees View on GitHub

Nobody has claimed this yet.

Dominant language
Swift
Stars
986
Forks
76
Avg merge
21h 11m
Merged PRs (30d)
4

Description

When calling a JS function through BridgeJS with String parameters, every String is

  • lowered by calling _swift_js_make_js_string
  • which decodes and retains in JS object store (new entry, new ref = 1)
  • the object store ref is passed as wasm parameter
  • the JS thunk immediately get the object and releases (count--, remove from store)

this is very wasteful, and makes retain the number one bottleneck in current ElementaryUI performance benchmarks.

Example:

@JSFunction func hello(_ v: String) throws

// generates
func _$hello(_ v: String) throws(JSException) -> Void {
    let vValue = v.bridgeJSLowerParameter()
    bjs_hello(vValue)
    if let error = _swift_js_take_exception() {
        throw error
    }
}
function bjs_hello(v) {
    try {
        const vObject = swift.memory.getObject(v);
        swift.memory.release(v);
        imports.hello(vObject);
    } catch (error) {
        setException(error);
    }
}

I suggest:

  • passing String always "in-line" (ie: as address + length) without retaining in the JS memory store
  • still support JSString by-ref to control Swift caller caching (ie: retain once and use same ref multiple times)

Contributor guide

Open the contributing guide

First steps

  1. Read the whole issue, then the project's contributing guide.
  2. Comment on the issue to say you are picking it up — it saves two people doing the same work.
  3. Fork the repository and make your change on a branch.
  4. Open a pull request that references the issue number.

Research direction

Start by tracing _swift_js_make_js_string, bridgeJSLowerParameter, and the generated bjs_hello thunk shown in the issue, then inspect how JSString references are represented. Done means ordinary String parameters cross the bridge without a JS memory-store retain/release cycle while JSString still supports caller-controlled by-reference reuse; verify the impact against the ElementaryUI performance benchmarks.

Written by the indexing model from the issue text.

Assessment

Tech stack
javascript, swift, wasm
Domain
api, performance, web-dev
Issue type
Refactor
Difficulty
5/5
Estimated time
Over a week
Activity status
Stale
Clarity
Mostly clear
Newbie friendliness
28/100

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.