Compose performance
Skill thetruong1099/android-mvi-base-code/.claude/skills/compose-performance
npx -y skills add thetruong1099/android-mvi-base-code --skill compose-performanceAssembled from the repository path, not quoted from the project. Check it against their README if it does not work.
2 things to look at
- no licenseNo license file was found in the repository. Code published without one is not open source by default, so using it at work is a question for whoever answers licensing questions where you are.
- 0 stars0 stars. Stars are a popularity signal and not a quality one, but at this level it is likely that nobody has read this closely except its author, and you would be relying on your own review.
What its author says it does
Copied from the file, not written here
Jetpack Compose performance optimization patterns for this project. Covers recomposition rules, type stability with @Immutable/@Stable, lambda stability for callbacks, deferred state reads, PagingData with stable item keys, animation performance, and debugging recomposition with Layout Inspector.
SKILL.md
3.9 KB, as published. Nobody here has run it
Compose Performance
Recomposition Rules
- Composable functions can recompose at any time — no side effects in body
- Recomposition skips unchanged parameters — make parameters stable
- Use
rememberfor expensive computations - Use
derivedStateOffor derived state
Stability
What makes a type stable?
- Primitives (
Int,String,Boolean) @Immutableor@Stableannotated classesdata classwith all stable properties
// Domain models passed to Composable should be stable
@Immutable
data class ItemUiModel(val id: String, val name: String, val avatarUrl: String)
// ViewState: data class is stable if all fields are stable
// Note: Flow fields cause instability but are acceptable for PagingData
data class SampleViewState(
val items: Flow<PagingData<ItemUiModel>>? = null, // Flow is NOT stable, but OK here
) : IViewState
Lambda Stability
// GOOD: Lambda hoisted to stateful Screen - created once, passed down
SampleScreenInternal(
onItemClick = { item -> viewModel.onTriggerEvent(SampleViewEvent.OnItemClick(item)) }
)
// BAD: Lambda created inline causes recomposition
ItemCard(
onItemClick = { viewModel.onTriggerEvent(SampleViewEvent.OnItemClick(it)) }
// New lambda instance every recomposition!
)
Defer State Reads
// BAD: Reading state in composition phase
Box(modifier = Modifier.offset(y = scrollState.value.dp))
// GOOD: Defer read to layout phase with lambda
Box(modifier = Modifier.offset { IntOffset(0, scrollState.value) })
remember & derivedStateOf
val sortedItems = remember(items) { items.sortedBy { it.name } }
val hasItems by remember { derivedStateOf { items.isNotEmpty() } }
PagingData + Compose (Correct Pattern)
@Composable
fun SampleScreenInternal(viewModel: IBaseViewModel<...>) {
val refreshState = rememberPagingRefreshState<ItemModel>()
BaseScreenComponent(
viewModel = viewModel,
externalIsRefreshing = refreshState.isRefreshing,
onRefreshListener = { refreshState.refresh() },
) { state, padding ->
val items = state.items?.collectAsLazyPagingItems() // lifecycle-aware
refreshState.bind(items)
PagingVerticalGridComponent(
items = items,
columns = GridCells.Fixed(2),
) { pagingItems ->
items(
count = pagingItems.itemCount,
key = pagingItems.itemKey { it.id }, // Stable keys - required!
) { index ->
pagingItems[index]?.let { ItemCard(it) }
}
}
}
}
Animation Performance
// Use Compose animation APIs
val alpha by animateFloatAsState(
targetValue = if (isVisible) 1f else 0f,
animationSpec = tween(300),
label = "alpha",
)
// Transition API for complex animations
val transition = updateTransition(targetState = isExpanded, label = "expand")
val height by transition.animateDp(label = "height") { expanded ->
if (expanded) 200.dp else 56.dp
}
Debugging Recomposition
Use Android Studio Layout Inspector with "Show Recomposition Counts" enabled.
// Debug-only: recomposition highlighter
@Composable
fun Modifier.recompositionHighlighter(): Modifier {
val count = remember { mutableIntStateOf(0) }
count.intValue++
return this.drawWithContent {
drawContent()
drawRect(
color = Color.Red.copy(alpha = (count.intValue * 0.1f).coerceAtMost(1f)),
size = Size(4.dp.toPx(), size.height),
)
}
}