skip to content

What does Jetpack Compose's strong skipping mode, on by default since Kotlin 2.0.20, change about skipping composables and about lambdas?

level: middleimportance: should knowfreq 54%

answer

  1. restartable now implies skippable
  2. two comparison rules by stability
  3. lambdas wrapped in remember
  4. keyed on what the lambda captures
  5. two opt-out annotations

basics

~20 s

Strong skipping makes every restartable composable skippable, comparing stable parameters with equals() and unstable ones by instance (===). It also remembers each lambda inside a composable, keyed on its captures, so recomposition stops handing children new lambda instances.

solid answer

~40 s

Strong skipping is a Compose compiler mode, on by default since Kotlin 2.0.20. It changes two things. First, **all restartable composables become skippable**, even with unstable parameters: Compose compares stable parameters with `equals()` and unstable ones with instance equality, `===`, and skips the call when all match. Non-restartable composables stay unskippable. Second, **every lambda inside a composable is remembered**: the compiler wraps it in `remember`, keyed on its captures, with unstable captures compared by instance. Before this mode, a lambda capturing an unstable value was re-allocated on each pass, so a child taking it never skipped. Two consequences follow: mutating an unstable object in place leaves a skipped child showing stale data, and a fresh but equal instance still forces recomposition. You opt out with `@NonSkippableComposable` on a function or `@DontMemoize` on a lambda.

go deeper

for a junior

Know that recent Kotlin versions turn on strong skipping, so Compose can skip composables even when a parameter type is not stable.

for a middle

State the two changes: restartable implies skippable with equals or === by stability, and lambdas remembered on their captures. Name the two opt-out annotations.

for a senior

Diagnose the two bugs strong skipping exposes: stale UI from mutating an unstable parameter, and recomposition from a data source that re-allocates equal objects.

for a principal

Decide whether a codebase still needs stability annotations once strong skipping is on, weighing instance churn from the data layer against unchecked promises.

## Where it comes from The **Compose compiler** decides which composables can be skipped during recomposition. Since Kotlin 2.0 it ships with Kotlin as the Compose Compiler Gradle plugin, and from **Kotlin 2.0.20** it enables **strong skipping mode** by default. On Kotlin 2.0 releases before 2.0.20 you turned it on per module with `enableStrongSkippingMode = true` in the `composeCompiler` block. Strong skipping changes the compiler's behaviour in exactly two ways. ## Change 1: unstable parameters no longer block skipping Without strong skipping, a composable is skippable only if all its parameters are **stable**. One unstable parameter, such as a `List<Product>` or a class from a module the Compose compiler never processed, means Compose re-runs the function every time its parent recomposes. With strong skipping: - **every restartable composable is skippable**, whatever its parameters; - **stable** parameters are compared with `Object.equals()`; - **unstable** parameters are compared with instance equality, `===`; - **non-restartable** composables, such as those marked `@NonRestartableComposable`, remain unskippable. If every parameter matches its previous value under its rule, the call is skipped. ### What instance comparison means in practice Take `data class Contact(var name: String, var number: String)`. Because of the `var`s, Compose infers it unstable. | What the app does | Strong skipping on | Strong skipping off | |---|---|---| | Passes the same instance again | skipped | recomposed | | Mutates `contact.name` in place, then the parent recomposes | skipped, so the old name stays on screen | recomposed | | Passes a new instance with equal values | recomposed | recomposed | The middle row is why mutable classes passed as parameters are a bug either way: Compose only tracks changes to its own `State` objects, and an in-place write is invisible to it. The last row is why a data source that allocates new, equal objects on every emission still causes recomposition. When that happens often, annotating the class `@Stable` or `@Immutable` switches the comparison to `equals()`, as long as `equals()` is cheap. ## Change 2: lambdas are remembered The compiler always treats function types as stable. A common misconception is that a lambda capturing an unstable value is itself unstable; it is not. The old problem was allocation: without memoization, each recomposition created a **new** lambda object, so a child that took it as a parameter saw an unequal argument and recomposed. With strong skipping, every lambda inside a composable is wrapped in a `remember` call keyed on its captures: ```kotlin // What you write val onOpen = { navigateTo(product) } // What the compiler produces, in effect val onOpen = remember(product) { { navigateTo(product) } } ``` The keys follow the skipping rules: unstable captures by instance, stable captures by `equals()`. An ordinary `remember` compares all its keys with `equals()`, so this generated one is slightly different. ## Opting out 1. `@NonSkippableComposable` on a function keeps it restartable but never skipped. 2. `@DontMemoize` on a lambda expression, written `val onTap = @DontMemoize { ... }`, stops that one lambda being remembered. ## Costs and limits - Skippable functions and remembered lambdas generate a little more code, so APK size grows slightly; Google measured a few kilobytes on a sample app. - Strong skipping is not a cure for re-allocation. If a parent builds a new unstable object on every pass, `===` fails and the child re-runs. - It does not make collection parameters compare by content. A `List` stays unstable and is compared by reference, which is cheap; annotating a class that holds a large list `@Immutable` makes Compose compare every element on every pass. ## Why interviewers ask it Many answers to "why does my composable recompose" still describe the pre-2.0.20 world, where any unstable parameter meant no skipping. The question checks that you know the default moved, the two comparison rules, and the two new bugs it makes possible: stale UI from in-place mutation and recomposition from equal but new instances.

  • If strong skipping already skips unstable parameters, why would you still annotate a class @Stable?
    Strong skipping compares unstable parameters by instance. When a data source allocates new objects with equal values on each emission, as a database reader can for every row of a list, `===` fails and the row recomposes. Annotating the class switches the comparison to `equals()`, which pays off only when `equals()` is cheap.
  • Is a lambda that captures an unstable value an unstable parameter in Jetpack Compose?
    No. The compiler always treats function types as stable. Without strong skipping the problem was that such a lambda was re-allocated on every recomposition, so the child saw an unequal argument. Strong skipping remembers the lambda keyed on its captures, so the same instance is passed while the captures are unchanged.

  • Strong skipping compares every parameter with equals().
  • Under strong skipping, mutating a var on a parameter triggers recomposition.
  • A lambda that captures an unstable value is itself an unstable type.
  • Strong skipping makes @NonRestartableComposable functions skippable too.
  • Strong skipping must still be switched on by hand in every module.

Assumes Compose 1.12 built with Kotlin 2.0.20 or newer, where the Compose compiler enables strong skipping by default.