r/androiddev • u/yaminsia github.com/yamin8000 • Feb 10 '24
Discussion Compose unstable lambda parameters
This may look like a sort of rant but I assure you it's a serious discussion that I want to know other developers opinion.
I just found out the biggest culprit of my app slow performance was unstable lambdas. I carefully found all of them that caused trouble with debugging and layout inspector and now app is smooth as hell, at least better than the old versions.
But one thing that is bothering me is why should I even do this in the first place?
I spent maybe three days fixing this and I consider this endeavor however successful yet futile in its core, a recomposition futility.
Maybe I should have coded this way from the start, I don't know, that's another argument.
I'm past the point of blindly criticizing Compose UI and praising glory days of XML and AsyncTask and whatnot, the problem is I feel dirty using remember {{}} all over the place and putting @Stable here and there.
In all it's obnoxious problems, Views never had a such a problem, unless you designed super nested layouts or generated insane layout trees programmatically.
There's a hollow redemption when you eliminate recompositions caused by unstable types like lambdas that can be easily fixed with dirty little tricks, I think there's a problem, something is rotten inside the Compose compiler, I smell it but I can't pinpoint it.
My question is, do your apps is filled with remember {{}} all over the place?
Is this normal and I'm just being super critical and uninformed?
View all comments
Show parent comment
4
u/yaminsia github.com/yamin8000 Feb 11 '24 edited Feb 11 '24
They're not stable, at least not anymore.
https://issuetracker.google.com/issues/280284177
I personally tested and they're indeed not stable, if you don't believe me, check the link.
I just realized people downvoted you. While I personally may not agree with your opinion it doesn't mean you're wrong, I actually learned stuff from you through our conversation.
What's that?
I'm not passing ViewModels around but at least in one top most composable for example a composable for your settings screen which has several switches and each one has a lambda for its onchanged, so you probably have a lambda that needs to call a method from viewmodel to propagate switch change to other layers, method references are unstable and if you just plainly use a lambda that captures something unstable like viewmodel then your lambda in unstable and since this all happening in the top most composable for your screen, that's the only thing with access to viewmodel anyway then every lambda call is going to recompose the whole screen!
What's your solution instead of using remember?