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

Maik

09/17/2020, 9:06 AM
I have a question of understanding about re-composition. I have written the following simple code example:
@Composable
fun StateDemo() {
var counterOne by remember { mutableStateOf(0) }
var counterTwo by remember { mutableStateOf(0) }
var items = remember { mutableStateListOf<Int>() }
Column {
Button(onClick = { counterOne++ }) {
Text("One")
}
Button(onClick = { counterTwo++ }) {
Text("Two")
}
Button(onClick = { items.add(counterOne) }) {
Text("List")
}
MyText(counterOne)
MyText(counterTwo)
MyList(items)
}
}
@Composable
fun MyText(counter: Int) {
Text("counter: $counter (${System.currentTimeMillis()})")
}
@Composable
fun MyList(items: MutableList<Int>) {
Text("list: ${items.size} (${System.currentTimeMillis()})")
}
Unfortunately, the behavior in case of re-composition is not completely clear to me. If I click the button "One", the function "*MyText(counterOne)*" is re-drawn, the function "*MyText(counterTwo)*" remains completely unaffected. The behavior with the button "Two" is similar. Unfortunately in both cases the function "*MyList(items)*" is also redrawn. The behavior is not clear to me. What am I doing wrong! I want to prevent this function from being drawn, because in my opinion it is not involved in the re-composition. Many thanks in advance!
s

Se7eN

09/17/2020, 9:21 AM
@jim How about using
mutableStateOf<List<Int>>()
?
m

Maik

09/17/2020, 9:21 AM
@jim Thanks for the tip with the
SnapshotStateList
. I know that "Compose" can call the function at any time, I just wanted to keep the parts to be drawn as small as possible for performance reasons.
j

jim

09/17/2020, 9:22 AM
@Se7eN No, that would not do what you want, as
List<Int>
does not guarantee the list won't change, it only states avoids the reference holder from being able to call the mutation methods.
👍 1
@Maik Yeah, totally makes sense. Watching for unnecessary recompositions is a good way to optimize and ensure your code is performant, so you're doing the right thing there. Just wanted to make note so people don't make assumptions though. Also, FWIW, in this case, it is technically possible for us to have been smarter and seen that the
MutableList
passed into
MyList(items)
is going to be a
SnapshotStateList
and thus we could be smarter and skip in that situation. I kinda thought we had already implemented that, but maybe we had to turn it off due to bugs in the implementation? Anyway, you could probably file a bug if you'd like, worst case it would be closed as a duplicate if we're already tracking it.
👍 1
m

Maik

09/17/2020, 9:28 AM
Thank you very much!
c

Chuck Jazdzewski [G]

09/17/2020, 3:57 PM
@Leland Richardson [G] Just started working on a feature to do what @jim describes above. He is streaming part of his work on YouTube and Twitch. A recording of the first "episode" can be found here (

https://www.youtube.com/watch?v=bg0R9-AUXQM

) which also described much of the mechanism the compiler uses to decide if a function should skip or not as well as what changes we are making to make the above work the way you expected.
💯 2
4 Views