gaaclarke / agents

Dart package that conveniently wraps an isolate, ports and state for easy isolates.
BSD 3-Clause "New" or "Revised" License
79 stars 7 forks source link

Questions about Isolate Groups and "move" operator #6

Closed KyleFin closed 1 year ago

KyleFin commented 1 year ago

(Context: I've learned a bit about parallelism in general and in Dart but don't have much hands-on experience)

isolate_agents looks like an awesome implementation of the Actor pattern! Thanks for implementing and sharing it, especially the nice write up and example use-cases in your Medium post and this repo.

I had a few thoughts / questions:

Thanks again!

gaaclarke commented 1 year ago

Thanks Kyle.

You mentioned that controlling the lifecycle of isolates is a benefit of isolate_agents compared to Flutter's .compute(). How do Agents relate to isolate groups? I remember hearing exciting stuff ~18 months ago about how much https://github.com/dart-lang/sdk/issues/46754 would reduce the cost of spinning up new isolates. With Agents, would you create a pool of long-running agents and send work to them?

When running on a version of Dart with isolate groups, agents will be spawned in the isolate group. Isolate groups make the cost of spinning up isolates much cheaper, but they still aren't free. With agents you have the ability to create long running agents if you want. Unfortunately it's a complicated question to answer if that is the best way to do things because of the optimization that happens when you kill an isolate. It does constant time returning values in that case. So if you are returning a large value from an isolate it might actually be cheaper to spawn and kill a new isolate for each calculation (i.e. time_to_spin_up_isolate - time_to_transmit_output < 0).

Another performance consideration you mentioned is the need for a "move" operator to allow constant time communication between isolates. Again I'm not familiar with how this all works, but I thought you might be interested in this Rust crate that generates FFI bindings and allows "zero copy returns over the FFI boundary". It would be awesome to use Dart everywhere (with agents?) instead of Rust for high-performance parallelism. Just thought I'd share in case that approach is of interest. (I love how you took inspiration from Clojure for isolate_agents)

Thanks for sharing Membrane. I dig it. You can simulate constant time communication if the data you are transmitting is held in a buffer outside of the Dart VM. However it isn't really constant-time since it requires extra work reading and writing from that buffer since it isn't in the dart heap in the memory format of dart objects.