CoursesCompose Multiplatform, one screen from state to pixels

Puzzle 3 · 4 of 11

Who read that state?

The composition remembers every call. When a counter changes, Compose doesn't run your whole screen again; it runs only the calls that read the counter. Here's how it knows who read it, and when it runs them.

All puzzles in this course

The problem

Puzzle 1's composition holds a group for every call on the screen, often hundreds. You tap a button and count++ runs. Running all of them again on every tap would be slow: Android's docs call running the whole UI tree again "computationally expensive, which uses computing power and battery life". So which calls need to run again, and who decides?

See it

Screen holds Header and Counter; count is snapshot state; Counter reads count; a tap writes count = 1 and only Counter is marked; Counter runs again from its start while Screen and Header do not; the Recomposer does it at the next frame, once for several writes ScreenHeaderCounterButton Text: 0 count = 0 read by Counter count = 1 Text: 1not runnot run Recomposer3 writes, 1 frame, 1 run

Scroll sideways to see the full diagram.

  1. count is snapshot state, made by mutableStateOf: a value holder whose every read and every write Compose watches. The screen shows 0.

    Step 1 of 5
  2. While Counter runs, it reads count.value: a state read. Compose notes which group was running at that moment: Counter's.

    Step 2 of 5
  3. A tap writes count = 1. Compose marks every group that read count, and only those. Predict: what runs again?

    Step 3 of 5
  4. Only Counter, from its start: the plugin made its group a restart group, one Compose can run again on its own. Screen and Header don't run.

    Step 4 of 5
  5. Who runs it? The Recomposer: it waits for the next frame, then runs every marked group. Three writes before that frame cause one run.

    Step 5 of 5

The building block

mutableStateOf(0) doesn't give you a plain Int. It gives you snapshot state: a value holder, MutableState, whose every read and every write of .value Compose watches. (The name comes from Compose's snapshot system, which can also show different threads a consistent picture of all state at once. This module only needs the watching.) Only these objects are watched: change a plain var in some class and Compose never hears about it.

Whenever a composable reads .value while it runs, that's a state read, and Compose notes which group was running at that moment. With var count by remember { mutableStateOf(0) }, reading count calls a getter that reads .value, so it's the same read. What counts is where the read happens, not where the number ends up on screen: puzzle 7 follows that on a real screen.

A write that changes the value marks every group that read it. Then the marked groups run again, but a group can only run again on its own if the compiler plugin made it possible. For most composable functions it does: puzzle 0's start was startRestartGroup, and at the end the plugin hands the runtime a small lambda that calls the function again with the same arguments. That makes the function's group a restart group: a group Compose can run again by itself, from its start. (Android's docs call a block that can run again this way a restart scope; in this course it's the same thing, and puzzle 8 meets the word again.) Marked Counter runs again; Screen and Header don't run at all. (Some composables, such as Column, are inline functions: their content is part of the caller's group, so a read inside Column { } marks the function around it.)

Who does the running? The Recomposer, the part of the runtime that decides when marked groups run. It doesn't run anything at the moment of the write. It waits for the next frame, then runs every marked group, so three writes before one frame cost one run, showing the latest value. And if a write puts in the same value that's already there, nothing is marked at all.

Go deeperWhy wait for the frame?
A drag handler writes a new position 5 times between two frames. What would running the readers at each write cost, and what does the screen show either way?

Running at each write would run the readers 5 times, but the screen only changes once per frame, so 4 of those runs would never be seen. Waiting for the frame runs them once, with the latest position.

Back to the main puzzle

The analogy

The lighting cues of the play. Every scene that uses the blue light signs the blue cue's card (a state read). When the director changes the blue light, the stage manager flags exactly the scenes on that card, and redoes them before the next performance (the Recomposer at the next frame), starting each from its first line (a restart group). Changing the cue three times during one afternoon still means one redo that night. Setting it to the colour it already has flags nothing.

Check yourself

Header reads one snapshot state, title, and Counter reads another, count. A tap changes only title. What runs again at the next frame?

Not quite. Screen didn't read title, so Screen isn't marked; Header restarts on its own.

Not quite. Marks follow reads: Counter never read title, so it isn't marked.

Right. A write marks only its own readers.

Counter shows count, now 4. A Reset button's handler sets count to 4, then another tap sets it to 4 again. What runs at the next frame?

Not quite. The Recomposer never runs per write; it waits for the frame, and here nothing was marked at all.

Not quite. Several writes cost one run only when they change the value; these changed nothing, so nothing was marked.

Right. A write of the same value marks nothing.

A ViewModel keeps the count in a plain var field instead of mutableStateOf. Counter shows it, and a tap adds 1. What does the screen show?

Right. Only snapshot state is watched.

Not quite. A restart group can run again, but only when something marks it.

Not quite. The Recomposer only runs groups that a watched write marked; a plain var is never watched. Re-read that part ↑

Sources
  • Compose runtime source: SnapshotState.kt ("The MutableState class is a single value holder whose reads and writes are observed by Compose."; "If [value] is written to with the same value, no recompositions will be scheduled.")
  • Android Developers: Jetpack Compose phases ("When you read the value of a snapshot state during one of the phases listed previously, Compose automatically tracks what it was doing when it read the value."; "Under the hood of the property delegate, "getter" and "setter" functions are used to access and update the State's value."; "Each block of code that can be re-executed when a read state changes is a restart scope.")
  • Android Developers: State and Jetpack Compose ("Mutable objects that are not observable, such as ArrayList<T> or a mutable data class, are not observable by Compose and don't trigger a recomposition when they change.")
  • Compose compiler plugin source: ComposableFunctionBodyTransformer.kt ("Restartable composable functions get wrapped with "restart groups"."; "we generate a lambda that teaches the runtime how to "restart" that group"; and, for lambdas: "val restartable = declaration.shouldBeRestartable() && !inlineLambdaInfo.isInlineLambda(declaration)")
  • Android Developers: Thinking in Compose ("recomposing the entire UI tree can be computationally expensive, which uses computing power and battery life.")
  • Compose runtime source: Recomposer.kt ("The scheduler for performing recomposition and applying updates to one or more [Composition]s."; and, where it waits: "Align work with the next frame to coalesce changes.")
  • Compose foundation source: Column.kt (public inline fun Column()

What you now know

mutableStateOf makes snapshot state; Compose watches every read and write of its value. While a composable runs, each state read is noted against the group that was running. A write that changes the value marks those groups, and at the next frame the Recomposer runs each marked restart group again, from its start. Nobody else runs. Writing the same value marks nothing, and several writes before one frame cause one run.