Skip to content

[perf] storage: cache a long project's whole commit history - #42

Merged
princepride merged 1 commit into
mainfrom
perf/commit-record-cache
Nov 2, 2026
Merged

princepride merged 1 commit into
mainfrom
perf/commit-record-cache

Conversation

@princepride

@princepride princepride commented Nov 2, 2026 •

Copy link
Copy Markdown
Collaborator

问题与改动

每次读取工程都要校验完整的提交链,而每条提交记录都存着一份完整的工程快照。原来的记录缓存最多只保留 128 条或 8 MiB,历史一长就装不下,每次读取都得从磁盘重新解析全部记录。

实际影响(本机,工程目录位于网络存储):一个有 317 条提交、记录共 55 MB 的工程,每次读取快照需要 3–13 秒。读取期间一直持有目录锁,导致:

  • 会话心跳超时,编辑会话过期,提示「工程已关闭或会话已过期」;
  • 正在运行的生成任务在提交、轮询、下载或保存阶段因锁超时被标记为中断,提示「提交结果不明」或「生成或本地保存失败」。

改动:缓存上限提高到 4096 条或 512 MiB,写成具名常量并注明原因。复用缓存前仍按文件签名检查记录是否被修改、替换或删除,这条原有约束不变。

Test Plan

  • .venv/bin/python -m pytest tests -q
  • npm test
  • .venv/bin/python scripts/generate_contracts.py --check
  • 在上述 317 条提交的工程上,测量修改前后的快照读取耗时。

Test Result

  • 后端测试 614 项通过。新增的 test_long_history_is_validated_from_disk_only_once 构造 200 条提交的工程,要求第二次读取时不再解析任何提交文件;该测试在修改前失败、修改后通过。
  • 前端测试 423 项通过,契约校验通过。
  • 实测:同一工程的快照读取从 3–13 秒降到约 0.05 秒。修复后尚未重新跑一遍长时间的批量生成,会话和任务中断问题的消失是基于原因推断,未单独验证。

未覆盖:

  • 每条提交都保存完整快照,提交历史仍会线性增长;本 PR 只解决重复解析的问题。
  • 内存占用上限为每个进程 512 MiB,没有专门测量其他规模工程下的内存曲线。

🤖 Generated with Claude Code

## summary

- Every project read validates the full commit chain, and each record
  holds a full project snapshot. The record cache kept at most 128
  records or 8 MiB, so a longer history was reparsed from disk on
  every read. A 317-commit project (55 MB of records) took 3-13 s per
  snapshot on network storage.
- Those reads held the catalog lock for as long, so session heartbeats
  and job checkpoints timed out: edit sessions expired and running
  generations were interrupted at submit, poll, download or save.
- The cache now holds up to 4096 records or 512 MiB. The same project
  reads in about 0.05 s. Changed, replaced or missing records are still
  detected by their file signature before a cached record is reused.

## validation

- `.venv/bin/python -m pytest tests -q`: 614 passed. The new
  `test_long_history_is_validated_from_disk_only_once` fails without
  this change.
- `npm test`: 423 passed.

Co-Authored-By: Claude Opus 5.5 <[email protected]>
@princepride
princepride merged commit 2996479 into main Nov 2, 2026
@princepride
princepride deleted the perf/commit-record-cache branch November 2, 2026 14:32
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant