Looking at the documentation of Immutable, there's a code example:
@Immutable
data class Person(val name: String, val phoneNumber: String)
@Composable
fun PersonView(person: Person) {
Column {
Row {
Text("Name: ")
Text(person.name)
}
Row {
Text("Phone: ")
Text(person.phoneNumber)
}
}
}
@Composable
fun PeopleView(people: List<Person>) {
Column {
for (person in people) {
PersonView(person)
}
}
}
and a caption:
Marking Person immutable allows calls the PersonView Composable function to be skipped if it is the same person as it was during the last composition.
My question is:
If recomposition occurs only when arguments of @Composable
function change and data classes like Person
in the code above (i.e. containing only primitive vals) cannot be changed without creating a new instance, then what's the optimization here?
When would the calls to the PersonView Composable function be skipped compared to the same code but without the @Immutable
annotation? Is it somehow related to the mutable/unstable argument people: List<Person>
of PeopleView
?