Compose Performance & Stability Interview Questions
UI › Compose UI
What is recomposition skipping, and what makes a composable skippable?
What a strong answer covers: When a parent recomposes, Compose can skip a child whose parameters are all stable and equal to the last time. A type is stable if Compose can trust that equal means unchanged and that it notifies of changes: primitives, String, immutable data classes with stable fields, and State. With strong skipping mode (default since Kotlin 2.0.20), unstable parameters are compared by instance equality, so far more composables are skippable.
Why are List and other collections often treated as unstable, and how do you deal with that?
What a strong answer covers: List is an interface that could be backed by a MutableList, so the compiler can't prove it won't change. Options: use kotlinx.collections.immutable (ImmutableList, PersistentList), mark a wrapper class @Immutable or @Stable, add a stability configuration file listing types, or rely on strong skipping where the same list instance is skipped. Don't mutate lists in place.
How would you find out why a screen is recomposing too much?
What a strong answer covers: Use Layout Inspector's recomposition counts to see which composables recompose and skip; enable Compose compiler reports and metrics to see which classes are unstable and functions not skippable; add composition tracing to Perfetto system traces. Common culprits: reading fast-changing state high up, unstable lambdas or classes, creating new objects in parameters, and missing remember.
What are the most effective techniques to improve Compose performance?
What a strong answer covers: Defer state reads to layout or draw with lambda modifiers (offset { }, drawBehind, graphicsLayer { }); use derivedStateOf for state that changes faster than the UI needs; remember expensive calculations; keys and contentType in lazy lists; stable, immutable UI models; avoid backwards writes (writing state that was just read); and always test release builds with R8 and a Baseline Profile.
What is a backwards write in Compose, why can it cause an infinite recomposition loop, and how do you spot one?
What a strong answer covers: A backwards write is writing to a state object after it was already read in the same composition, for example incrementing a counter in a composable body after displaying it. The write invalidates the scope that just read it, so Compose recomposes, which writes again, looping forever every frame. You spot it as constant recomposition counts in Layout Inspector or high CPU on an idle screen. Fix by moving writes into event handlers or effects.