Taming Recomposition
Stable params and fewer recompositions.
Recomposition: The Compose Performance Knob
In Jetpack Compose, the UI is described by functions. When state changes, Compose recomposes: it re-runs the composables that read that state to update the screen.
Recomposition is normal and cheap when scoped well. But when it happens too often or over too much of the tree, it becomes the #1 cause of Compose jank. This lesson teaches you to keep recomposition small and rare.
Seeing Recomposition Counts
Before fixing, measure. The Layout Inspector in Android Studio shows recomposition counts per composable in real time. You can also use the Compose compiler's metrics, or a quick debug counter.
A simple trick: a SideEffect increments a ref each time a composable recomposes, so you can log surprises during development.
@Composable
fun RecompositionCounter(tag: String) {
val count = remember { mutableStateOf(0) }
SideEffect { count.value++ }
Log.d("Recompose", "$tag recomposed ${count.value} times")
}
// Drop RecompositionCounter("PriceLabel") inside a composable
// to watch how often it re-runs while you interact.All lessons in this course
- Measuring Performance
- Taming Recomposition
- Memory Leaks and Fixes
- Startup and Baseline Profiles