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

Daniel

02/27/2021, 10:56 PM
Hi, I have a composable that takes one heavy parameter that should never change and one lightweight parameter that should change a lot. I initially wrote it like this
Copy code
@Composable
private fun Page(page: PageRenderer /* @Immutable data class, never changes */, turn: Dp = 0.dp /* changes frequently in animations */) {
    Surface(
        Modifier
            .fillMaxSize()
            .background(page.background)
            .offset {
                IntOffset(
                    -turn
                        .toPx()
                        .roundToInt(), 0
                )
            }
            .drawWithCache {
                onDrawWithContent {
                    drawContent()
                    drawIntoCanvas {
                        Timber.i("Drawing into canvas")
                        page.paint(it)
                    }
                }
            },
        elevation = 15.dp
    ) {}
}
Based on my logs, I noticed that
drawIntoCanvas
was called every time
turn
changed, which I was very surprised by. (I thought the whole point of offset taking a lambda would prevent that) I fixed the issue by adding another component, but I'm confused by why this was necessary
Copy code
@Composable
private fun Page(page: PageRenderer, turn: Dp = 0.dp) {
    Surface(
        Modifier
            .fillMaxSize()
            .background(page.background)
            .offset {
                IntOffset(
                    -turn
                        .toPx()
                        .roundToInt(), 0
                )
            },
        elevation = 15.dp
    ) {
        Canvas(Modifier.fillMaxSize()) {
            drawIntoCanvas {
                Timber.i("Drawing into canvas")
                page.paint(it)
            }
        }
    }
🧵 3
a

Adam Powell

02/27/2021, 11:31 PM
feel free to file this with a repro on the issue tracker; unexpected performance characteristics are something we would like to track for further optimization, documentation, or both