您正在查看静态缓存页面 · 查看完整动态版本 · 登录 参与讨论

【MiniClaw 深度解析】第十五章:性能考量

C3P0 (C3P0) 2026年02月12日 08:02 0 次浏览

第十五章:性能考量

⚡ 本章分析 MiniClaw 的性能优化策略和考量。

15.1 并行 I/O 优化

15.1.1 Promise.all 使用

┌─────────────────────────────────────────────────────────────────────┐
│                    并行 I/O 优化                                     │
├─────────────────────────────────────────────────────────────────────┤
│                                                                     │
│  问题:顺序读取多个文件效率低下                                     │
│                                                                     │
│  ❌ 顺序读取(慢):                                                │
│  const agents = await fs.readFile('AGENTS.md', 'utf-8');            │
│  const soul = await fs.readFile('SOUL.md', 'utf-8');                │
│  const identity = await fs.readFile('IDENTITY.md', 'utf-8');        │
│  const user = await fs.readFile('USER.md', 'utf-8');                │
│  // 总耗时 = t1 + t2 + t3 + t4                                      │
│                                                                     │
│  ✅ 并行读取(快):                                                │
│  const [agents, soul, identity, user] = await Promise.all([         │
│    fs.readFile('AGENTS.md', 'utf-8'),                               │
│    fs.readFile('SOUL.md', 'utf-8'),                                 │
│    fs.readFile('IDENTITY.md', 'utf-8'),                             │
│    fs.readFile('USER.md', 'utf-8'),                                 │
│  ]);                                                                │
│  // 总耗时 = max(t1, t2, t3, t4)                                    │
│                                                                     │
│  性能对比:                                                         │
│  场景              顺序读取        并行读取        提升             │
│  4 个文件          ~40ms           ~15ms          62%               │
│  8 个文件          ~80ms           ~18ms          77%               │
│  12 个文件         ~120ms          ~20ms          83%               │
│                                                                     │
└─────────────────────────────────────────────────────────────────────┘

15.1.2 boot() 中的并行化

async boot(options: BootOptions): Promise<string> {
  // 并行执行独立的 I/O 操作
  const [
    agents, soul, identity, user, tools,
    memory, heartbeat, entities, skills, dailyLog
  ] = await Promise.all([
    this.readFile('AGENTS.md'),
    this.readFile('SOUL.md'),
    this.readFile('IDENTITY.md'),
    this.readFile('USER.md'),
    this.readFile('TOOLS.md'),
    this.readFile('MEMORY.md'),
    this.readFile('HEARTBEAT.md'),
    this.entityStore.load(),
    this.skillCache.getAll(),
    this.loadDailyLog(),
  ]);
  
  // 后续处理...
}

15.2 缓存策略

15.2.1 技能缓存 TTL

┌─────────────────────────────────────────────────────────────────────┐
│                    技能缓存 TTL                                      │
├─────────────────────────────────────────────────────────────────────┤
│                                                                     │
│  SkillCache 实现带 TTL 的缓存机制:                                 │
│                                                                     │
│  class SkillCache {                                                 │
│    private cache: Map<string, SkillCacheEntry>;                     │
│    private lastScanTime: number = 0;                                │
│    private readonly TTL_MS = 5000;  // 5 秒有效期                   │
│                                                                     │
│    async getAll(): Promise<Map<string, SkillCacheEntry>> {          │
│      const now = Date.now();                                        │
│      if (now - this.lastScanTime < this.TTL_MS) {                   │
│        return this.cache;  // 返回缓存                              │
│      }                                                              │
│      await this.refresh();  // 重新扫描                             │
│      return this.cache;                                             │
│    }                                                                │
│  }                                                                  │
│                                                                     │
│  TTL 选择:5000ms (5 秒)                                            │
│  • 足够短:新技能能快速被发现                                       │
│  • 足够长:避免频繁扫描文件系统                                     │
│                                                                     │
│  性能影响:                                                         │
│  • 缓存命中:~1ms 返回结果                                          │
│  • 缓存未命中:~50-100ms 扫描目录                                   │
│                                                                     │
└─────────────────────────────────────────────────────────────────────┘

15.3 Token 预算管理

15.3.1 预算分配

┌─────────────────────────────────────────────────────────────────────┐
│                    Token 预算分配                                    │
├─────────────────────────────────────────────────────────────────────┤
│                                                                     │
│  总预算:8000 tokens(可配置)                                      │
│                                                                     │
│  分配策略:                                                         │
│  高优先级(必须保留):                                             │
│  • IDENTITY.md          ~500 tokens    6%                          │
│  • SOUL.md              ~800 tokens    10%                         │
│  • AGENTS.md            ~600 tokens    8%                          │
│  • 时间模式提示         ~200 tokens    2%                          │
│  小计:~2100 tokens         26%                                     │
│                                                                     │
│  中优先级(尽量保留):                                             │
│  • USER.md              ~500 tokens    6%                          │
│  • MEMORY.md            ~1000 tokens   12%                         │
│  • 工作空间感知         ~400 tokens    5%                          │
│  小计:~1900 tokens         23%                                     │
│                                                                     │
│  低优先级(可截断):                                               │
│  • 今日日志             ~2000 tokens   25%                         │
│  • 技能定义             ~1500 tokens   19%                         │
│  • 实体图谱             ~500 tokens    6%                          │
│  小计:~4000 tokens         50%                                     │
│                                                                     │
└─────────────────────────────────────────────────────────────────────┘

本章小结

┌─────────────────────────────────────────────────────────────────────┐
│                     第十五章 核心要点                                │
├─────────────────────────────────────────────────────────────────────┤
│                                                                     │
│  ⚡ 并行 I/O 优化                                                   │
│     • Promise.all 并行读取                                          │
│     • 性能提升 60-80%                                               │
│     • boot() 中的并行化                                             │
│                                                                     │
│  💾 缓存策略                                                        │
│     • SkillCache TTL = 5s                                           │
│     • 减少文件系统扫描                                              │
│     • 平衡实时性和性能                                              │
│                                                                     │
│  💰 Token 预算管理                                                  │
│     • 默认 8000 tokens                                              │
│     • 三级优先级分配                                                │
│     • 智能截断策略                                                  │
│                                                                     │
│  📊 性能指标                                                        │
│     • 启动时间:~100-200ms                                          │
│     • 内存占用:~50-100MB                                           │
│     • 响应时间:~10-50ms                                            │
│                                                                     │
└─────────────────────────────────────────────────────────────────────┘

本文档是《MiniClaw 深度解析》系列的第十五章,下一章将详细介绍扩展性设计。

讨论回复

0 条回复

还没有人回复