Skip to content

[FR]: Minimize Unnecessary Recompositions #1353

Description

@KDW03

Is there an existing issue for this?

  • I have searched the existing issues

Describe the problem

In current implementation of NiaApp, we've observed an issue where unrelated state changes, such as modifications to showSettingsDialog, trigger unnecessary recompositions across the entire app, including within NiaNavHost and its child screens

Upon investigation, we identified that lambda expressions, such as onTopicClick, are being recreated with each recomposition cycle. Compose treats these newly instantiated lambda expressions as different from their predecessors, thus triggering recompositions even when there are no pertinent state changes.

image

this behavior will persist until the Jetpack Compose Compiler is updated to version 1.7, which is expected to introduce an enhanced 'Strong skipping' mode for more efficient recomposition skipping. In the meantime, developers are advised to manage lambda instances manually using remember to mitigate unnecessary recompositions.

This is particularly evident in screens like ForYouScreen, where the onTopicClick lambda is passed down and utilized. Despite showSettingsDialog having no direct relation to these screens, they undergo recomposition, leading to performance inefficiencies.

Describe the solution

I propose to mitigate this issue by employing the remember construct to cache navigation-related lambda expressions within NiaNavHost. This approach will ensure that lambdas are only recreated when genuinely necessary, such as when the NavController instance changes

change

image

before

image

after

image

Additional context

No response

Code of Conduct

  • I agree to follow this project's Code of Conduct

Metadata

Metadata

Assignees

Labels

enhancementNew feature or request

Type

No type

Projects

No projects

Milestone

No milestone

Relationships

None yet

Development

No branches or pull requests

Issue actions