Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Paywalls: optimize AdaptiveComposable #1377

Merged
merged 1 commit into from
Oct 20, 2023
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Paywalls: optimize AdaptiveComposable
This fixes the performance issue where it was constantly laying out, consuming about 10% CPU non-stop.

With this change, after the initial layout, CPU usage is 0% on a paywall.
  • Loading branch information
NachoSoto committed Oct 19, 2023
commit 299a11ec7ff845769d82f339ce451e85ea45b1c3
Original file line number Diff line number Diff line change
Expand Up @@ -27,23 +27,27 @@ internal fun RowScope.AdaptiveComposable(composables: List<@Composable () -> Uni
modifier = Modifier
.fillMaxWidth()
.onGloballyPositioned { coordinates ->
maxSize = coordinates.size.width
if (coordinates.size.width != maxSize) {
maxSize = coordinates.size.width
}
}
.align(Alignment.CenterVertically),
) {
// Offscreen measurement
Box(
Modifier
.graphicsLayer(alpha = 0.2f)
.graphicsLayer(alpha = 0.0f)
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This didn't have an impact, but I realized it was the wrong value.

.fillMaxWidth()
.wrapContentWidth(unbounded = true)
.align(Alignment.Center),
) {
composables.forEachIndexed { index, view ->
Box(
Modifier.layout { measurable, _ ->
val placeable = measurable.measure(Constraints())
viewSizes[index] = placeable.width
if (viewSizes[index] == 0) {
viewSizes[index] = measurable.measure(Constraints()).width
}

layout(0, 0) {}
},
) {
Expand Down