返回提示词目录第 1345 页

分析并优化应用冷启动性能

根据启动追踪拆解耗时阶段,给出按收益排序的代码优化方案,并建立真机基准与回归监控。

完整提示词

<context>
The user's {{platform}} app is slow to open. Cold start (process not in memory) is the case that matters most and the one developers rarely see on their fast test phones. Rough reference points: Android vitals treat a cold start of 5 seconds or more as excessive, and users notice anything above about 2 seconds; Apple advises that the first frame should appear within about 400 ms of launch. Measure on a low-end device, release build, cold, several runs.

Startup is usually slow because of work that does not need to happen before the first useful screen: initialising every SDK (analytics, crash reporting, ads, feature flags, A/B testing) synchronously, dependency injection graphs built eagerly, disk and database reads or migrations on the main thread, network calls awaited before rendering, heavy first layouts, large JavaScript bundles or Dart isolate setup, and a splash screen used to hide all of it.
</context>

<task>
<startup_trace>
{{startup_trace}}
</startup_trace>

1. Record the baseline: cold, warm and hot start times if available, device model, OS version, build type, number of runs and spread. If they are missing or from a debug build or emulator, give the measurement method first: `adb shell am start -W` and Macrobenchmark `StartupTimingMetric` on Android, Instruments App Launch template and `XCTApplicationLaunchMetric` on iOS, `flutter run --trace-startup --profile` for Flutter, and native markers plus Hermes profiling for React Native.
2. Lay out the timeline in phases: process start and runtime init (pre-main on iOS, Application.onCreate and content providers on Android, JS bundle load or Dart init), first activity or scene creation, first frame, and first meaningful content (time to full display). Assign the measured time to each phase.
3. For each item of work before first frame, classify it: required for the first screen, can be deferred until after first frame, can be lazy (on first use), can run on a background thread, or can be removed.
4. Propose fixes ranked by milliseconds saved on the low-end device:
   - Defer and lazily initialise SDKs; on Android check content-provider auto-init and use the App Startup library to control order; on iOS reduce dynamic frameworks and work in `+load` or static initialisers.
   - Move disk, database, preference and keychain reads off the main thread, or make them lazy.
   - Render the first screen from cached or placeholder data; never await the network before first frame.
   - Simplify the first layout; avoid inflating hidden screens.
   - Android: Baseline Profiles and, where relevant, R8 optimisation. iOS: fewer dynamic libraries, avoid heavy work in `application(_:didFinishLaunchingWithOptions:)`. React Native: Hermes, inline requires or lazy modules, smaller bundle. Flutter: defer plugin initialisation, deferred components, avoid heavy work before `runApp`.
   - Use the platform splash screen API only to cover real minimal work, not to hide slowness.
5. Show the code changes for the top fixes as diffs or before-and-after snippets.
6. Give the guard: a startup benchmark in CI or on a device farm on a fixed low-end device, with a regression threshold, and production monitoring of start times (Android vitals, MetricKit, or the team's performance monitoring).
</task>

<constraints>
- Never use debug builds or emulators for the numbers you report; say when the user's numbers are from one.
- Savings are estimates until measured on the device; give ranges.
- Deferring an SDK must keep its function: say what is lost (for example crash reports during the first second) and whether that is acceptable.
- Do not invent SDK APIs; mark uncertain ones [CHECK DOCS].
{{> guardrails/scope-discipline}}
</constraints>

<output_format>
## Baseline
Table: start type | device | build | median ms | runs. Or the measurement method if missing.
## Startup timeline
Table: phase | ms | main work in it.
## Ranked fixes
Table: fix | phase | estimated ms saved | effort | trade-off.
## Changes
Code for the top fixes.
## Measure and guard
Benchmark setup, threshold and production metric.
</output_format>

可替换内容

  • {{platform}}
  • {{startup_trace}}
  • {{> guardrails/scope-discipline}}
  • [CHECK DOCS]

分类与标签

编程开发、冷启动优化、性能追踪、真机基准、性能回归

前往 AI 图片创作