BitFun icon indicating copy to clipboard operation
BitFun copied to clipboard

[perf] 桌面端启动与大历史会话端到端性能

Open limityan opened this issue 6 months ago • 5 comments

目标

降低桌面端冷启动到可交互、默认大历史 session 首次打开/切换、以及首次可读后的滚动和窗口变化等关键交互耗时;同时避免 loading 回闪、裸白屏、滚动跳动和输入框异常锚定。性能结论以用户场景端到端数据为准,局部时间片下降不能单独作为收益结论。

结论

阶段性目标已达成。最新 main 已包含 #1356,并在 Windows desktop release-fast、隔离 E2E workspace、同机样本下完成最终复测:冷启动与大历史 session 关键场景相比 issue 创建期均有明确端到端下降;大历史 session 首开、快速切换、首次滚动、窗口宽度变化未复现 blank/回闪。

关键结果快照

用户场景 issue 创建期基线 最新 main 结果 变化
冷启动到聊天主界面可操作 1538.9 ms 589.7 ms(425.9-652.8,n=20) -949.2 ms(-61.7%)
首次打开默认大历史 session,到最新正文可读 965.1 ms 164.1 ms -801.0 ms(-83.0%)
首次打开默认大历史 session,到完整历史补齐 1118.1 ms 274.2 ms -843.9 ms(-75.5%)
再次打开默认大历史 session,到最新正文可读 457.7 ms 171.9 ms -285.8 ms(-62.5%)
大历史 session 零延迟快速切换,到目标最新正文可读 无同口径基线 245.6 ms,blank=0 新增 guard
首次可读后立刻滚动 无同口径基线 133.0 ms,blank=0 新增 guard
首次可读后调整窗口宽度 无同口径基线 148.6 ms,blank=0 新增 guard

关键分片:Noto Sans SC 随包字体体积从 31,660,680 bytes 降到 12,703,276 bytes(-59.9%);NotoSansSC-Regular WOFF2 最新 main 启动资源耗时 p50 为 44.7 ms(n=20),相对同日 TTF main p50 240.3 ms 明显下降。

核心方案

  • 大历史 session 优先保障最新正文首屏可读,完整历史在不打断滚动、输入、切换和 resize 的前提下补齐。
  • 启动期非关键系统与后台任务避开首屏关键窗口,防止和首次可交互、首次打开 session 竞争。
  • 随包 Noto Sans SC 从完整 TTF 切换为完整 WOFF2,只改变字体资源格式,不做子集化、不删字重、不改字体族、fallback 或字符覆盖。
  • 通过冷启动稳定性脚本与大历史 session 交互矩阵看护首开、快速切换、滚动、resize、blank/回闪等关键场景。

风险与边界

  • WebView2、系统调度和磁盘状态仍可能带来启动 outlier;本 issue 以阶段性端到端收益闭环,不把所有 native/WebView 抖动问题混在同一个综合 issue 内继续扩张。
  • 字体优化保持完整字符覆盖和 400/500/600 字重语义;不以牺牲中文显示质量换性能。
  • 大历史 session 优化不改变用户可见行为:打开后锚底、快速切换、输入、滚动、窗口变化和关闭语义都需要保持稳定。

limityan avatar May 29 '26 10:05 limityan

Follow-up PR #953 addresses the historical-session open issues found during the startup investigation:

  • Caps completed historical model-round first render to 80 groups, then appends 80 groups every 16ms.
  • Fixes TodoWrite duplicate keys from restored legacy/truncated ids.
  • Replaces internal session-view truncation markers with user-facing preview text.
  • Fixes the usage-card Tooltip ref warning.

Diagnostic data used for this PR:

  • Problem logs showed 156 and 263 duplicate-key warnings keyed by [truncated for session view], with the stack rooted in TodoWriteDisplay.
  • For a 263-group class of case, the first render is bounded to 80 groups instead of all 263 groups, a 69.6% reduction in initial group render volume.
  • Local debug logs after the duplicate-key/truncation-path fixes showed duplicate-key warnings and legacy truncation marker logs reduced to 0.

This does not close the issue. The remaining follow-up is still direct optimization of interactive_shell_ready and related startup readiness costs.

limityan avatar May 29 '26 11:05 limityan

Update from #966:

This PR adds the release-fast startup/session performance harness and records the latest post-rebase sample for the follow-up optimization work. The issue should remain open after #966 because it does not claim an end-to-end startup reduction; it makes the current slices measurable and fixes the visible startup splash background mismatch.

Latest release-fast sample:

Scenario Metric Result
Startup interactive_shell_ready 1943.5 ms
Startup main_window_shown 1484.6 ms
Startup non_critical_init_done 1886.8 ms
Long session first open initially loaded turns 3 / 80
Long session first open latest-turn first frame 48.9 ms
Long session background hydrate full hydrate 56.5 ms

Startup API hot spots in the same sample:

Command Count Total Max
get_config 10 1438.7 ms 357.5 ms
initialize_ai 1 335.9 ms 335.9 ms
git_is_repository 4 73.3 ms 28.7 ms
remote_get_workspace_info 1 69.5 ms 69.5 ms
git_get_repository_basic 3 51.8 ms 17.8 ms

