Sub topic 13
A Repeatable Measure, Change, Re-Measure Workflow
This chapter assumes that you can build a basic Flutter app, use flutter run, and read Dart widget code. You also need a physical device or emulator and Flutter DevTools.
Performance work is an experiment. First, measure the problem. Then, make one targeted change. Finally, measure again under the same conditions. This process helps you tell a real improvement from a change that only appears faster.
Measure the baseline
Use profile mode for performance measurements. Debug mode adds checks and instrumentation that can change timing. Release mode is useful for final validation, but it removes some profiling support.
Choose the same device, screen, data, and user action for every run. Start with a short warm-up so startup work and shader compilation do not dominate the result.
flutter devices
flutter run --profile -d <device-id>Open DevTools from the running application or with the command-line workflow supported by your Flutter installation. Select Performance and record a trace while reproducing the slow interaction.
Use the Flutter frames chart to find frames that exceed the frame budget. Record useful facts instead of relying on an impression:
- Which action produced the slow frame?
- How many slow frames appeared?
- Did the UI thread or raster thread take longer?
- Which events appear in Frame Analysis and Timeline Events?
- Was the first run slower than later runs?
A 60 Hz display gives Flutter about 16.67 milliseconds for each frame. A 120 Hz display gives about 8.33 milliseconds. If a frame misses its available interval, the screen may show a delayed update or visible stutter. This visible stutter is called jank.
Make one targeted change
The trace should guide the change. For UI-thread work, look for expensive code in build, repeated layout work, large state updates, or synchronous parsing. Move expensive computation away from build, reduce unnecessary rebuilds, or split a large update into smaller updates.
For raster-thread work, inspect effects such as large shadows, clips, opacity layers, complex paths, and excessive pixels. Simplify the visual tree or reduce the area that must be painted.
Shader compilation can cause an isolated slow frame when a visual effect appears for the first time. Warm up the relevant interaction before comparing ordinary frame timings. Impeller can reduce some shader-related problems on supported platforms, but it does not make every paint operation cheap.
Use enhance-tracing options when the normal trace does not identify the responsible widget. Build, layout, and paint tracing add detail, so enable them for a focused investigation rather than every long recording.
You can also mark an important synchronous operation with a custom timeline event:
import 'dart:developer';
List<String> parseCatalog(String raw) {
return Timeline.timeSync('parse_catalog', () {
return raw
.split('\n')
.where((line) => line.isNotEmpty)
.toList();
});
}The parse_catalog event appears in Timeline Events and gives the operation a searchable name.
Re-measure and compare
Repeat the exact baseline procedure. Use the same device, route through the same screens, and interact with the same data. Compare the slowest frames, the number of missed frames, and the thread responsible.
| Result after the change | Likely decision |
|---|---|
| UI time falls and slow frames disappear | Keep the change and test more cases |
| Raster time is unchanged | The fix did not address the bottleneck |
| First run improves but later runs do not | Investigate shader or startup work |
| Results vary widely | Improve the test procedure before drawing conclusions |
Keep a small record of each experiment. Include the commit, device, Flutter version, interaction, and frame timings. If the result is worse, revert the change before testing another hypothesis. One change at a time keeps the evidence understandable.
Exercise
You move JSON parsing out of build(), but the raster thread still exceeds the frame budget. What should you do next?
Show answer
Keep the UI-thread fix only if its measurements improved, then inspect raster events and Frame Analysis for expensive painting, layers, effects, or shader compilation. Make one raster-focused change and measure again.