Skip to content

性能:「需要我」只重读有新消息的会话 - #60

Merged
NginxL merged 2 commits into
mainfrom
feat/perf-needs-incremental
Dec 3, 2026
Merged

NginxL merged 2 commits into
mainfrom
feat/perf-needs-incremental

Conversation

@NginxL

@NginxL NginxL commented Dec 3, 2026

Copy link
Copy Markdown
Owner

做了什么

「需要我」(今天页、今天卡片、飞书页共用)改成按会话增量重算:

  • Store 记每个会话的版本号:新消息、编辑、撤回、屏蔽标记、补历史、会话行变化、会话消失或回来,都推进受影响会话的版本;说不清影响哪些会话的写(换账号清库、迁移)整体作废。不动消息和会话行的写(已处理标记、机器人识别、免打扰、头像、记账)不推进。
  • 按会话缓存从消息算出的部分(最后一段没回的话、@我、发言人、更早的未回 @我),键是(会话、版本、条数、全局代次),最多 600 个会话,最久没用的先丢;重算时只对版本变了的会话批量查询,读的过程中又有新提交的不写回缓存。

为什么

0.11.76 上线后第一小时的扩展内渲染统计:其他页面 p95 都在 1–5 ms,只有首页的今天卡片 p50 53 ms / p95 79 ms。原因是首页每 2 分钟刷一次卡片,期间总有新消息写入,「需要我」每次都要重读约 8,000 行消息(在线上库的副本上测:每次 40–55 ms,其中 85% 在取行和转字典)。

效果

同样规模的合成数据(387 个会话、16 万条消息),写入一个会话后的重算:中位数 106.77 ms → 1.45 ms,最慢 1.84 ms;随机写入后增量结果和全量重算完全一致。线上对比见上线后的评论。

怎么验证的

bash scripts/test.sh 和 PYTHON=/usr/bin/python3 bash scripts/test.sh 全部通过;新测试在改动前的代码上失败。

🤖 Generated with Claude Code

NginxL and others added 2 commits December 4, 2026 05:29
- Store keeps a per-chat version (new, edited, recalled, blocked and history messages; chat rows; gone and back)
  and a generation for writes it cannot place (an account wipe, a migration); writes that touch no message or chat
  row say so (chat_ids=()).
- ChatsToday caches each chat's message-derived part of needs() by (chat, version, limit, generation), at most 600
  chats (least recently used out), rereads the changed ones in one batch, and never stores a read a newer commit
  overtook. Recompute after one chat's write: 106.77 ms -> 1.45 ms median on 387 chats / 160k messages.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
- 核心:每个会话记版本号,「需要我」按会话缓存,写入后只重读版本变了的会话(今天卡片在首页每次刷新几乎都要重算,现在从约 50 ms 降到 2 ms 以内);结果和全量重算一致。

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
@NginxL
NginxL merged commit e8e527f into main Dec 3, 2026
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