Uli Bubenheimer
05/28/2024, 1:43 PMrememberUpdatedState
writes its state twice on initial composition. I wonder if it is possible to avoid this, and if so, under what circumstances it could make sense.
fun <T> rememberUpdatedState(newValue: T): State<T> = remember {
mutableStateOf(newValue)
}.apply { value = newValue }
For example, when there are several rememberUpdateState
usages in one place, say 6 or 7 or 13, one might combine them into a single state holder object. Maybe in this case an optimization like that could make more sense?
class Holder(
prop1: Type1,
prop2: Type2,
//...
prop7: Type7
) {
var prop1 by mutableStateOf(prop1)
var prop2 by mutableStateOf(prop2)
//...
var prop7 by mutableStateOf(prop7)
}
val updatedState = remember { Holder(prop1, prop2, /*...*/, prop7) }.also {
it.prop1 = prop1
it.prop2 = prop2
//...
it.prop7 = prop7
}
I imagine if I used composer compiler plugin APIs directly then I could skip the extra assignments, for example like this: if (!currentComposer.inserting) { it.prop1 = prop1 }
Filip Wiesner
05/28/2024, 1:46 PMYou mean the default value and then the assignment inwrites its state twice on initial compositionrememberUpdatedState
apply
? Why is that a problem? It's the same valueUli Bubenheimer
05/28/2024, 1:47 PMFilip Wiesner
05/28/2024, 1:49 PMUli Bubenheimer
05/28/2024, 1:51 PMFilip Wiesner
05/28/2024, 1:54 PMUli Bubenheimer
05/28/2024, 2:18 PMStylianos Gakis
05/28/2024, 2:32 PMAlbert Chang
05/28/2024, 3:07 PMUli Bubenheimer
05/28/2024, 3:39 PMrememberUpdatedState
's implementation has popped up for me in various different contexts. This is what triggers my interest in understanding the pattern's performance implications in general. rememberUpdatedState
is focused on a single state, so optimization may not make much sense, but the scales might tip a different way when double writing, say, 13 states at once.shikasd
05/28/2024, 3:44 PMUli Bubenheimer
05/28/2024, 3:48 PMcurrentComposer.inserting
without my app code blowing up? If not then none of this really matters.shikasd
05/28/2024, 3:50 PMinserting
, it should be fine to use it, changing this flag's behavior would blow up a lot of things internallyUli Bubenheimer
05/28/2024, 3:53 PM