text
stringlengths
1
372
<topic_start>
checking for offscreen layers
the saveLayer method is one of the most expensive methods in
the flutter framework. it’s useful when applying post-processing
to the scene, but it can slow your app and should be avoided if
you don’t need it. even if you don’t call saveLayer explicitly,
implicit calls might happen on your behalf. you can check whether
your scene is using saveLayer with the
PerformanceOverlayLayer.checkerboardOffscreenLayers switch.
once the switch is enabled, run the app and look for any images
that are outlined with a flickering box. the box flickers from
frame to frame if a new frame is being rendered. for example,
perhaps you have a group of objects with opacities that are rendered
using saveLayer. in this case, it’s probably more performant to
apply an opacity to each individual widget, rather than a parent
widget higher up in the widget tree. the same goes for
other potentially expensive operations, such as clipping or shadows.
info note
opacity, clipping, and shadows are not, in themselves,
a bad idea. however, applying them to the top of the
widget tree might cause extra calls to saveLayer,
and needless processing.
when you encounter calls to saveLayer,
ask yourself these questions:
<topic_end>
<topic_start>
checking for non-cached images
caching an image with RepaintBoundary is good,
when it makes sense.
one of the most expensive operations,
from a resource perspective,
is rendering a texture using an image file.
first, the compressed image
is fetched from persistent storage.
the image is decompressed into host memory (gpu memory),
and transferred to device memory (ram).
in other words, image I/O can be expensive.
the cache provides snapshots of complex hierarchies so
they are easier to render in subsequent frames.
because raster cache entries are expensive to
construct and take up loads of GPU memory,
cache images only where absolutely necessary.
you can see which images are being cached by enabling the
PerformanceOverlayLayer.checkerboardRasterCacheImages switch.
run the app and look for images rendered with a randomly colored
checkerboard, indicating that the image is cached.
as you interact with the scene, the checkerboarded images
should remain constant—you don’t want to see flickering,
which would indicate that the cached image is being re-cached.
in most cases, you want to see checkerboards on static images,
but not on non-static images. if a static image isn’t cached,
you can cache it by placing it into a RepaintBoundary
widget. though the engine might still ignore a repaint
boundary if it thinks the image isn’t complex enough.
<topic_end>
<topic_start>
viewing the widget rebuild profiler
the flutter framework is designed to make it hard to create
applications that are not 60fps and smooth. often, if you have jank,
it’s because there is a simple bug causing more of the UI to be
rebuilt each frame than required. the widget rebuild profiler
helps you debug and fix performance problems due to these sorts
of bugs.
you can view the widget rebuilt counts for the current screen and
frame in the flutter plugin for android studio and IntelliJ.
for details on how to do this, see show performance data
<topic_end>
<topic_start>
benchmarking
you can measure and track your app’s performance by writing
benchmark tests. the flutter driver library provides support
for benchmarking. using this integration test framework,
you can generate metrics to track the following:
tracking these benchmarks allows you to be informed when a
regression is introduced that adversely affects performance.
for more information, check out integration testing.
<topic_end>
<topic_start>
other resources
the following resources provide more information on using
flutter’s tools and debugging in flutter:
<topic_end>
<topic_start>
debugging performance for web apps
info note
profiling flutter web apps requires flutter version 3.14 or later.
the flutter framework emits timeline events as it works to build frames,
draw scenes, and track other activity such as garbage collections.
these events are exposed in the
chrome DevTools performance panel for debugging.
you can also emit your own timeline events using the dart:developer
timeline and TimelineTask APIs for further performance analysis.
<topic_end>
<topic_start>
optional flags to enhance tracing
to configure which timeline events are tracked, set any of the following top-level properties to true
in your app’s main method.
<topic_end>
<topic_start>
instructions