dart-lang / sdk

The Dart SDK, including the VM, JS and Wasm compilers, analysis, core libraries, and more.
https://dart.dev
BSD 3-Clause "New" or "Revised" License
10.07k stars 1.56k forks source link

[dart2wasm] Multi isolate support #55364

Open felix-ht opened 5 months ago

felix-ht commented 5 months ago

Currently, dart2wasm lacks support for isolates, dampening my enthusiasm for WASM as a means to achieve seamless concurrency on the web.

Referencing this issue, it appears that an upstream dependency prevents the implementation of isolates with native-like features. It will takes years until this is fully specified and implemented in (all modern) browsers.

@mraleph's metioned the possibility to implement a simplified version of isolates, albeit with certain limitations. These limitations seem to include:

Despite these constraints, I would prefer this limited form of concurrency over none. The absence of effective concurrency options means tasks like parsing large files or responses lead to performance issues. The current solution using web workers is unwieldy and starkly conflicts with Dart's commitment to providing a great developer experience.

felix-ht commented 3 months ago

One faster option, that is still better than the currently possible serialisation based approach, would be to go with the threads proposal for wasm.

This is in the specification phase 4, instead of 1 so might land within a reasonable time frame.

An incremental adoption would still be my preferred variant:

  1. Threading with Serialisation (now)
  2. Threading Memory Sharing based (Threads)
  3. Threading with shared everything threads (Far future)

The upside would be that 1 and 2 should have easy webworker equivalent so the api could be kept consistent between JS and WASM


If serialisation is not an option we could also be to go with creating sperate workers on the js side and sharing data with SharedArrayBuffer or with tranferable objects. This is what Emscripten and wasm-bindgen-rayon This could also work seemlessly for dart2js.

mraleph commented 3 months ago

One faster option, that is still better than the currently possible serialisation based approach, would be to go with the threads proposal for wasm.

Threads proposal only allows sharing linear memory - you can't share Wasm GC structs, that's why we need shared everything threads proposal.

So with what is available right now in Wasm (even with threads proposal) you can only send structured data by copying it, and only typed arrays backed by SABs can be really truly shared.

felix-ht commented 3 months ago

Thanks for sharing that info @mraleph i wasn’t aware of the Wasm GC limitations!