https://kotlinlang.org logo
#decompose
Title
# decompose
v

Vaibhav Jaiswal

03/01/2024, 1:13 PM
@Arkadii Ivanov I am facing this issue, In whatever composable I am passing a function reference of a function inside component, my composable recomposes no matter what Any solution to this Example:
Copy code
FeedPostItem(
    post = it,
    modifier = Modifier.fillMaxWidth(),
    onAction = component::handlePostAction
)
a

Arkadii Ivanov

03/01/2024, 1:22 PM
Well, you can try
onAction = remember(component) { component::handlePostAction }
. But I strongly advice to avoid premature optimisations, unless you have measuarable performance issues. Re-composition itself doesn't imply re-rendering, and it's usually OK to recompose even on every animation frame. Your composables inside
FreePostItem
should be still skipped if their state has not been changed.
v

Vaibhav Jaiswal

03/01/2024, 1:28 PM
Okay that makes sense But I can see my whole lazy column being recomposed, That wont cause any issue, even if the actual ui elements are getting skipped?
Even if it recomposes 20+ times?
a

Arkadii Ivanov

03/01/2024, 1:33 PM
It shouldn't! Compose is pretty smart. But if you really want to optimise, you can also try the new Strong Skipping Mode introduced in Compose 1.5.4. See the related article: https://medium.com/androiddevelopers/jetpack-compose-strong-skipping-mode-explained-cbdb2aa4b900
v

Vaibhav Jaiswal

03/01/2024, 1:34 PM
I already have Strong Skipping Enabled, still all my composables are recomposing due to lambdas
One of my screens have become very heavy and laggy in some devices
d

Djuro

03/01/2024, 1:36 PM
Are you sure this is not due to your compose function being unstable? There is pattern recommended by google when working with `ViewModel`s. Is this maybe what you are searching for? It recommends using a stateful and stateless composable to avoid recomposition Note: you can think of your component as of viewmodels
👍 1
v

Vaibhav Jaiswal

03/01/2024, 1:42 PM
@Djuro I'm already following this, the decompose component is only used in my screen level component and all other child components take the properties and lambda functions
d

Djuro

03/01/2024, 1:44 PM
is your
post
stable?
Could you check from compose compiler reports? You can find an article on how to get stability report here
v

Vaibhav Jaiswal

03/01/2024, 1:45 PM
yup
Post
is stable, all field are
val
and of Stable type
d

Djuro

03/01/2024, 1:58 PM
I am unsure than, could you provide the evidences from compose compiler reports so we are sue that the lambda is the one that's unstable
v

Vaibhav Jaiswal

03/01/2024, 2:03 PM
how to do that?
a

Arkadii Ivanov

03/01/2024, 2:30 PM
I think
FeedPostItem
can still recompose if the outer (containing) function also recomposes for whatever reason (e.g. if there is
val state by component.state.subscribeAsState()
in there). But the point is that the re-composition should stop quickly somewhere inside
FeedPostItem
- any composables called inside
FeedPostItem
with fully stable and unchanged parameters should be skipped. It might be worth extracting something to separate functions with stable arguments.
☝️ 1
Though, the strong skipping mode is supposed to help!
d

Djuro

03/07/2024, 10:20 AM
@Vaibhav Jaiswal check out this article. You can skip to “Unstable lambdas” section. Issue you have is probably happening because of lambdas with unstable captures which is coming from your component. Everything is explained well in the article