-
-
Notifications
You must be signed in to change notification settings - Fork 51
Add WebWorkerTaskExecutor
#256
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Conversation
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's [`worker_threads` module]. [SE-0417]: https://github.com/swiftlang/swift-evolution/blob/main/proposals/0417-task-executor-preference.md [Web Workers API]: https://developer.mozilla.org/en-US/docs/Web/API/Web_Workers_API [`worker_threads` module]: https://nodejs.org/api/worker_threads.html
Time Change: +399ms (4%) Total Time: 9,570ms
View Unchanged
View Baselines
|
aec76ce
to
e3181b0
Compare
For now, the data is not used in the worker thread, but it can be used in the future.
Seems like blocking the main thread also blocks the Web Worker threads from starting on some browsers.
CC: @ephemer might be interested in :) |
Hi @kateinoigakukun thanks, just saw this in the JSKit code and randomly saw your ping here after investigating more closely. I don't get notifications from GitHub, sorry! This is interesting, and definitely makes sense to me as an approach for Swift concurrency. Our use case is to put an ML workload onto a background thread. That workload is managed by tfjs, and we can't do much more with splitting up the work into chunks like in the example here. I think we'd have to use "real" threading for our use case to benefit from this direction of work. I think generally that would be the best outcome for Swift Wasm too (using "real" Wasm threads). I know that not all runtime environments support it though (including ours – we need to support browsers back to ~Safari 12) |
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 swiftlang/swift#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