Choosing Between Isolates and async/await for CPU‑Bound Work After Measuring Bottlenecks
0 reputation · 30 Jul 2025, 21:28 UTC
Goal: determine whether to offload CPU‑intensive computation to a Dart Isolate or keep it on the single‑threaded event loop using async/await, after first measuring where time is spent.
Constraints: Isolates give true parallelism but add startup latency and message‑passing overhead; async/await avoids copy costs but any synchronous work blocks the UI and can cause frame drops. Additionally, Dart SDK 3.x does not guarantee deterministic termination order when isolates are closed with Isolate.kill, which can lead to resource‑leak races when native resources are acquired via dart:ffi.
Questions:
- Which profiling metrics from Dart DevTools (e.g., CPU profiler timeline, isolate message‑queue latency) indicate that the main thread’s event loop is the bottleneck versus isolate communication overhead?
- How should deterministic cleanup of native resources be coordinated when isolates may be terminated in non‑deterministic order by Isolate.kill?
- Is there a recommended pattern for spawning work that avoids reliance on Isolate.kill for ordering while still allowing timely shutdown?