
Jank Isn't Always Your Dart Code: UI Thread, Raster Thread and saveLayer
- كاتب موثّق
- Flutter 3.x
محتويات المقال
const constructors, smaller widgets, fewer rebuilds. Nothing changes. When someone finally opens the DevTools frames chart in profile mode, the UI bars were short all along; the tall red bars belong to the raster thread. The cause is one line repeated in every list item: each expired coupon is wrapped in Opacity(opacity: 0.5).
The common misconception
build() methods, so fixing means rebuilding less and computing less.
build(): keep it cheap, keep setState local, use const. Dart is the only code you write, so it feels like the only code that can be slow. And on other platforms, "don't block the main thread" covers most stutter. The half-truth: if the UI thread is the slow one, it really is your Dart code. But it's only one of two threads that must finish every frame on time.
What actually happens
Two threads, two budgets
Read the two graphs before you optimize
void main() => runApp( const MaterialApp( showPerformanceOverlay: true, // top: raster thread, bottom: UI thread home: CouponsScreen(), ), );What the raster thread pays for
: drawing a group into an offscreen buffer, which might trigger a render target switch on the GPU. The docs call it one of the most expensive methods in the framework, and widgets might call it for you: Opacityon a group,ShaderMask,ColorFilter, and clips withClip.antiAliasWithSaveLayer.Clipping without saveLayeris cheaper, but still costly, especially in an animation.Shadows in specific situations, and rendering textures from image files, which the docs list among the most expensive operations. Shader compilation, on the first use of a shader. DevTools marks those frames in dark red. precompiles its shaders when the engine is built, and since Flutter 3.27 it's the default on iOS and Android API 29+.
Why the coupons stuttered
Opacity docs are direct: for values other than 0.0 and 1.0 it "requires painting the child into an intermediate buffer." Ten expired coupons on screen meant ten such groups in every frame the list scrolled, all of it raster-thread work. The Dart side was cheap, so every build() optimization missed the actual cost.
class ExpiredCoupon extends StatelessWidget { const ExpiredCoupon({super.key, required this.coupon}); final Coupon coupon; @override Widget build(BuildContext context) => Opacity( opacity: 0.5, // the whole group goes through an intermediate buffer child: Row(children: [ Image.asset(coupon.imagePath, width: 48), Text(coupon.title), ]), );}class ExpiredCoupon extends StatelessWidget { const ExpiredCoupon({super.key, required this.coupon}); final Coupon coupon; @override Widget build(BuildContext context) => Row(children: [ // Each piece draws itself semi-transparent: no group to buffer Image.asset( coupon.imagePath, width: 48, color: const Color.fromRGBO(255, 255, 255, 0.5), colorBlendMode: BlendMode.modulate, ), Text( coupon.title, style: const TextStyle(color: Color.fromRGBO(0, 0, 0, 0.5)), ), ]);}OpacityLayers, one per coupon; the second held none. The trick works because the image and the text don't overlap; the docs note that a semi-transparent color is only a substitute for Opacity when there are no overlapping parts.
build(), layout or a callback | build(), rebuild less, use an isolate | ||
saveLayer from group opacity, masks or filters | |||
RepaintBoundary | |||
How to apply it in your project
Measure in profile mode on a real device Run flutter run --profileon the slowest phone you support, then open the performance overlay or the DevTools frames chart while you reproduce the stutter.Decide which thread is red UI bars red: profile the Dart code. Raster bars red with short UI bars: stop optimizing build()and look at what the screen draws.Hunt for implicit saveLayer calls Search for Opacity,ShaderMask,ColorFilterandClip.antiAliasWithSaveLayer, especially in list items and animations, and ask the docs' question: does the app need this effect at all?Push effects down to individual elements Use semi-transparent colors for shapes and text, the colorandcolorBlendModeof anImage, andborderRadiusinstead of a rounded clip. To animate opacity, useAnimatedOpacityorFadeTransition: an animatedOpacityrebuilds every frame.Measure again Compare the raster bars before and after. If they didn't move, put the effect back and look for the next suspect.
Key takeaways
A frame needs both the UI thread and the raster thread to finish on time, and each has its own budget. The UI thread runs your Dart code and produces a layer tree; the raster thread turns it into GPU work. A red raster bar with short UI bars means the scene is expensive to draw, not that build()is slow.saveLayer, often hidden behindOpacity, masks, filters and some clips, is one of the most expensive operations.Profile in profile mode on a real device, find the red thread, then fix that thread's cost.
التعليقات (1)