1. Leverage Stable and Immutable Types
Annotate your data models with
@Stableor@Immutable.Prefer using primitive types and immutable data classes in composable parameters.
Avoid passing mutable or non-observable types as parameters.
2. Keep Composables Small and Focused
Break down large composable functions into smaller, single-responsibility components.
This lets Compose (and the compiler) minimize the scope of recompositions.
3. Use remember and rememberSaveable Properly
Use
rememberto store objects or state that should persist across recompositions.Use
rememberSaveablefor state that must survive process death (with types supported by Bundle).Avoid initializing objects or expensive computations directly in composables—use
rememberto cache them.
4. Avoid Unstable Objects in Parameters
Don’t pass new objects, collections, or lambdas directly as parameters (e.g.,
List, lambda{}).If you must, use
rememberor wrap them in a stable class.
5. Use Keys in Dynamic Lists
In
LazyColumn,LazyRow, etc., always provide a uniquekeyfor items.This helps Compose match slot tables and avoid recomposing unchanged items.
6. Apply movableContentOf for Movable UI Blocks
Use
movableContentOfto move composables efficiently in the hierarchy without recomposition, such as in drag-and-drop, item reordering, or when you need to “teleport” content.
7. Minimize Lambda Allocations
Prefer stable, top-level, or
remember-ed lambdas over inline lambdas.If a lambda doesn’t need to change, move it out or wrap with
remember.
8. Utilize Derived State for Expensive Computations
Use
derivedStateOfto memoize values that are calculated from other state, so they only recalculate when dependencies change.
9. Avoid Passing State Directly to Deep Child Composables
Lift state up and only pass the minimal, necessary data to child composables.
Pass callbacks for actions, not the whole state object.
10. Use Compose Compiler Metrics and Reports
Enable Compose compiler metrics (
metricsDestination,reportsDestination) to analyze where recompositions happen, stability info, and parameter tracking.Regularly review these reports to identify recomposition hotspots.
11. Stay Updated with Compose Compiler Versions
Always use the latest stable Compose Compiler and runtime libraries for the best optimizations and bug fixes.
12. Mark Functions as Inline Where Appropriate
Use
inlinekeyword for small, frequently used functions—this can help reduce composable overhead in some cases.
13. Avoid Recomposition Triggers in Parent When Possible
Use
rememberUpdatedStatewhen passing changing state to long-lived side-effect composables (likeLaunchedEffect), so parent recomposition doesn’t restart effects.
14. Be Careful With State Hoisting
Hoist only the necessary state up to avoid re-triggering recompositions at higher levels than necessary.
15. Review Unstable Collections
If you must use mutable collections, wrap them with stable interfaces or use observable state (e.g.,
SnapshotStateList,SnapshotStateMap).
✨ Summary Table
| Tip | What it Optimizes |
|---|---|
| Annotate models as Stable/Immutable | Parameter stability |
Use remember, derivedStateOf | Avoid redundant work |
| Use keys in Lazy lists | Efficient item reuse |
| Keep composables small | Minimize recomposition |
| Use compiler metrics | Spot and fix bottlenecks |
| Avoid direct lambdas/objects in params | Parameter stability |
| Move lambdas/state up/out | Minimize allocations |
| Use latest Compose libraries | Leverage best compiler |
Comments
Post a Comment