Stylianos Gakis
11/11/2021, 9:20 AMremember
something or not. More in thread: 🧵Stylianos Gakis
11/11/2021, 9:21 AM@Composable
private fun Title(
title: String,
locale: Locale,
) {
Text(
text = title.uppercase(locale),
)
}
From what I understand, if this recomposes for whatever reason, .uppercase
will be called again (uppercase may potentially not be too expensive but I don’t know that right now. Also the function could be something a bit more expensive in another scenario). If this happens to be inside a composable that for example animates, therefore recomposes multiple times it could be called rapidly many times.
So if we change it to something like this:
@Composable
private fun Title(
title: String,
locale: Locale,
) {
val titleText = remember(title, locale) {
title.uppercase(locale)
}
Text(
text = titleText,
style = MaterialTheme.typography.overline
)
}
This should only change when either title
or locale
change.
So the question are:
• If this was part of a big composable, is this an optimization that one would try and do, or is this considered premature optimization? (I don’t like the extra code complexity, it can get out of hand if we do that for every single thing)
• If this is a simple leaf node of some composable. If that composable recomposes a lot of times per second without changing title
or locale
, is compose going to skip recomposing this leaf composable because it can see that it doesn’t need to?Zach Klippenstein (he/him) [MOD]
11/11/2021, 2:57 PMZach Klippenstein (he/him) [MOD]
11/11/2021, 2:58 PMStylianos Gakis
11/11/2021, 3:03 PMjava.util.Locale
I’m working with, is there a way for me to debug and see if compose does in fact infer if it’s stable or not?
I’ve seen the proposal about extracting out a state holder class to make the code look a bit cleaner. Do you know if there are any of the new compose codeLabs that cover this? I am afraid I don’t really know when I should start thinking about it and how to do it properly.Zach Klippenstein (he/him) [MOD]
11/11/2021, 3:10 PMZach Klippenstein (he/him) [MOD]
11/11/2021, 3:11 PMStylianos Gakis
11/11/2021, 3:15 PMZach Klippenstein (he/him) [MOD]
11/11/2021, 3:18 PM-Pandroidx.enableComposeCompilerReports=true
Zach Klippenstein (he/him) [MOD]
11/11/2021, 3:21 PMrememberUpdatedState
). Or depending on the logic maybe derivedStateOf
can be used somehow.Zach Klippenstein (he/him) [MOD]
11/11/2021, 3:22 PMStylianos Gakis
11/11/2021, 3:30 PMStylianos Gakis
11/11/2021, 3:31 PMenableComposeCompilerReports
flag goes, I am afraid I am not familiar with working with those before. Even after adding it, where would I be able to access this report? I couldn’t find any documentation on it.Zoltan Demant
11/11/2021, 3:33 PMZach Klippenstein (he/him) [MOD]
11/11/2021, 3:37 PM-composables.txt
. The contents should be self-explanatoryZach Klippenstein (he/him) [MOD]
11/11/2021, 3:38 PMremember
– keys are always compared using equals
IIRCStylianos Gakis
11/11/2021, 4:09 PMenableComposeCompilerReports
flag to output the file for me, not sure if I am not finding the file itself (ran find . -name '*composables.txt'
) or if I am applying the flag wrong (ran all ./gradlew -Pandroidx.enableComposeCompilerReports=true app:[build|assemble|install]Debug
) 🤕Zach Klippenstein (he/him) [MOD]
11/11/2021, 5:02 PM-Pandroidx.enableComposeCompilerMetrics=true
as wellStylianos Gakis
11/11/2021, 5:11 PM./gradlew app:buildDebug -Pandroidx.enableComposeCompilerReports=true -Pandroidx.enableComposeCompilerMetrics=true
nop, nothing.
I really don’t want to be wasting your time btw, but all of this seems to not be (public) documented behavior for me to look up myself 😕
I feel like I must be messing up somewhere and I don’t even know whereZach Klippenstein (he/him) [MOD]
11/11/2021, 5:14 PMLocale
isn’t gonna be stable so if you can’t annotate the parameter directly then you could try wrapping it in a data class or somethingZach Klippenstein (he/him) [MOD]
11/11/2021, 6:16 PMcompile.kotlinOptions.freeCompilerArgs += listOf(
"-P", "plugin:androidx.compose.compiler.plugins.kotlin:metricsDestination=/path/to/folder",
"-P", "plugin:androidx.compose.compiler.plugins.kotlin:reportsDestination=/path/to/folder",
)
Orhan Tozan
11/11/2021, 6:35 PMStylianos Gakis
11/12/2021, 2:24 PMUnsupported plugin option: androidx.compose.compiler.plugins.kotlin:metricsDestination=/reports/compose
Unsupported plugin option: androidx.compose.compiler.plugins.kotlin:reportsDestination=/reports/compose
depending on which one I put first.
Do you know if this is only supported by a specific compose version? This comment might be relevant. I tried with 1.0.4 and 1.0.5 but neither of them worked.