Profiling a Flutter app
Today I ran into some performance issue when I used my image gallery app on a folder of 17k images. Lots of jank. So I decided to use the Flutter profiler for the first time.
I started from the Flutter performance profiling page.
![](https://flutter.dev/images/flutter-logo-sharing.png)
They give 3 ways to run the profiler, two of which applied to me:
- in VS Code, modify launch.json to enable profiling.
- Run
flutter run --profile
.
I decided to do it in VS Code. However I didn't know how to find that file. It wasn't in the folder of my project, and it wasn't in the IDE's settings page. So I went to VS Code's documentation.
![](https://code.visualstudio.com/assets/docs/editor/debugging/Debugging.png)
They say you should go to the Run and Debug pane to create the file.
![](https://www.wafrat.com/content/images/2021/06/image-1.png)
Upon clicking to create the launch.json file, it started installing some libraries:
Installing C# dependencies...
Platform: darwin, arm64
Downloading package '.NET Core Debugger (macOS / x64)' (45875 KB).................... Done!
Validating download...
Integrity Check succeeded.
Installing package '.NET Core Debugger (macOS / x64)'
Downloading package '.NET Core Debugger (macOS / arm64)' (47834 KB).................... Done!
Validating download...
Integrity Check succeeded.
Installing package '.NET Core Debugger (macOS / arm64)'
Downloading package 'Razor Language Server (macOS / x64)' (60206 KB).................... Done!
Installing package 'Razor Language Server (macOS / x64)'
Finished
Failed to spawn 'dotnet --info'
And failed.
![](https://www.wafrat.com/content/images/2021/06/image-2.png)
Clicking Get the .NET Core SDK opens a page to download it. It downloads and installs fine.
![](https://www.wafrat.com/content/images/2021/06/image-3.png)
This time clicking the create launch.json worked and created a config with two profiles:
- Launch the app
- Launch the app in profile mode.
The options also show up on the left pane, where I can run them.
![](https://www.wafrat.com/content/images/2021/06/image-4.png)
It took a few minutes to compile, more than usual. When it finally ran, it didn't show me the DevTools and widget tree as usual:
![](https://www.wafrat.com/content/images/2021/06/image-5.png)
This StackOverflow post says the Inspector doesn't work in profile mode. Yet the Flutter's page on profiling says:
Once your app is running in profile mode, launch DevTools.
But clicking the new squiggly icon opens a new tab!
![](https://www.wafrat.com/content/images/2021/06/image-6.png)
![](https://www.wafrat.com/content/images/2021/06/image-8.png)
This is still not what I want though since this shows rendering speed, not where CPU time is spent.
Eventually I found the documentation on how to open the dev tools: https://flutter.dev/docs/development/tools/devtools/vscode.
![](https://www.wafrat.com/content/images/2021/06/image-7.png)
![](https://www.wafrat.com/content/images/2021/06/image-9.png)
Then I enabled the CPU recording, tested the app, then came back to the profiler top stop the recording. It showed me a flame graph, which showed me instantly what I was doing wrong.
![](https://www.wafrat.com/content/images/2021/06/image-10.png)
The moral is don't try to sort 17k elements in render()
. No matter how fast the machine, it won't be fast enough.