Files
script/ai
chaos b6fc113cdb perf: 多维度性能优化 (审计24项, 实施7项)
优化内容:
1. [HIGH] _maybe_trim_context 快速跳过: body<100KB 直接返回, 省去 json.loads+token估算
   - 99% 的小请求从 ~0.01ms 降到 0ms (零开销)
2. [HIGH] token估算用预编译正则替代逐字符扫描
   - 190K字符: 9.2ms → 6.0ms (35% faster)
3. [HIGH] 移除调试dump代码: >200KB请求同步写文件是热路径阻塞I/O
4. [HIGH] 提取公共函数: _dynamic_timeout() + _build_upstream_headers() + _upstream_request_with_retry()
   - proxy()和anthropic_messages()的401/429/504重试+超时+header逻辑去重 (-131行)
5. [MEDIUM] cache.get() 无锁快速路径: 先无锁检查token+expiry, 仅过期时加锁
6. [MEDIUM] SSE buffer用list+join替代字符串拼接: 5x faster, 避免O(n²)风险
7. [MEDIUM] 连接池扩大到96 (threads*1.5): 避免retry并发耗尽

基准测试: _maybe_trim_context(大请求) 110ms→71ms (36% faster)
功能测试: 全部通过
2026-07-22 15:35:57 +08:00
..