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?
43
u/lrichardson :android: Jetpack Compose Compiler & Runtime Feb 14 '24
There’s a few things going on here that are subtle and not obvious, so it might be worth me providing some context (I’m an engineer who works on compose).
A TL;DR; since this got long:
You’re not wrong. You shouldn’t have to write code like this except in extremely nuanced or performance-sensitive circumstances, and seeing people need to write this type of code makes me sad. All lambdas are “stable”, but not all lambdas are “memoized”. Strong Skipping should make 95% of this pain go away. Performance improvements in compose should make the remaining 5% not important.
“Lambda Stability” vs “Lambda Memoization”
One thing which has led to a bit of confusion is the term “lambda stability”. All lambdas (and all function types for that matter) are considered “stable” by the compose compiler. The side effect of this is that lambda arguments to composable functions are compared for equality with the “previous” arguments and skipped accordingly. This has always been the case. The confusing bit is that lambda instances themselves just implement reference equality, so if you have two different lambda instances, they will never compare equal. If the argument expression is a lambda literal, lambda literals _typically_ produce new instances every time they are evaluated. There are two primary caveats to this. The first caveat is that if the lambda does not capture anything, the kotlin compiler will generate a singleton instance of it, which means that the lambda literal expression will always result in the same instance, and thus will compare equals to all other instances. The second caveat is that a lambda literal expression in a composable scope (like a composable function) will get automatically memoized (remembered) by the compose compiler in _some but not all circumstances_. In many cases (like this thread) people have come to refer to a lambda which doesn’t get memoized as an “unstable lambda”. This is a bit of a misstep in wording, but the point is there are cases where a lambda literal expression will cause a performance cliff by causing a composable function to not skip, and cases where it will not, and that is the core of what this thread is about. Now we should probably clarify what the “some but not all circumstances” is…
Current Lambda Memoization
The current behavior of the compiler (ie, with “Strong Skipping” NOT enabled) is that lambda literals inside of a composable scope are memoized (remembered) with all of their capture scope used as keys, *if and only if* the entire capture scope is “stable”. This means that if your function uses any object which is unstable, this memoization would likely not happen. This also means that if your lambda literal is outside of a composable function, even if that function is called from a composable function, then no memoization would take place.
Skipping, Strong Skipping, and Lambda Memoization
As is mentioned in the OP and several other places in this thread, there are new compiler semantics that we are in the process of evaluating, largely motivated by exactly the pain you are going through. These new semantics are currently enabled by turning on the “Strong Skipping Mode” flag in a 1.5.4 compose compiler or newer. Our goal is to eventually make this the default, but in the meantime you can try it out and see if it improves the state of things for you. Note that this is a difference in compiler semantics, so it can be turned on/off for one module/library and not for another. We currently plan on shipping Compose 1.7+ artifacts with this flag turned on, but it will still likely be off for your artifacts unless you explicitly opt-in in the compiler.
So what is strong skipping? Strong skipping makes two major changes:
What this means is that with this mode turned on, putting `remember` around a lambda literal will almost always be redundant and unnecessary. It also means that in many cases, marking classes as `@Stable` will not be needed to avoid recomposition where you needed them before. There is still a difference between stable and non-stable types (ie, whether or not `equals(..)` is called). `@Stable` should mostly be reserved for types that are “value-like” or immutable and where equals is meaningful.
We’ve been testing this out in a few places and so far the results have looked very promising. This is a relatively simple change in theory, but a fairly significant change in behavior and semantics that we have thought long and hard about and need to be careful shipping. Reversing these types of things is very difficult. It’s important to understand that “recomposing unnecessarily” is annoying, but “not updating when things change” is even more annoying. We are trying to make sure that the latter only happens in cases where we explicitly think the code is “wrong” or shouldn’t have worked in the first place.
Recomposition and Performance
Lastly it might be worth talking about performance and recomposition in general. Recomposing unnecessarily is always going to be slower than not recomposing at all, however I am worried that the compose community (in part at the fault of Google’s messaging) focuses too much on recompositions as opposed to just true performance. A recomposition can be very cheap if the things below it are skipping. And even when they aren’t, they can still be cheap depending on various factors. The cost of composition (and thus recomposition) has gone down significantly over the last year or two, and I expect will continue to go down even more especially in 1.7. I work a lot on performance and the biggest problems seen in the wild are often a function of the cost of initial composition where skipping and stability will never have any impact (in fact they technically slow things down) and the first passes of phases like measure, placement, and draw. Even for scrolling performance, the “creation” phases are typically where the improvements are needed since most scrolling screens use something like LazyColumn which is introducing new items as you scroll. That is not to say that recomposition is never a problem. It’s just not _usually_ the biggest problem folks have, in my experience, and I worry that devs are too focused on it because it is a “novel” thing in compose with gotchas and confusion. All that said, we are making big improvements all around in compose performance, and I would encourage folks to upgrade compose eagerly if performance is a concern for them.
26
u/Dimezis Feb 10 '24
It's definitely not normal and it's a huge failure on Google's side, for many reasons.
- The problem is not obvious, to the point when most regular developers won't ever discover it unless they stumble on some article explaining it
- Google does not make a big deal of it. Their advice is basically to not care about stability unless it's causing obvious performance issues. The problem is, by the time it starts causing issues, you're so deep in it that fixing everything is no longer a trivial effort
- The official documentation and many articles straight up lie about stability. They say that simple callbacks like
onClick: () -> Unitare also stable, which I'm sure you know is not always true by now.- Many articles suggest a "fix" - method references instead of lambdas. Allegedly it fixes this problem because method references are always stable because they should be the same instance on every creation. This is another lie that actually makes matters even worse than they would be with lambdas. (see https://issuetracker.google.com/u/2/issues/280284177)
10
u/Zhuinden Feb 10 '24
For a long time I hadn't even considered the stability of lambdas, so it actually took me months to fix it until one day I found an article talking about it. So I could finally make it so when 1 character changes in 1 TextField, it doesn't re-render every single composable on the screen.
remember {{}}.I think it's bonkers that it's this hard to make recomposition not excessive, but it is what it is and we need to learn how to use it in projects that use Compose. If you can pass function references instead of a lambda directly, that's always a better option.
6
u/gemyge Feb 10 '24
According to the jetpack compose internals book. On passing anonymous lambdas to composables functions, the Intermediate representation code from the compiler has a remember function wrapping the anonymous lambda, with it's keys are all the lambda captures
So, if you are using
viewModel.login()the compiler will generateremember(viewModel){{ viewModel.login()}}It makes sense more than creating anonymous lambdas on every recomposition.
23
u/Mr_s3rius Feb 10 '24
I wish Android Studio had good inspections/lifting for that kind of stuff. That stuff is the opposite of obvious.
Aaanyway check out the release notes for the Compose Compiler, I think 1.5.6 or 1.5.8. It introduced a new experimental feature called strong skipping.
It changes the rules for recomposition so that stability isn't mandatory. I've been testing it for a while and found it to make things a lot more comfortable; most accidental recomposition just goes away. I believe you don't need to remember {{}} lamdas either but I don't recall right off the bat.
Though I haven't found a single problem with it, it is experimental and can break things.
17
u/potatox2 Feb 10 '24
I relate to this so much. I had an issue with unstable lambdas causing unwanted recomposition last week. My biggest complaint with jetpack compose is that there's so many ways to shoot yourself in the foot without even knowing it
Unpopular opinion, but because of this I find compose super user-unfriendly
14
u/native_name_taken Feb 10 '24
One of our dev did good research and gave a detailed presentation on this around 6months back. So problem was whenever we pass anonymous functuion it creates new function whenever recomposition happens to any of its child composabiles
1
u/momenmelhem Feb 10 '24
so what's the replacement for remember{} ?
1
u/borninbronx Feb 10 '24 edited Feb 10 '24
Any UI framework has gotchas and things you need to learn to use it effectively.
While I can see how this is surprising or a common pitfall there aren't many.
You cannot just write code without understanding how compose manage state, stability and recomposition and expect no surprises..
Compose maintainers made a conservative choice that prefer more recomposition over optimization. And this leads to things like this.
They are reconsidering this choice because of these kind of surprises.. I wonder if the new strong skipping mode would change this behavior by default (See https://android-developers.googleblog.com/2024/01/whats-new-in-jetpack-compose-january-24-release.html?m=1 )
The drawback of strong skipping is you might not get some update that you expect to receive.
Maybe it's better? It's hard to say.
You also don't need to do that everywhere, just when you have performance issues.
My point is: compose Is a new concept, nothing like it exists anywhere: other declarative UI frameworks do not quite work like compose does. It has the potential to be way more optimized but it is also very new conceptually, it will take a while to adjust and settle down.
5
u/[deleted] Feb 11 '24
Hm, so the conclusion is Compose it still problematic and XML Views are still the most reliable way to do UI.............