Follow-up candidates to keep tracking here:

  • Collapse or cache duplicate startup get_config reads, then validate whether interactive_shell_ready and non_critical_init_done move down.
  • Defer initialize_ai until the first agent/model action that actually needs it, or split cheap config validation from expensive provider/client setup.
  • Investigate native WebView create/show/page-load slices separately from frontend app readiness. The latest run still spent about 838 ms in main-window create/show before frontend milestones.
  • Keep long-session first-frame below the 100 ms target while separately optimizing cold background full hydrate for very large raw tool-result payloads.

limityan avatar May 30 '26 08:05 limityan

Update from PR #971 (perf(startup): defer editor and AI initialization):

  • Deferred Monaco loader/CSS/worker setup to first editor initialization.
  • Lazy-loaded editor panel implementations from FlexiblePanel.
  • Moved non-critical tool/context-menu initialization to bitfun:interactive-shell-ready with a 10s fallback.
  • Removed root App AI auto-initialization and narrowed startup-visible barrel imports.

Measured with pnpm --dir src/web-ui run build:

Scenario Entry JS Monaco runtime markers Root AI hook marker
gcwing/main@978e079f 8,298,942 bytes present present
PR #971 (de70290e) 4,294,378 bytes removed removed

Entry JS reduction: 4,004,564 bytes / 48.25%.

Remaining follow-up: run repeated release-fast cold-start E2E after merge and continue reducing shared API/config barrels so unrelated API modules such as the initialize_ai command wrapper no longer appear in the first shell bundle.

limityan avatar May 30 '26 09:05 limityan

Update from PR #1015 (perf(startup): add tracing and reduce shell latency):

This PR adds P0 observability plus a focused P1/P2 startup reduction. It does not close this issue; it establishes finer-grained timing and reduces the current latest-main startup path.

Strict A/B method: Windows desktop release-fast, 5 cold launches each. Baseline is gcwing/main@2a016364; PR head is 85225a72 rebased on that baseline.

Scenario Metric Main p50 PR p50 Delta
Cold startup first_script_eval 632.4 ms 545.6 ms -86.8 ms (-13.7%)
Cold startup main_window_shown 740.0 ms 593.4 ms -146.6 ms (-19.8%)
Cold startup interactive_shell_ready 1138.7 ms 778.4 ms -360.3 ms (-31.6%)
Cold startup non_critical_init_done 1421.2 ms 1296.3 ms -124.9 ms (-8.8%)
Long session first open restoreDuration 53.8 ms 47.6 ms -6.2 ms (-11.5%)
Long session first open hydrateDuration 123.6 ms 109.8 ms -13.8 ms (-11.2%)
Long session first open latestFrameSinceHydrate 43.2 ms 42.5 ms -0.7 ms (-1.6%)
Long session background hydrate fullHydrateDuration 33.8 ms 36.0 ms +2.2 ms (+6.5%)

New PR-only diagnostic fields now available for future stages:

Diagnostic PR p50
webviewBuildDuration 500.0 ms
mainWindowShownToInteractive 183.7 ms
workspaceInit 73.6 ms
pre-interactive command count 12
pre-interactive command frontend total 524.1 ms
pre-interactive command backend body total 3.0 ms
pre-interactive estimated bridge/queue total 521.1 ms
pre-interactive get_config count 3
pre-interactive get_config frontend total 295.8 ms
long-session clickToLatestFrame 166.1 ms
long-session clickToFullHydrateFrame 360.8 ms

Main implementation points:

  • Native/frontend startup trace now covers WebView/window timing, frontend readiness, Tauri command body/queue estimates, workspace initialization, and long-session first-open timing.
  • AppLayout is lazy-loaded and the startup overlay waits for layout readiness before handoff to avoid blank/flicker risk.
  • AI experience/model config reads no longer happen at import/constructor time; non-critical config/update/announcement work moves behind shell readiness.
  • Workspace initialization overlaps independent identity/global-state work while preserving state publication order.

Known risks and follow-up:

  • The small fullHydrateDuration p50 regression is explicitly tracked but below a material UX threshold in this run.
  • The new trace shows bridge/queue wait dominates backend command body time; the next stages should target session metadata pressure, git/workspace status batching, config/keybinding bootstrap, and remaining frontend import graph conflicts.
  • app.keybindings and remote workspace info were intentionally not deferred here because they affect global shortcuts and remote correctness.

limityan avatar Jun 01 '26 10:06 limityan

Closed after final main verification.

Final checks on gcwing/main@64a94f8cf with Windows desktop release-fast:

  • Cold startup to chat UI interactive: p50 589.7 ms, range 425.9-652.8 ms, n=20; issue creation baseline was 1538.9 ms.
  • Long session first open to latest text: 164.1 ms, blank=0.
  • Rapid switch to latest text: 245.6 ms, blank=0.
  • First scroll after content visible: 133.0 ms, blank=0.
  • Resize after content visible: 148.6 ms, blank=0.
  • Noto Sans SC bundled font size: 31,660,680 bytes -> 12,703,276 bytes.
  • NotoSansSC-Regular startup resource p50: 240.3 ms TTF baseline -> 44.7 ms WOFF2 on latest main.

Remaining native/WebView startup jitter should be tracked separately if it becomes a product target; it is not blocking this consolidated startup + large history session issue from closing.

limityan avatar Jun 30 '26 06:06 limityan