skip to content

In a multi-module app on Jetpack Compose, a product row re-runs every time its screen recomposes although its data did not change; how do you find and fix the cause?

level: seniorimportance: must knowfreq 60%

answer

  1. count recompositions first
  2. compiler reports on a release build
  3. unstable plus a new instance
  4. models from a non-Compose module
  5. four ways to make it stable

basics

~20 s

Compose compiler reports usually show the row's parameter unstable (a List, or a model from a non-Compose module) and it is re-allocated each pass. Stop re-allocating, or make the type stable: @Immutable, a stability configuration file or a UI wrapper.

solid answer

~50 s

First confirm it: Android Studio's Layout Inspector shows how often each composable recomposed or skipped. Then switch on the Compose compiler reports with `reportsDestination` (and `metricsDestination`) in the `composeCompiler` block and build a release variant. `<module>-composables.txt` lists each parameter as `stable` or `unstable`; `<module>-classes.txt` shows why a class is unstable, typically a `List` property or a type from a module built without the Compose compiler. With strong skipping, on since Kotlin 2.0.20, an unstable parameter is compared by instance, so the row re-runs only when it gets a new object; look for mapping to a UI model inside composition, or a data source emitting equal new objects. Fix the allocation first. Otherwise make the type stable: `@Immutable` or `@Stable` (since Compose 1.9 a non-Compose module can use them via `runtime-annotation`), a `stabilityConfigurationFile` entry, or a UI-module wrapper.

code

kotlin · 23 lines
kotlin
// :core:model, built without the Compose compiler
data class Product(val id: Long, val name: String, val tags: List<String>)

// :feature:catalog
data class ProductRowUi(val id: Long, val title: String, val tags: List<String>)

fun Product.toRowUi() = ProductRowUi(id = id, title = name, tags = tags)

@Composable
fun ProductRow(row: ProductRowUi, onOpen: (Long) -> Unit) {
    Text(row.title, modifier = Modifier.clickable { onOpen(row.id) })
}

@Composable
fun CatalogRows(products: List<Product>, onOpen: (Long) -> Unit) {
    Column {
        products.forEach { product ->
            // ProductRowUi is unstable (List property) and allocated anew on every pass,
            // so === never matches and every row re-runs whenever CatalogRows does.
            ProductRow(row = product.toRowUi(), onOpen = onOpen)
        }
    }
}

go deeper

for a junior

Know that Compose can skip a row whose inputs did not change, and that an unexpected re-run usually means an input looked changed to Compose.

for a middle

Read a classes report and say why a type is unstable: a List property, a var, or a type from a module the Compose compiler did not process.

for a senior

Separate the two worlds: before strong skipping an unstable parameter never skips; after, it skips by instance, so hunt the allocation. Fix the allocation before reaching for annotations.

for a principal

Turn one diagnosis into team practice: where UI models are mapped, which modules may carry stability annotations, and when the reports are worth reading.

