Before explaining the concepts of restartable and skippable, I’ll first explain what recomposition is.

Recomposition is the process of calling your composable functions again when inputs change. This happens when the function’s inputs change. When Compose recomposes based on new inputs, it only calls the functions or lambdas that might have changed, and skips the rest. — Android Documentation

The Compose compiler marks a function as restartable and skippable to determine whether it is skippable or restartable.

What is the Restartable and Skippable?

Restartable

Marking a function as restartable indicates that it can be called again if any changes occur in its inputs.

Skippable

Marking a function as skippable, it can be skip calling the function if the parameters haven’t changed since last call.

How Do We Know The Function Has Marked a Restartable and/or Skippable?

Write the script above to the build.gradle.kts (Project) file.

To get the result of the report, we can run this command,

./gradlew assembleRelease -PcomposeCompilerReports=true

This task will generate four files in the build directory of each module under the compose_compiler file.

  1. app_release-classes.txt -> A report on the stability of classes
  2. app_release-composables.txt -> A report on the restartability and skippability of the composables.
  3. app_release-composables.csv -> A csv version of the above text file.
  4. app_release-module.json -> Contains some overall stats.

After running the task, open up the app_release-composables.txt file and we will see all composable functions and each will be marked with whether they are restartable, skippable.

restartable scheme("[androidx.compose.ui.UiComposable]") fun ContactList(
unstable contactListState: ContactListState
)

This ContactList composable is restartable but not skippable. Why?

The ContactListState parameter has marked unstable. What does unstable mean in terms of stability, and how can I make it stable?

The stability of a composable’s parameters is crucial in determining whether it needs to be re-evaluated during recomposition in Compose.

Stable Parameters

  • If the parameters of a composable are stable and have not changed during recomposition, Compose skips it.
  • For example; String, Int, Float, ImmutableList types are stables parameters.

Unstable parameters:

  • If a composable has a unstable parameters, Compose always recomposes it even if the parameters have not changed.
  • For example; List, Map, Set types are unstable parameters. Also var declarations on Data class is unstable.
val numbers: List<Int> = mutableListOf(1, 2, 3)

Since standard Collection classes are defined as interfaces in Kotlin, their underlying implementation may be unstable. The Compose compiler can’t be sure of the immutability of this class since it only sees the declared type, and will mark it as unstable.

Source link