Again a question about `@Immutable` . This is the ...
# compose
d
Again a question about
@Immutable
. This is the quote from Chris Banes' article:
An object that is immutable means that ‘all publicly accessible properties and fields will not change after the instance is constructed’. This characteristic means that Compose can detect ‘changes’ between two instances very easily.
But how exactly does compose "detect changes"? If function parameter is immutable, what compose will do with it? referentially compare prev/next values? run
prev.equals(next)
? I can't seem to find any info about this in docs, most places say that "this enables some optimizations".
z
Some of these questions are answered by the docs on `StableMarker`: https://developer.android.com/reference/kotlin/androidx/compose/runtime/StableMarker
When all types passed as parameters to a
Composable
function are marked as stable then the parameter values are compared for equality based on positional memoization and the call is skipped if all the values are the equal to the previous call.
d
Thanks! "based on positional memoization" bit is still unclear to me, afaik positional memoization is about composable function calls and position of their emissions in the tree, but how is it related to the function parameters?... (I'll need to read more about this I guess)
d
Is
var thing by mutableStateOf("stuff")
considered
@Stable
or
@Immutable
because the delegate property is not changing?
From the docs linked, it looks like
@Stable
is true by not
@Immutable
.
The nuance around
@Immutable
has some serious perfomance implications.
s
From a compiler perspective, Stable is the same as Immutable, the different annotations are there mostly for documentation reasons
Thanks! "based on positional memoization" bit is still unclear to me, afaik positional memoization is about composable function calls and position of their emissions in the tree, but how is it related to the function parameters?... (I'll need to read more about this I guess)
If all parameters are stable, they are memoized whenever a function is called If the function is called again in that position, parameter values are compared and function can skip if values are the same
Practically, stability allows making functions restartable (and sometimes memoize lambdas), and that's about it
The documentation is intentionally vague to avoid enumerating all potential optimizations we use it for
d
thank you! Do parameter values get compared using
equals
?
s
Yes, except for function references, those are compared using referencial equality
d
Thank you, finally I have more in-depth understanding about this 🙂