Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Tip3 lambda usage #1

Closed
badoualy opened this issue Jun 3, 2022 · 2 comments · Fixed by #2
Closed

Tip3 lambda usage #1

badoualy opened this issue Jun 3, 2022 · 2 comments · Fixed by #2
Assignees

Comments

@badoualy
Copy link

badoualy commented Jun 3, 2022

Hi,

In the tip3, I think there's a potential catch that can trigger unncessary recompositions.
The declaration of the lambda at this line:

https://github.com/AidaIssayeva/recomposition_examples/blob/main/app/src/main/java/com/cupsofcode/recomposition_examples/Tip3.kt#L49

When Tip3 is recomposed, even if the CardInfo value doesn't change, a new lambda will be created and passed to GoodGreetingsTip3, which will cause a complete recomposition down to GoodInnerColumn.

This article is explaining why it happens and some solutions: https://proandroiddev.com/understanding-re-composition-in-jetpack-compose-with-a-case-study-9e7d96d98095

This isn't exactly the topic of the tip, but since this is a very common case, I think it would be worth to fix it.

The easy fix: GoodGreetingsTip3(cardInfo::value)

Edit: might also be worth to add a comment to explain this

@AidaIssayeva
Copy link
Owner

hi @badoualy 👋 ! I totally dropped the bomb on looking after this repo all summer.

Thank you for bringing it up this issue 🙏 ! This is a very good point! Updating the tip

@AidaIssayeva AidaIssayeva self-assigned this Sep 9, 2022
@AidaIssayeva AidaIssayeva linked a pull request Sep 9, 2022 that will close this issue
@badoualy
Copy link
Author

badoualy commented Sep 9, 2022

Hi,

actually after doing some further reading, it looks like in this case, using a lambda was ok, because the only captured value is a stable type, the compiler should actually mark the lambda as stable, and will be able to skip.

I think it was explained in this article: https://multithreaded.stitchfix.com/blog/2022/08/05/jetpack-compose-recomposition/

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging a pull request may close this issue.

2 participants