跳到正文
Skillpedia
移动应用10k · #11921

jetpack-compose-performance

它能做什么

这个技能检查 Jetpack Compose 界面代码中的性能问题,例如过多的重绘和掉帧。适合应用在滚动或打开界面时感到卡顿的 Android 开发者。

适用场景

  • ✓当 Android Compose 界面在滚动时出现卡顿或掉帧的时候。
  • ✓当你希望减少 Compose 界面中不必要的重绘时。
  • ✓当你希望设置发布构建选项和基准配置文件以加快应用启动的时候。

技能链接

https://github.com/android/skills/blob/main/jetpack-compose/jetpack-compose-performance/SKILL.md

复制后粘贴给你的 AI(Claude、ChatGPT 或 Cursor),让它按照说明操作。

使用方法

最简单的方法:复制下面的消息,粘贴给你的 AI。它会读取这个技能并按说明操作。

可直接粘贴的消息

请用这个技能帮我:https://github.com/android/skills/blob/main/jetpack-compose/jetpack-compose-performance/SKILL.md 阅读该链接中的 SKILL.md 文件,并在我的任务中按照它的说明来做。
想让你的 AI 自己找到这样的技能吗?安装免费的 Skillpedia MCP:只需一行,每次任务前它都会在 15,000+ 个技能中搜索。1 分钟完成安装 →

可以这样问 AI

“我的 Compose 信息流在滚动时卡顿。请找出性能问题并修复。”

每周 5 个新技能,1 分钟读完。

工作原理

该技能首先检查构建是否使用发布设置,因为调试构建的测量结果较慢。然后它会根据检查清单扫描每个可组合项和列表,并在提出修复方案之前阅读参考说明。

技术内容(原始 SKILL.md)

作者在 GitHub 上的英文原文。

Foundational prerequisites

Before micro-optimizing composables, ensure standard production flags are in place:

  1. Release Build: Never profile or benchmark debuggable builds; ART runtime verification severely degrades performance.
  2. R8 Minification: Keep R8 enabled to optimize bytecode and coroutine dispatch.
  3. Baseline Profiles: Generate baseline profiles for critical user journeys (cold start, key scrolling feeds) so the ART VM pre-compiles and preloads classes ahead of time.

Optimization audit workflow

  1. Exhaustive Scan : Scan the target code against all 7 core categories in the following sections. Evaluate every @Composable function, modifier chain, and lazy-layout data model against all checklist rules rather than stopping after the first issue found.
  2. Mandatory Reference Read Before Flagging or Editing : Whenever a file matches a rule in Categories 1--7, you MUST open and read the corresponding references/*.md file for the approved fix pattern and full scope guards before reporting a finding or editing code. Never rely on the summary bullets alone to write fixes.

1. State, snapshots, and stability (references/state-and-stability.md)

MANDATORY : Before flagging or fixing any issue in this category, you MUST read references/state-and-stability.md.

  • Phase Deferral (Lambdas) : Wrap frequently changing state reads in lambdas (such as Modifier.offset or Modifier.graphicsLayer) so reads occur in Layout or Draw phase rather than Composition.
  • Prohibit Backwards Writes (Verify Read-Before-Write Order) : Never modify state in a later phase or after it has already been read in the same composition pass (Read -> Write, or Composition Read -> Layout or Draw Write). Prevent backwards writes across phases to avoid first-frame popping, jank, and infinite recomposition loops (Backwards Write). Do not flag a state assignment at the start of a composable (Write -> Read or Composition Write -> Draw Read) as a backwards write---a backwards write strictly requires a read of that state to precede the write.
  • Avoid Redundant State Wrapping in Composition (Write -> Read) : Do not wrap synchronous values in remember { mutableStateOf(...) } only to overwrite them immediately at the top of composition before reading them (for example, var path by remember { mutableStateOf(Path()) }; path = viewModel.getPath()). While not a backwards write, this adds unnecessary slot table and MutableState snapshot overhead (and risks becoming a backwards write if a read is later added preceding the assignment); use a plain local val or read the value directly inside the target phase (such as Modifier.drawWithCache).
  • Explicit remember Keys & Slot Overhead : Always pass all input variables and composable parameters referenced inside remember as calculation keys. Remember that remember stores keys + 1 objects in the slot table and checks key equality on every recomposition; only remember when allocations are heavy or identity must be preserved. Do not pass snapshot state values/properties (e.g. state.value) as keys when wrapping derivedStateOf (passing the State instance itself is fine if the instance reference can change).
  • computedStateOf vs derivedStateOf (Compose 1.13+) : On Compose 1.13+, use computedStateOf for simple condition checks and filtered state from fast-changing inputs (such as scrollState.value > 0), which provides significantly faster invalidation than derivedStateOf (on Compose < 1.13, use remember { derivedStateOf { ... } }). Reserve derivedStateOf for heavy calculations that benefit from multi-read caching, or trivial calculations read frequently enough per composition (roughly 10 / numberOfStatesReadPerCalculation times without invalidating between reads) to make up for the invalidation cost.
  • Synchronous State Derivation : Audit for asynchronous produceState + snapshotFlow blocks observing Compose states (such as scroll positions or collection sizes); replace them with synchronous remember { derivedStateOf { ... } } (or computedStateOf on Compose 1.13+).
  • Trivial Primitives : Prefer plain custom Kotlin getters (get()) over derivedStateOf or computedStateOf for simple boolean expressions and property lookups.
常见问题

我应该在调试构建中测量性能吗?

不应该。该技能说明调试构建的运行速度比实际使用慢,因此应在发布构建中测量。

什么是基准配置文件?

它是一个告诉 Android 提前准备哪些代码的文件,使启动和滚动等关键流程运行得更快。

它会重写整个界面吗?

它会根据检查清单寻找具体问题,并为发现的每个问题提出有针对性的修复建议。

详情

作者的原始描述

Diagnose and optimize Android Jetpack Compose UI code. Use this skill when analyzing performance bottlenecks of existing code to minimize recomposition overhead and prevent dropped frames and UI lag.

repo: android/skills

热度

GitHub 星标
10k
排名
#11921
许可证
Apache-2.0
更新时间
2026/10/29

这个技能有用吗?

0

登录 后即可投票。

评论

  • …

同一仓库的更多技能

相似技能