https://kotlinlang.org logo
#compose
Title
# compose
d

dimsuz

06/28/2023, 2:44 PM
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

Zach Klippenstein (he/him) [MOD]

06/28/2023, 2:47 PM
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

dimsuz

06/28/2023, 3:42 PM
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

dewildte

06/28/2023, 4:36 PM
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

shikasd

06/28/2023, 7:09 PM
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

dimsuz

06/29/2023, 10:10 AM
thank you! Do parameter values get compared using
equals
?
s

shikasd

06/29/2023, 10:47 AM
Yes, except for function references, those are compared using referencial equality
d

dimsuz

06/29/2023, 11:30 AM
Thank you, finally I have more in-depth understanding about this 🙂
99 Views