Performance and battery
What makes an element redraw, which properties are expensive, and how to build a theme that costs nothing while it sits there.
On this page
A launcher is a strange program to write for: its process lives for days and is brought to the front dozens of times an hour. The expensive mistake is not a slow animation, because that one you notice. It is a theme that keeps working while nothing is happening.
The measured baseline is worth knowing, because it is what your theme spends against. Sitting idle on the home screen for a minute, the launcher with its default theme draws one frame and uses about a quarter of a percent of one core, roughly three times less than the stock Pixel launcher, which keeps a clock ticking. A single careless formula gives that away.
What makes an element recompute#
The launcher decides per element whether it has to be re-evaluated while something is moving. It decides by looking for the names of the moving values in that element's formulas:
| A formula mentioning | Re-evaluates |
|---|---|
time_…, date_…, weekday_name, month_name | Once a second, for as long as it is on screen |
tilt_x, tilt_y, compass_heading | About twenty times a second, and turns on the sensor |
parallax_x, parallax_y | Every frame while the phone moves, never while it is still, and turns on the gyroscope |
lock_state | Every frame for about half a second after an unlock |
page_position, page_scroll_fraction, page_swipe_progress | Every frame of a swipe or a scroll |
scrub_… | Every frame while a finger is on the scrub strip |
animate( in a loop, which is the default | Every frame, forever |
animate(…, false) | Every frame until it has run once, then not at all |
spring( | Every frame until it settles, then stops |
Three things follow from this being per element:
- A ticking clock costs one element, not the page. Put the seconds in their own text element rather than in a formula that also positions the block around it.
- Mentioning a name is enough. The check is on the text of your formulas, so a variable of your own called
time_leftmakes that element tick every second even though nothing in it depends on the clock. Name your own variables so they do not collide; see variables of your own. - The sensors are theme-wide. If any element anywhere in the theme reads tilt or compass, the launcher registers the sensor and it stays registered. One decorative parallax on a page nobody visits costs battery all day.
The order to reach for#
For the same visual effect, these are not equally expensive:
- A value that already changes.
page_positionandpage_scroll_fractionfollow the finger, cost nothing extra to watch, and track a gesture exactly. Almost every parallax, fade and scale effect should be built on one of them. spring()or anappearanimation. Frames only until it settles.- A looping
animate()or atimeanimation. Frames forever, whether anybody is looking or not. Leaving out the third argument ofanimate()does not avoid this: it defaults to repeating.
An effect built with animate() that could have been built on page_position is both more expensive and worse: it drifts out of step with the swipe it is supposed to accompany.
Expensive properties#
backdropBlurblurs everything behind the element, every frame it changes. It is the most expensive single property in the format. One glass panel is a design; four stacked translucent layers is a slideshow on a mid-range phone.imageBlur,imageSaturation,imageBrightnessare cheaper, because they touch the picture rather than the screen behind it, but they still run per image, so a feed of forty blurred thumbnails adds up.elevationdraws a shadow. Cheap once, noticeable on every card of a long list.- Large
maxItems. A repeater or grid only builds what is visible, but every card evaluates its ownrowHeighton every measure pass. Keep per-card formulas short, and cap lists that have no natural end.
Two habits that cost nothing#
Read time no finer than you show it. A field containing time_second re-evaluates every second forever. A clock without seconds must not mention it, not even in a rounding expression that discards it.
Hide, do not fade. hidden removes an element: it is not measured, not drawn, and its formulas are not evaluated. opacity of 0 keeps all of it, invisibly. For a panel that is usually closed, hidden is the correct choice and it is also the free one.
Measuring rather than guessing#
Two traps make a theme look worse than it is.
A debug build is not a measurement. Code runs interpreted there, and Compose UI is roughly twice as expensive. A theme that stutters in a development build can be perfectly smooth in a released one. This was, once, a whole afternoon of hunting a problem that did not exist.
The emulator shares a machine. Whatever else the host is doing lands in your numbers. Measure on quiet hardware, and interleave runs rather than comparing one long run against another.
Before you publish#
- Open the theme, put the phone down, and look at it again after a minute. Nothing should have moved that you did not intend to move.
- Swipe between every pair of pages. A stutter here is usually one formula, and it is usually a
timeanimation doing a gesture's job. - Check that no element reads tilt or compass unless the theme is actually about that.
Documentation for launcher 0.1.0-alpha Android 12 and later last updated