الانتقال إلى المحتوى
تطبيقات الموبايلFlutter· 7 دقائق للقراءة

Jank Isn't Always Your Dart Code: UI Thread, Raster Thread and saveLayer

Each frame has two budgets. When the raster thread is the slow one, faster build() methods change nothing.

ReadIn

مقال كل يوم يفرق معاك

22 نوفمبر 2026
  • كاتب موثّق
  • Flutter 3.x
محتويات المقال

A coupons screen scrolls smoothly on the developer's phone and stutters on a budget Android device. The team spends a day on the usual advice: 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

The belief: dropped frames always come from slow Dart code, usually in build() methods, so fixing means rebuilding less and computing less.

It's a fair starting point. Most performance advice is about 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

The UI thread runs all your Dart code and the framework's: build, layout and paint. Paint doesn't produce pixels. As the docs put it, the UI thread "creates a layer tree, a lightweight object containing device-agnostic painting commands, and sends the layer tree to the raster thread to be rendered on the device." The takes that tree and talks to the GPU; Skia and Impeller, the graphics libraries, run there. Despite the name, it runs on the CPU too.

Because the work is split, the best-practices page gives each thread its own budget: on a 60 Hz display, 16 ms for building and 16 ms for rendering. If latency matters, aim for 8 ms each, and for 120 Hz screens, under 8 ms in total. A frame that misses either budget is dropped.

Read the two graphs before you optimize

The performance overlay draws one graph per thread: the top one, labeled "GPU", is the raster thread, and the bottom one is the UI thread. The docs give the rule: a red bar in the UI graph means "the Dart code is too expensive"; a red bar in the GPU graph means "the scene is too complicated to render quickly." When both are red, start with the UI thread. The DevTools frames chart shows the same split as a pair of bars per frame.

Dart main.dart
void main() => runApp(      const MaterialApp(        showPerformanceOverlay: true, // top: raster thread, bottom: UI thread        home: CouponsScreen(),      ),    );

What the raster thread pays for

You can't access the raster thread directly, but, in the docs' words, "if this thread is slow, it's a result of something you've done in the Dart code." The usual suspects, from the docs:

  • : 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: Opacity on a group, ShaderMask, ColorFilter, and clips with Clip.antiAliasWithSaveLayer.
  • Clipping without saveLayer is 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

The 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.

Dart expired_coupon.dart
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),        ]),      );}

In a widget test with ten coupons, the first version's layer tree held ten 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.

SymptomSlow threadTypical causeFirst thing to try
Red UI barsUIHeavy work in build(), layout or a callbackMove work out of build(), rebuild less, use an isolate
Red raster bars, short UI barsRastersaveLayer from group opacity, masks or filtersApply the effect to individual elements
Red raster bars through an animationRasterClipping, or fading a large static scenePre-clip the image, or try a RepaintBoundary
A dark red frame on first useRasterShader compilationCheck whether the app is running on Impeller

How to apply it in your project

  1. Measure in profile mode on a real deviceRun flutter run --profile on the slowest phone you support, then open the performance overlay or the DevTools frames chart while you reproduce the stutter.
  2. Decide which thread is redUI bars red: profile the Dart code. Raster bars red with short UI bars: stop optimizing build() and look at what the screen draws.
  3. Hunt for implicit saveLayer callsSearch for Opacity, ShaderMask, ColorFilter and Clip.antiAliasWithSaveLayer, especially in list items and animations, and ask the docs' question: does the app need this effect at all?
  4. Push effects down to individual elementsUse semi-transparent colors for shapes and text, the color and colorBlendMode of an Image, and borderRadius instead of a rounded clip. To animate opacity, use AnimatedOpacity or FadeTransition: an animated Opacity rebuilds every frame.
  5. Measure againCompare 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 behind Opacity, 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. Flutter performance profiling — Flutter docs
  2. Performance best practices — Flutter docs
  3. Use the Performance view — Flutter DevTools docs
  4. Opacity class — Flutter API
  5. Impeller rendering engine — Flutter docs

التعليقات (1)