Slide 1

Slide 1 text

Elevate user experience with Background Isolate Unlock and utilize real multithreading in Flutter Muhammad Alif Akbar | Senior Software Engineer at

Slide 2

Slide 2 text

Hello, I’m Alif Senior Software Engineer, Mobile at Grab (2021 - Present) linkedin.com/in/muhammad-alif-akbar Software House → E-Commerce →

Slide 3

Slide 3 text

Table of Contents 1. What is Multithreading 2. What is Isolate 3. Background Isolate 4. Quick Demo

Slide 4

Slide 4 text

What is Multithreading

Slide 5

Slide 5 text

Process (An App) Instance of a program that is executing on a computer / phone

Slide 6

Slide 6 text

Thread (Sub Process) Sub-component of a process that run/execute tasks Allocate variable “a” Set variable “a” to 100 Allocate variable “b” Set variable “b” to 200 Allocate variable “sum” Set variable “sum” to a + b Output variable “sum”

Slide 7

Slide 7 text

Main Thread / UI Thread / Main Loop Thread that run main tasks, usually related to UI events Draw Splash Screen Check Token Draw Home Screen

Slide 8

Slide 8 text

Main Thread / UI Thread / Main Loop Thread that run main tasks, usually related to UI events Draw Splash Screen Check Token Draw Home Screen View All Click

Slide 9

Slide 9 text

Main Thread / UI Thread / Main Loop Thread that run main tasks, usually related to UI events Draw Splash Screen Check Token Draw Home Screen View All Click 1s 1s 1s 1s

Slide 10

Slide 10 text

Main Thread / UI Thread / Main Loop Thread that run main tasks, usually related to UI events Draw Splash Screen 1s Get Profile Draw Home Screen View All Click 5s 1s 1s

Slide 11

Slide 11 text

Multi-Thread Use of multiple concurrent threads within a single process Read Token Draw Splash Screen Check Token Draw Home Screen Get Profile Get Statistic Report

Slide 12

Slide 12 text

MULTI-THREAD ALLOW US TO DELEGATE LONG RUNNING TASK INTO DIFFERENT THREAD, SO THAT UI (THREAD) WON’T BE FREEZED.” “

Slide 13

Slide 13 text

Multi-Thread Problem Race conditions, deadlocks, and synchronization Read Token Draw Splash Screen Check Token Draw Home Screen Get Profile Get Statistic Report

Slide 14

Slide 14 text

Multi-Thread Problem Race conditions, deadlocks, and synchronization Shared Space (Memory & State) Read Token Draw Splash Screen Check Token Draw Home Screen Get Profile Get Statistic Report

Slide 15

Slide 15 text

Multi-Thread Problem Race conditions, deadlocks, and synchronization Read Token Draw Splash Screen Check Token Draw Home Screen Get Profile Get Statistic Report Shared Space (Memory & State) 3 1 2

Slide 16

Slide 16 text

Multi-Thread Problem Race conditions, deadlocks, and synchronization Read Token Draw Splash Screen Check Token Draw Home Screen Get Profile Get Statistic Report Shared Space (Memory & State)

Slide 17

Slide 17 text

Multi-Thread Problem Race conditions, deadlocks, and synchronization Shared Space (Memory & State) x0 +1 x10 Output +1 +2

Slide 18

Slide 18 text

MULTI-THREAD, WHILE VERY USEFUL, IT INTRODUCES ADDITIONAL COMPLEXITIES AND POTENTIAL ISSUES.” “

Slide 19

Slide 19 text

What is Isolate

Slide 20

Slide 20 text

FLUTTER (WAY OF WATER) MULTI-THREADING

Slide 21

Slide 21 text

Grab’s Flutter Adoption Journey Q4, 2019 Q2, 2020 Q3, 2020 Q1, 2022

Slide 22

Slide 22 text

Async/Await is not Isolate It’s just postponing less crucial task to later time Draw Splash Screen 1s Get Profile Draw Home Screen View All Click 5s 1s 1s

Slide 23

Slide 23 text

Async/Await is not Isolate It’s just postponing less crucial task to later time Draw Splash Screen Await Get Profile Draw Home Screen View All Click 1s 5s 1s 1s Async Get Profile 1ms

Slide 24

Slide 24 text

You could see the screen is frozen, due to long running task on main thread.” “

Slide 25

Slide 25 text

What is Isolate As implied from the name, It’s an executor with Isolated Space Main Isolate Draw Splash Screen Check Token Draw Home Screen Read Token Get Profile Get Statistic Report Secondary Isolate

Slide 26

Slide 26 text

How Isolate Communicate Through communication (send/receive) ports Main Isolate Draw Splash Screen Async Get Profile Draw Home Screen Exec Get Profile Get Statistic Report Await Get Profile View All Click Await Get Profile Exec Get Profile Communication Ports Secondary Isolate

Slide 27

Slide 27 text

Talk less, Code More! 😡 Fortunately, we already have easy access to Isolate. Using compute function.

Slide 28

Slide 28 text

Background Isolate

Slide 29

Slide 29 text

Previous Isolate Limitation Communication to Platform level need to be done on Main Isolate Main Isolate Secondary Isolate Communication Ports Method Channel

Slide 30

Slide 30 text

Previous Isolate Limitation Communication to Platform level need to be done on Main Isolate Main Isolate Background Isolate Communication Ports Method Channel

Slide 31

Slide 31 text

Show Me Codes!😡 You have to register which isolate working as Background Isolate

Slide 32

Slide 32 text

How to retrieve the data? 🧐 No easy way to do it (yet). You have to implement the Dart ports.

Slide 33

Slide 33 text

Still confused? Read resources below at your own pace to get more understanding. When you do, notice who wrote the answer on SO. 😉

Slide 34

Slide 34 text

Scenarios where it’ll be helpful Why background Isolate is important Image Processing / Camera Filters Game 3D with GPU Acceleration High Throughput Real Time Tracking/Comms Creating camera filters, image detection and recognitions, written purely on Dart could be feasible. Todays, you could build simple Games using Flutter. There is micro framework called flame. Utilizing background Isolate, you could create more complex game with access to GPU without to have to burden the main thread. For specific use cases, where you might ignore comms cost/efficiency. You could communicate with servers as frequent as you want. Without glitching the UI due to large JSON parsing. 01 02 03

Slide 35

Slide 35 text

Quick (Recorded) Demo

Slide 36

Slide 36 text

Show Me The Result!🧐 Loading Animation While Doing Long Running Task

Slide 37

Slide 37 text

Show Me The Result!🧐 Frame Rate Performance

Slide 38

Slide 38 text

Show Me The Result!🧐 Frame Rate Performance It’s very subtle but you could notice there’s a frame skip while transitioning on left image.” “

Slide 39

Slide 39 text

Try to inspect App screen’s performance and identified any long running task that block UI thread. Moving long running task to different Thread/Isolate can help to deliver smoother UX to user. Multithreading concept in Flutter is known as Isolate. Which creates a simulated concurrent space, which has different/isolated memory and state. A completely background Isolate is now possible since Flutter 3.7.0. Key Takeaways

Slide 40

Slide 40 text

Muhammad Alif Akbar Mar 18, 2023 | Baparekraf Developer Day • 2023 Contact [email protected] / @muh.alifgiant