eygraber
10/14/2021, 11:41 PMLazyColumn/LazyRow
to use mutable state for the items, so that updating a property of a single item doesn't cause all the visible items to recompose?Francesc
10/15/2021, 2:03 AMeygraber
10/15/2021, 3:56 AMset
with the new element that has a stable key)Csaba Kozák
10/15/2021, 7:22 AMList
? I ask that because List
is not stable.eygraber
10/15/2021, 1:07 PMSnapshotStateList
eygraber
10/15/2021, 7:00 PMSnapshotStateList
is to have an observable list state, and not necessarily to reduce the scope of recomposition.Csaba Kozák
10/18/2021, 8:22 AMSnapshotStateList
is also @Stable
so if you pass that the a composable, it should enable skipping and smart recompositions.eygraber
10/18/2021, 8:54 AMMutableState<List>
just with a different mechanism of action (and maybe some extra/different optimizations).
In both cases adding/removing from the list will cause all visible items to recompose, as will updating a specific element. I believe this is because the list is passed to the items
function, and so that gets recomposed, which in turn either recomposes or recreates all of the items.
If that's acceptable for performance, then I guess that's fine, but if it's not then something needs to be done. The only way I've found to achieve that is to make the elements of the list use MutableState
for all properties, which is not the most ideal (from an architecture / data design perspective)Steffen Funke
11/03/2021, 7:43 PMeygraber
11/03/2021, 9:27 PMState
). For example, should I be concerned that mutating a SnapshotStateList
causes all visible items to recompose.
I think the answer is the standard "it depends" and "profile as necessary" that comes with optimization, but I'm not 100% if it falls under the same category as that.Zach Klippenstein (he/him) [MOD]
11/03/2021, 10:46 PMI’ve been assuming that any unnecessary recomposition is a “bad thing”,
The question in essence is if we should be generally reducing recomposition as much as possible, or if we should only aim to do that if there are performance issues that come up.Optimizing to reduce all possible unnecessary recompositions too early has the pitfalls of any premature optimization. The compose compiler and runtime already do a lot of work to avoid unnecessary work, and often recomposing a function ends up being less work than it looks if all the other composables it calls skip. So to answer your original question, using an immutable list is probably fine. The first thing to do would be decide whether using an immutable or mutable list makes the code simpler.
should we generally annotate state data classes with „@Stable” to avoid overcomposition?No, since the compiler should already infer data classes as stable. It’s very easy to get trigger-happy with
@Stable
annotations and suddenly you’ve got unstable classes that are incorrectly annotated and that will result in bugs.Steffen Funke
11/04/2021, 5:17 AM@Immutable
and therefore experienced no skipping / deep recompositions when using monolithic state classes. Will try that out today.
EDIT: Not working as expected: https://kotlinlang.slack.com/archives/CJLTWPH7S/p1636009011389800Zach Klippenstein (he/him) [MOD]
11/04/2021, 4:18 PMZach Klippenstein (he/him) [MOD]
11/04/2021, 4:20 PM