## The symptom A catalog screen in a multi-module app shows a list of product rows. Something above the rows changes often (the screen fades its toolbar as you scroll), so the screen composable recomposes on every scroll frame. Each pass, every `ProductRow` re-runs too, though no product changed. Why the screen recomposes so often is a question for the lesson on state and recomposition; the question here is why the rows are not **skipped**. ## Step 1: confirm before you optimise - **Layout Inspector** in Android Studio shows, per composable, how many times it recomposed and how many times it was skipped. If `ProductRow`'s recomposition count climbs while you scroll and its skip count does not, the row is not being skipped. - Measuring what that costs in frame time on a device is covered in the lesson on Jetpack Benchmark. The Compose documentation warns against chasing skippability where there is no measured problem. ## Step 2: read the compiler's stability verdict Compose compiler reports are off by default. Turn them on per module and build a **release** variant, which the documentation recommends for accurate results: ```kotlin composeCompiler { reportsDestination = layout.buildDirectory.dir("compose_compiler") metricsDestination = layout.buildDirectory.dir("compose_compiler") } ``` `reportsDestination` writes three files per module: 1. `<module>-composables.txt`: each composable with its tags (`restartable`, `skippable`) and each parameter marked `stable` or `unstable`; 2. `<module>-classes.txt`: each class with the stability of every property and the inferred `<runtime stability>`; 3. `<module>-composables.csv`: the composables report as CSV. `metricsDestination` adds overall statistics, such as the number of composables in the module. A typical finding: ```plaintext unstable class ProductRowUi { stable val id: Long stable val title: String unstable val tags: List<String> <runtime stability> = Unstable } ``` ## Step 3: work out why an unstable parameter re-runs | Toolchain | Unstable parameter means | |---|---| | Kotlin before 2.0.20, or strong skipping turned off | the row is never skipped | | Kotlin 2.0.20 or newer, strong skipping on by default | the row is skipped only when the **same instance** is passed | On a current toolchain, an unstable row that re-runs every pass is receiving a **new object** every pass. The usual sources: - the screen maps domain models to a UI model **inside composition**, such as `ProductRow(product.toRowUi())`, so each pass allocates a fresh, equal object; - the data source emits new objects with equal values, as a database reader does when it re-reads a whole list after any row changes; - a model from a **data module the Compose compiler never ran on** is inferred unstable, even when it is a `data class` of `val`s, because the compiler can only infer stability for types it processed or that are marked stable. ## Step 4: fix it, cheapest change first 1. **Stop the re-allocation.** Map to UI models where the screen's state is produced, not in the composable body; if the instance is reused, `===` matches and the row skips. 2. **Make the type stable by construction.** Use only `val` properties of stable types. For collections, a kotlinx immutable collection such as `ImmutableList` is recognised as stable, though the library is still alpha. 3. **Annotate it.** `@Immutable` or `@Stable` tells the compiler to treat the class as stable, so equal new instances compare with `equals()`. Since Compose 1.9 the annotations live in `androidx.compose.runtime:runtime-annotation`, so a data module can use them without depending on the Compose runtime. The annotation is a promise the compiler does not check. 4. **Use a stability configuration file.** List classes you do not own or whole packages, such as `com.example.model.*` or `kotlin.collections.*`, and pass it with `stabilityConfigurationFile`. Same promise, made centrally. 5. **Wrap** data-layer types in UI-module classes the compiler can infer. ## Traps - Annotating a class that holds a **large list** `@Immutable` makes Compose compare every element with `equals()` on each pass, which can cost more than the recomposition you avoided. - Marking a class stable that is mutated in place gives you a row that **skips when it should not** and shows stale data. - Fixing the row does not fix the screen; the per-frame recomposition of the parent still needs its own fix.

  • The model class lives in a data module without Compose. What are your options to make it stable?
    List it, or its package, in the stability configuration file; annotate it with `@Immutable` or `@Stable` by depending on `runtime-annotation` (Compose 1.9 and later), which does not pull in the Compose runtime; apply the Compose compiler to that module; or wrap it in a UI-module class the compiler can infer.
  • Why can marking a class with a large list @Immutable make things slower?
    Once the class is stable, Compose compares it with `equals()`, and a data class's `equals()` compares the list element by element on every pass. Under strong skipping an unstable list is compared by reference, which is constant-time; trading that for a full comparison can cost more than the recomposition it avoids.
  • After the fix, how do you confirm that the product row now skips?
    Rebuild the release variant and re-read the reports: the row's parameter should now read `stable`, or the allocation should be gone from composition. Then scroll with Layout Inspector open and watch the row's skip count climb instead of its recomposition count. Whether frame time improved is a question for benchmarking.

  • With strong skipping on, an unstable parameter means the row is never skipped.
  • A data class of vals from any module is always inferred stable.
  • @Immutable makes the compiler verify that a class cannot change.
  • Annotating every model @Immutable is free and always speeds things up.
  • Compose compiler reports are written by default on every build.

Assumes Compose 1.12 with Kotlin 2.0.20 or newer (strong skipping on by default); runtime-annotation has carried @Stable and @Immutable since Compose 1.9.