Why do we need (another) Isolates?

Hardik Mehta
3 min readApr 17, 2023

--

Photo by Tine Ivanič on Unsplash

Hello there 👋

In the previous article, we saw what is an isolate and got to know the difference between concurrency and parallelism. This article will explain why do we need another isolate?

Things are fine if we are to only design beautiful, stunning flutter apps without the need for heavy tasks, Balle Balle 🕺🏼 but our app needs to do more than one task, for example, interacting with file storage and network or perhaps parsing a large file or processing an image. You guessed it right 😌👈🏼, we do not want to use the main isolate for long-running, CPU-intensive tasks as it can cause frame drops for our app users leading to a bad user experience and potential uninstalling 😓🫣

We need to understand in what scenario the UI can be blocked. They can be grouped into 2 categories, I/O-bound and CPU-bound.

IO-bound: I/O refers to the data in movement, for example, network HTTP request/response, reading/writing data to local storage, and message passing among threads.

CPU-bound: Here, the time to complete a task is correlated to the speed of the processor. Tasks that do a lot of calculations with comparatively little data are often CPU bound. Image resizing, mathematical calculations, video editing, and gaming are some examples of CPU Bound tasks.

We need to perform these blocking tasks while keeping the main isolate unchained for its event handling. Dart has a few tools in its arsenal to get this done, namely async-await, Future and Stream. Let’s see what purpose they serve.

Async-Await+Future ⏰

These keywords provide a declarative way to define asynchronous functions. We can perform non-blocking IO-bound operations such as requesting data over the internet or a file read-write. The good thing is Dart has already identified and solved all the IO-bound blocking operations for us with Future

Async splits a long synchronous task into smaller ones and this helps the main thread to stay at 60 FPS

However, we need to keep in mind that the code runs in the same main thread in an interleaved and cooperative manner. Thus in the case of a network call, while we wait for the HTTP response, which can take several seconds, our app can be blocked and UI can be frozen if the code is not written properly.

Example: Requesting data over a network

With respect to CPU-bound operations, the above-mentioned technique does not unblock the UI thread. Let’s take a look at this maths example.

Example of how a CPU-bound task can block UI rendering

In the above video, we can see that doing heavy computation tasks in an async method is causing the lag in the app.

This is where spawning a new isolate or a worker Isolate comes in 🦸🏽 Since isolates are separate, we can have more than one isolate within our app, and that’s how we can have multiple threads.

Dart is a single-thread language. If that was the case then we shouldn’t be able to use multiple cores. Or maybe the statement has other meaning. Please correct me in the comments

Now we know why we need to create another isolate. Let’s see in the next article how we can create them.

https://hardikm9850.github.io/
https://www.linkedin.com/in/hardik9850/

--

--

Hardik Mehta
Hardik Mehta

Written by Hardik Mehta

Believer that everything testes better with butter. https://hardikm9850.github.io/

No responses yet