I'm starting to try Jetpack Compose and was wondering why a simple list was so poorly optimised when I scrolled. Even in release build. So I started adding logs and I realized that sometimes the list seemed to be recreating itself.
Here is my composable :
@OptIn(ExperimentalMaterialApi::class)
@Composable
fun MissionsList() {
val list = remember { (0..75).map { it.toString() } }
LazyColumn(
verticalArrangement = Arrangement.spacedBy(22.5.dp)
) {
Log.e("LazyColumn", "CREATE")
items(
count = list.size,
key = { list[it] }
) {
Card(
modifier = Modifier
.fillMaxWidth()
.height(100.dp),
elevation = 0.dp,
shape = RoundedCornerShape(20.dp),
onClick = { /*TODO*/ }
) {
Log.e("card", "${list[it]}")
Text(
text = list[it],
modifier = Modifier.padding(15.dp)
)
}
}
}
}
Here is output :
15:20:30.738 LazyColumn E CREATE
15:20:30.810 card E 0
15:20:30.828 card E 1
15:20:30.841 card E 2
15:20:30.866 card E 3
15:20:30.879 card E 4
15:20:31.250 Chip E 1
15:20:36.404 card E 6
15:20:36.496 card E 7
15:20:36.560 card E 8
15:20:36.645 card E 9
15:20:36.730 card E 10
15:20:36.840 card E 11
15:20:37.043 card E 12
15:20:38.324 card E 13
15:20:38.381 card E 14
15:20:38.417 card E 15
15:20:38.449 card E 16
15:20:38.478 card E 17
15:20:38.504 card E 18
15:20:38.529 card E 19
15:20:38.560 card E 20
15:20:38.570 card E 21
15:20:38.579 card E 22
15:20:38.610 card E 23
15:20:38.653 card E 24
15:20:38.685 card E 25
15:20:38.694 card E 26
15:20:38.717 card E 27
15:20:38.761 card E 28
15:20:38.782 card E 29
15:20:38.793 card E 30
15:20:38.820 card E 31
15:20:38.859 card E 32
15:20:38.892 card E 33
15:20:38.921 card E 34
15:20:38.972 card E 35
15:20:39.006 card E 36
15:20:39.020 LazyColumn E CREATE
15:20:39.027 card E 37
15:20:39.045 card E 30
15:20:39.047 card E 31
15:20:39.048 card E 34
15:20:39.066 card E 35
15:20:39.069 card E 32
15:20:39.070 card E 33
15:20:39.072 card E 36
15:20:39.131 card E 38
15:20:39.169 card E 39
15:20:39.218 card E 40
15:20:39.281 card E 41
15:20:39.354 card E 42
...
I noticed that about every 30 items the list is recreated. What did I miss?
CodePudding user response:
The LazyColumn uses a sliding window to track the key for each index, which can cause the lambda that contains your log statement to be recalculated. That doesn't mean that all the work Compose/LazyColumn did to generate your UI is thrown out though, so this shouldn't cause any significant performance impact.
You mentioned seeing performance issues in a release build. If that's optimized with R8 and you're using baseline profiles, scrolling a list like this should be plenty performant.