Where should I start digging when looking to optim...
# compose
z
Where should I start digging when looking to optimize a large/complex screen that stutters? 🧵👀
b
Be more agressive with remember and its keys to reduce computations
👍 1
👍🏽 1
z
In the good old days of XML, Id look at things like the view hierarchy and try to reduce its depth. With compose, Im not sure where or what I should look at to start with. Im testing this on a release build with R8 enabled, theres lag when navigating to/from the screen, and theres overall lag while the screen is active.
s
I’ve found adding some
SideEffect
to log when a recomposition happens in certain scopes a valuable tool. I’ve had cases where I was somehow triggering an infinite loop of recompositions and this is how I found out.
👍 2
2
👍🏽 1
c
I agree with @Stylianos Gakis. This article explains in more detail how you can do this. https://www.jetpackcompose.app/articles/donut-hole-skipping-in-jetpack-compose
👍🏽 1
z
Thanks, those are all useful tips! How many recompositions would be acceptable? With
AnimatedContent
Im seeing 1-3; and turning animations off results in 1 (in rare cases, 2).
a
I would also advise you to profile your screen with CPU Profiler, for me it showed that main thread spent 90% of time notifying derivedStateOf observers, and removing them drastically improved frame rate on my screen
👍 2
👍🏽 1
z
Thanks @Alexander Sitnikov! Im seeing a massive call to
measureAndLayout
which I guess is due to all the composables in the complex screen, pretty much nothing outside of that.
Im sorry 🤦🏽‍♂️ Somewhere along the line my build variant reverted to debug. The lag is all gone now that Im actually using a release build! I appreciate all the tips though!
😅 4
s
A classic, we’ve all been there 😅
🥲 2
k
Why is there so much performance difference in debug and release build?
s
Not 100% knowledgeable on the details, but the Google devs have all been suggesting that you should never look at performance on a non minified build especially with compose. (Example) https://kotlinlang.slack.com/archives/CJLTWPH7S/p1630248475315600?thread_ts=1630247931.314700&cid=CJLTWPH7S I remember reading something about how compose uses lambdas extensively and with minification there is a considerable difference. I'll try and link a better explanation why here if I ever stumble upon it