跳转至

Performance

重要性:⭐⭐⭐ 目标读者:性能优化 / 大型项目用户 关联analysis/performance-history.mdstartup-optimization.md


1. 性能概览

┌────────────────────────────────────┐
│  启动时间      ~500ms              │
│  TTFT         ~1-1.5s             │
│  内存基础     ~50-100MB            │
│  内存大 session ~200-500MB        │
│  API 成本     ~$0.10-1/turn       │
└────────────────────────────────────┘

5 维


2. 5 阶段启动时间线

T+0ms     Bun 启动
T+50ms    模块求值开始
T+100ms   startMdmRawRead 触发(并行)
T+100ms   startKeychainPrefetch 触发(并行)
T+200ms   顶部副作用完成
T+250ms   imports 完成
T+300ms   main() 开始
T+300ms   preAction: await MDM/keychain
T+350ms   preAction: init()
T+400ms   preAction: initSinks
T+400ms   preAction: runMigrations
T+400ms   preAction: loadRemoteManagedSettings (fire)
T+400ms   run() 开始
T+500ms   第一个 render
T+500ms   startDeferredPrefetches 触发(12 个 fire)

总冷启动: ~500ms

~500ms


3. 5 大启动优化

3.1 顶部并行预取(~200ms)

// main.tsx 顶部
profileCheckpoint('main_tsx_entry')
startMdmRawRead()       // 与 import 并行
startKeychainPrefetch() // 与 import 并行

节省 200ms

3.2 Print 模式 fast-path(~50-100ms)

const isPrintMode = process.argv.includes('-p')
if (isPrintMode && !isCcUrl) {
  await program.parseAsync(process.argv)
  return program  // 不注册 20+ subcommand
}

节省 50-100ms

3.3 feature-gated DCE(~50ms)

const x = feature('X') ? require('./X.js') : null

商业版砍掉

3.4 首屏后预取

void initUser()
void getUserContext()
// 12 个 fire-and-forget

不阻塞

3.5 eager settings

eagerLoadSettings()  // run() 之前

早期加载


4. 5 大缓存层

缓存 TTL 用途
memoize 永远 session 内不变
memoizeWithLRU LRU 动态对象
memoizeWithTTLAsync TTL 凭据 / 定期更新
sessionStorage 永久 session log
content hash 永久 同样内容同样路径

5 类


5. 8 大 React 优化

优化 用途
Hooks-first 业务在 hooks
useMemo 重计算缓存
useCallback 事件引用稳定
useDeferredValue 打字不卡
useSyncExternalStore 外部 store
精确 selector 单字段订阅
React.memo 防止子组件重渲染
env-var 提到 mount-time PageUp 优化

8 种


6. 30+ profileCheckpoint

// main.tsx
profileCheckpoint('main_tsx_entry')
profileCheckpoint('main_function_start')
profileCheckpoint('main_warning_handler_initialized')
profileCheckpoint('main_client_type_determined')
profileCheckpoint('main_before_run')
profileCheckpoint('main_after_run')

// preAction
profileCheckpoint('preAction_start')
// 7 个

// run
profileCheckpoint('run_commander_initialized')
profileCheckpoint('run_before_parse')
profileCheckpoint('run_after_parse')

30+ 埋点


7. 性能数字

7.1 启动时间

模式 时间
完整冷启动 ~500ms
--bare ~300ms
-p (print) ~400ms
CLAUDE_CODE_EXIT_AFTER_FIRST_RENDER=1 ~300ms
缓存命中 ~200ms

5 模式

7.2 启动内存

状态 占用
刚启动 ~50-100MB
+ plugin +5-20MB/plugin
+ MCP server +5-10MB/server
大 session ~200-500MB

4 状态

7.3 解析速度

速度
bash parse (普通) < 1ms
bash parse (大) ~5-20ms
bash parse (病态) 50ms 超时

3 档

7.4 Yoga 布局

节点数 时间
1-10 < 1ms
10-100 ~1-10ms
100-1000 ~10-100ms

3 档

7.5 LLM 成本

模型 Input Output
Sonnet $3/M $15/M
Opus $15/M $75/M
Haiku $0.8/M $4/M

3 模型


8. 6 种启动模式

# 1. 完整(默认)
claude

# 2. Print 模式(fast-path)
claude -p "..."

# 3. Bare 模式(跳过预取)
claude --bare

# 4. Perf 模式(仅测时间)
CLAUDE_CODE_EXIT_AFTER_FIRST_RENDER=1 claude

# 5. Debug 模式(详细输出)
claude --debug

# 6. IDE 模式(桥接)
claude --remote-control

6 模式


9. 5 个性能监控工具

9.1 profileReport

import { profileReport } from './utils/startupProfiler.js'
profileReport()  // 输出总报告

启动报告

9.2 getYogaCounters

import { getYogaCounters } from './yoga-layout'
const { layouts, cacheHits } = getYogaCounters()

Yoga 监控

9.3 useFpsMetrics

useFpsMetrics()  // UI FPS

FPS 监控

9.4 Event Loop Stall Detector

// ANT-ONLY
startEventLoopStallDetector()

主线程监控

9.5 Statsig 上报

profileReport()  // 远程采样

远程


10. 5 个优化建议

10.1 拆分大文件

# main.tsx 4683 行 → 拆 subcommand
# REPL.tsx 5005 行 → 拆 sub-screen

10.2 减少 hooks 数

# 50+ hooks → 合并

合并

10.3 减少 utils 数

# 198 个 → 合并相似

合并

10.4 增量布局

# Yoga 增量

增量

10.5 Worker 卸载

# 主线程卸载到 Worker

Worker


11. 5 个 prompt cache 优化

11.1 Static 前缀

const systemPrompt = `You are Claude Code...`  // 静态

稳定

11.2 Content hash

generateTempFilePath(..., { contentHash: ... })  // 内容哈希

稳定

11.3 Delta 模式

getAgentListingDeltaAttachment(...)  // 只发变化

delta

11.4 顺序稳定

[system, tools, history, user]  // 顺序

顺序

11.5 user input 末尾

[..., userInput]  // 末尾

末尾


12. 4 个性能数字

数字 含义
30+ profileCheckpoint 埋点数
25+ bash validator 数
5 类 缓存策略
60+ Yoga exports

4 数字


13. 关键 trade-off

启动快  ↔  功能多
体积小  ↔  兼容性广
冷启动  ↔  预热
内存少  ↔  缓存多

4 维 trade-off

项目选功能多 + 兼容性广 + 预热(用户可用 --bare 加速)。


14. 监控 dashboard

// 推测
// Statsig dashboard 显示:
// - 启动时间分布
// - TTFT 分布
// - 错误率
// - feature flag 使用率

远程


15. 总结

Claude Code 性能 = 多维优化 + 用户可控

核心: - ~500ms 冷启动 - 30+ 埋点 - 5 类缓存 - 8 大 React 优化 - 5 个启动优化 - 用户可控 6 模式

下一步: - 看 performance-history.md - 调 startup-optimization.md - 监控 dashboard