Closed kateinoigakukun closed 2 months ago
Time Change: +399ms (4%)
Total Time: 9,570ms
Test name | Duration | Change | |
---|---|---|---|
Serialization/JavaScript function call through Wasm import | 24ms | +5ms (18%) | ⚠️ |
Serialization/JavaScript function call through Wasm import with int | 17ms | +3ms (18%) | ⚠️ |
Serialization/JavaScript function call from Swift | 110ms | +15ms (13%) | ⚠️ |
Serialization/Swift Int to JavaScript with assignment | 343ms | +28ms (8%) | 🔍 |
Serialization/JavaScript Number to Swift Int | 320ms | +20ms (6%) | 🔍 |
CC: @ephemer might be interested in :)
WebWorkerTaskExecutor is an implementation of
TaskExecutor
protocol, which is introduced by SE-0417 since Swift 6.0. This task executor runs tasks on Worker threads, which is useful for offloading computationally expensive tasks from the main thread.The
WebWorkerTaskExecutor
is designed to work with Web Workers API and Node.js'sworker_threads
module.This depends on https://github.com/swiftlang/swift/pull/75008
Example
Try it out: https://swiftwasm-threading-example.vercel.app/
Source: https://github.com/kateinoigakukun/swiftwasm-threading-example/
Usage
Also JavaScript-side needs some tweaks:
Dataflow overview
Known limitations
@MainActor
does not hop back to main threadDue to an issue in Cooperative global executor,
@MainActor
andMainActor.run
don't switch execution thread whenWebWorkerTaskExecutor
is preferred.JSObject
instance cannot cross the thread boundaryDue to the underlying Web Worker limitation, JavaScript objects (
JSObject
) cannot be shared with or transferred to another thread. You need to convert it into Swift-native objects to represent it in shared memory space.TODO