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

Robert Jaros

03/13/2024, 9:51 AM
Hello. When doing some experiments with compose runtime I'm observing a significant performance degradation with a repeatable dependence on the number of recompositions. I've even made a chart of my data (see 🧵). Can anybody explain what could be the cause of this? Perhaps someone has encountered a similar issue?
compose.png
As you can see there is a significant degradation at the beginning, then between 7K and 9K recompositions and again between 13K and 15K.
I'm using compose multiplatform 1.6.0 on web targets (js, wasm).
Thankfully it seems to be problem in my code and not compose itself 😉
z

Zach Klippenstein (he/him) [MOD]

03/13/2024, 2:51 PM
This doesn’t seem surprising - the more work you do, the more time it will take to do it
r

Robert Jaros

03/14/2024, 9:09 AM
The direct reason of the issue was creation of new
MutableState
instances and execution of composable functions subscribed to this state on every recomposition. The state was created with the
remember
function, but the composables were continuously removed and added to the composition, so
remember
didn't help. The strange thing, I'm not sure I understand, is what should happen to the state object and all subscriptions when composable leaves the composition. I think it should be canceled and disposed, and at the first look it was happening (the composable functions were not executed). But with a great number of recompositions the performance was degrading, as if compose was still referencing those "disposed" state objects and processing them somehow.
z

Zach Klippenstein (he/him) [MOD]

03/14/2024, 4:34 PM
adding and removing things from the composition isn’t free. If you’re doing that enough on every frame, you’re going to see a performance hit. I’m curious why you’re adding/removing so much?
r

Robert Jaros

03/14/2024, 4:44 PM
I'm experimenting with server side rendering by running a compose web app in nodejs, orchestrated and recomposed by incoming requests. I realize compose was definitely not designed for this 😉, but in general it seems to work quite fine (especially after resolving this performance issue and with wasm target speed).
z

Zach Klippenstein (he/him) [MOD]

03/14/2024, 4:50 PM
that seems fine in theory
doesn’t explain why there’s so many recompositions. Maybe the models coming from your server aren’t being compared correctly?
r

Robert Jaros

03/14/2024, 4:56 PM
I was generating these recompositions on purpose just to stress test the engine.
3 Views