fix: 优化内存峰值与 CPU 性能,降低 100-300MB 内存占用

- claude.ts: 流式字符串拼接从 O(n²) += 改为数组累积 join,消除 4 处热点
- Messages.tsx: 合并 3 组独立遍历为单次 pass(thinking/bash 查找、3-filter 链、divider/selectedIdx)
- HighlightedCode.tsx: ColorFile 实例添加模块级 LRU 缓存(50 条),避免重复创建
- screen.ts: StylePool 衍生缓存添加 1000 条上限淘汰,防止无界增长
- CompanionSprite.tsx: TICK_MS 从 500ms 提升至 1000ms,减少 setState 频率
- connection.ts: MCP stderr 缓冲从 64MB 降至 8MB
- stringUtils.ts: MAX_STRING_LENGTH 从 32MB 降至 2MB
- sessionStorage.ts: Transcript 写入队列添加 1000 条上限
- query.ts: spread 改 concat 减少一次数组拷贝
- PromptInputFooterLeftSide.tsx: 显示进程 pid 便于调试

Co-Authored-By: Claude Opus 4.7 <noreply@anthropic.com>
This commit is contained in:
claude-code-best
2026-05-02 00:45:03 +08:00
parent f484fc34c8
commit ef10ad2839
13 changed files with 397 additions and 81 deletions

View File

@@ -119,6 +119,44 @@ export class StylePool {
this.none = this.intern([])
}
private static readonly CACHE_MAX = 1000
/**
* Evict oldest entries from derivative caches when they exceed the limit.
* ids/styles are never evicted (id is an array index).
*/
private evictCacheIfNeeded(): void {
if (this.transitionCache.size > StylePool.CACHE_MAX) {
const keys = this.transitionCache.keys()
for (
let i = 0;
i < this.transitionCache.size - StylePool.CACHE_MAX;
i++
) {
const k = keys.next().value
if (k !== undefined) this.transitionCache.delete(k)
}
}
if (this.inverseCache.size > StylePool.CACHE_MAX) {
const keys = this.inverseCache.keys()
for (let i = 0; i < this.inverseCache.size - StylePool.CACHE_MAX; i++) {
const k = keys.next().value
if (k !== undefined) this.inverseCache.delete(k)
}
}
if (this.currentMatchCache.size > StylePool.CACHE_MAX) {
const keys = this.currentMatchCache.keys()
for (
let i = 0;
i < this.currentMatchCache.size - StylePool.CACHE_MAX;
i++
) {
const k = keys.next().value
if (k !== undefined) this.currentMatchCache.delete(k)
}
}
}
/**
* Intern a style and return its ID. Bit 0 of the ID encodes whether the
* style has a visible effect on space characters (background, inverse,
@@ -136,6 +174,7 @@ export class StylePool {
(rawId << 1) |
(styles.length > 0 && hasVisibleSpaceEffect(styles) ? 1 : 0)
this.ids.set(key, id)
this.evictCacheIfNeeded()
}
return id
}

View File

@@ -114,7 +114,7 @@ export async function withConnectionTimeout<T>(
*/
export function captureStderr(
transport: StdioClientTransport,
maxSize = 64 * 1024 * 1024,
maxSize = 8 * 1024 * 1024,
): {
getOutput: () => string
clearOutput: () => void