Skip to content

[PR] fix: build bounded-boot file index in O(N) instead of O(N^2) - #6688

Open
ranxianglei wants to merge 1 commit into
masterfrom
wt-6662-ework-daemon
Open

ranxianglei wants to merge 1 commit into
masterfrom
wt-6662-ework-daemon

Conversation

@ranxianglei

Copy link
Copy Markdown
Owner

What

SessionStore.bootBounded() built each root's file index with
filesByRoot.set(root, [...(filesByRoot.get(root) ?? []), file]), re-copying every
previously-accepted path reference for each new accepted file — O(N^2) copies per
root. The work runs even when maxSessions=0 disables the decode window (#5219),
so that cap could not remove it. At 20k files/root the two roots copied ~400M
references before the proxy could listen.

Fix

Allocate each root's file list once and append accepted paths in traversal order:

const rootFiles: string[] = [];
for (const file of files) {
    if (path.basename(file).endsWith(".content-store.json")) continue;
    this.diskFiles.push(file);
    rootFiles.push(file);
}
filesByRoot.set(root, rootFiles);

The complete disk index, hot/cold separation, content-store filtering, candidate
ordering, warm limits, and the background scan are all unchanged — only the index
build's allocation pattern changes. filesByRoot is read later (hot path +
legacy loop) in traversal order, which is preserved.

Verification

  • New regression gate tests/issue6662-bounded-boot-file-index.test.ts: times
    boot({ maxSessions: 0 }) over 20k files/root (40k total) and asserts the
    linear cost. Measured on this machine: 181ms (fixed) vs 2913ms (O(N^2))
    — the gate's 1000ms bound separates the two with wide margin.
  • tsc --noEmit clean.
  • Reporter's benchmark (Node 24.22.1/Linux, scripts/bench-bounded-boot-index.mjs):
    base median 3595ms vs fixed 253.5ms for 20k/root.

Closes #6662

@ranxianglei ranxianglei changed the title fix: build bounded-boot file index in O(N) instead of O(N^2) [PR] fix: build bounded-boot file index in O(N) instead of O(N^2) Nov 14, 2026
@github-actions

github-actions Bot commented Nov 14, 2026 •

Copy link
Copy Markdown
Contributor

📦 Built Package Artifact

Branch: wt-6662-ework-daemon (7c3ba91)

Latest build — install this exact version

npm install -g billion-context@0.0.10120-pr.6688.e68d7d4

This exact version is immutable and reproducible. The rolling pr tag tracks the newest test build across all PRs.

Recent builds (commit → version)

commit version build
7c3ba91 billion-context@0.0.10120-pr.6688.e68d7d4 build
a7c048a billion-context@0.0.9945-pr.6688.2305b30 build

Download artifact

  1. Download the artifact from the Actions run
  2. Extract the tarball and install:
tar xzf billion-context-pr6688.tgz
npm install -g package

Install instructions are refreshed on each push.

@ranxianglei

Copy link
Copy Markdown
Owner Author

🤖 Powered by ework · qwen3.8-27b

🔶 需要决策

(ai: vllm-qwen/qwen3.8-27b)

背景一句话: 本 PR 与更早开出的 #6663(外部贡献者 Afloat16,2026-11-14 06:59 UTC)是同一 issue #6662 的两个 PR,修复完全相同(bootBounded 的 O(N²)→O(N) 索引构建,src 改动字节等价),违反「一条线程一个修复 PR」;按先来后到应关后到的 #6688、保留 #6663,但 agent 无权关闭 PR,且我无外部贡献者 fork 分支的推送权限,无法把 #6688 的回归测试移植进 #6663。

查重: 重复 —— #6688 与 #6663 重复(同一目标 issue #6662、同一机制、同一修法)。#6663 先到(06:59 UTC),#6688 后到(09:32 UTC);#6688 开 PR 前的查重(见 #6662 线程 09:45 评论)漏检了当时已 open 的 #6663,属流程违规,在此如实记录。

待决问题: 保留哪个 PR?

我的推荐: 选项 A。不决定的默认行为: 两个 PR 都挂着、都不合并,#6662 保持未修——现状是最坏结果。

Review 结论(八问):

  1. 是什么问题: bootBounded() 构建每 root 的文件索引时,每收一个新文件就把已建好的整个列表重新拷贝一遍(O(N²) 引用拷贝),大会话库(20k 文件/root)下 listen 前空转约 3.5 秒。
  2. 什么原因导致的: filesByRoot.set(root, [...(filesByRoot.get(root) ?? []), file]) —— 每次 set 都重新分配并拷贝整个旧数组;拷贝的产生者就是这个追加写法本身,不是表面现象。
  3. 影响范围与严重程度: 默认启动路径(sessionWarmMax 默认 0 → maxSessions=0;索引构建与 decode 窗口是否启用无关,Service crashed, sessions exploded instantly. | 服务崩溃,会话瞬间爆炸 #5219 已封 decode 侧,这是索引侧)。p2: 能用但启动慢;20k 文件/root ≈ 3.5s pre-listen 停顿(与 Service crashed, sessions exploded instantly. | 服务崩溃,会话瞬间爆炸 #5219 健康预算同类风险,量级小得多)。
  4. 怎么修复的: 每个 root 的列表一次性分配、按遍历顺序原地追加(O(N));只改分配模式,不动任何逻辑。
  5. 是否有回归问题: 无。逐项清点触碰路径的既有行为: 完整磁盘索引(diskFiles)、content-store 过滤、hot/cold 分离、遍历顺序(唯一两个读点 persist.ts:1049 hot 窗口与 :1095 legacy 循环,均 ?? [] 且保序)、warm 预算、后台扫描——全部未变。新代码对空 root 也会无条件 set(root, []),与旧代码「无 key」行为等价(两个读点都走 ?? [])。相邻门禁 Service crashed, sessions exploded instantly. | 服务崩溃,会话瞬间爆炸 #5219(decode cap 三条)与 Web UI first paint stalls 78-180s+: /__bili/overview cold scan blocks the event loop, and the freeze itself feeds the respawn churn #3199(T1-T3)本地全过。
  6. 是否破坏缓存: 不涉及——无缓存/重试/游标/去重键变更。
  7. 是否改了设计方向: 不涉及——仅内部分配模式。
  8. 是否引入了新的配置: 不涉及。

验证证据:

标签: 已给 #6688 补 perf + p2 + scope/storage(按本 PR 自身 diff 判定;开 PR 时未打标签属交付不完整,已补正)。

结论: 代码本身可合(§7.4 自动合并门禁 1-8 全满足,待 CI 绿);唯一阻塞是两 PR 一线程的处置,请按选项 A/B 拍板。

@ranxianglei

Copy link
Copy Markdown
Owner Author

🤖 Powered by ework · qwen3.8-27b

(ai: vllm-qwen/qwen3.8-27b)

CI 状态更新: head a7c048a 的最新 CI 运行(18:54 触发)在 runner 队列中已排约 45 分钟(0 failed,尚无 job 开跑)——队列被并发 PR 占满,非本 PR 问题;e2e 车道已 skipped(文件清单未触发)。本地证据维持: typecheck 干净、回归门禁 red/green 双向验证、相邻定向测试全绿。最终判定以 CI 出队列后的结果为准。两 PR 处置(选项 A/B,见上一条)仍待拍板。

@ranxianglei

Copy link
Copy Markdown
Owner Author

🤖 Powered by ework · qwen3.8-27b

(ai: vllm-qwen/qwen3.8-27b)

本轮收工小结(前面两条已含完整八问与决策请求,此处只给最终状态):

已完成:

  1. PR [PR] fix: build bounded-boot file index in O(N) instead of O(N^2) #6688 代码审核完毕——修复正确(bootBounded 索引构建 O(N²)→O(N),仅改分配模式),逐项回归清点无破坏;回归门禁 red/green 双向本地验证(pre-fix master 3426ms 失败 / PR head 216–241ms 通过,1000ms 上限);typecheck 干净;相邻门禁(Service crashed, sessions exploded instantly. | 服务崩溃,会话瞬间爆炸 #5219 decode-cap、Web UI first paint stalls 78-180s+: /__bili/overview cold scan blocks the event loop, and the freeze itself feeds the respawn churn #3199 T1-T3、bounded-storage-scale)全绿。
  2. 查重结论:[PR] fix: build bounded-boot file index in O(N) instead of O(N^2) #6688 与更早的 [PR] perf(storage): build bounded boot file indexes incrementally #6663(外部贡献者,06:59 UTC 开)重复,修法字节等价;按先来后到推荐保留 [PR] perf(storage): build bounded boot file indexes incrementally #6663(选项 A),已在 [PR] perf(storage): build bounded boot file indexes incrementally #6663 下留言请贡献者补入 [PR] fix: build bounded-boot file index in O(N) instead of O(N^2) #6688 的回归测试文件。
  3. [PR] fix: build bounded-boot file index in O(N) instead of O(N^2) #6688 标签已按自身 diff 补齐: perf + p2 + scope/storage。

剩余与风险:

bootBounded() re-copied every previously-accepted path reference for each
new file when building the per-root index, giving O(N^2) copies per root
that ran even with maxSessions=0 (decode window disabled, #5219). At 20k
files/root the two roots copied ~400M references before listen.

Allocate each root's file list once and append accepted paths in traversal
order; the complete disk index, hot/cold separation, content-store
filtering, candidate ordering, and background scan are all unchanged.

Closes #6662
@ranxianglei

Copy link
Copy Markdown
Owner Author

🤖 Powered by ework · qwen3.8-27b

(ai: vllm-qwen/qwen3.8-27b)

本轮收工:PR #6688 与 master 的冲突已解决并推送。rebase 后净变更只剩回归门禁测试——因为 O(N) 修复本身已经通过 #6663 合入 master 了。

七问:

  1. 这次做了什么:把 PR 分支 wt-6662-ework-daemon rebase 到当前 master(1ee510c5),解决 src/persist.ts 的唯一冲突,--force-with-lease 推送。新 head = 7c3ba91b22。

  2. 为什么会冲突:master 在这期间已经合并了 [PR] perf(storage): build bounded boot file indexes incrementally #6663(外部贡献者 Afloat16,commit 94ec3dda2e,经 batch6-wave6 76640c7c95 进 master)——它修的是同一个 issue [Performance] Bounded boot repeatedly copies the growing history-file index before listen #6662、同一处 bootBounded() 索引构建,但落点形态与本 PR 不同([PR] perf(storage): build bounded boot file indexes incrementally #6663 把 filesByRoot.set(root, rootFiles) 放在 for 循环前,本 PR 放在循环后)。两边改了同一行区域、改法不同 → 文本冲突。

  3. 怎么解决的:冲突区取 master 版本(循环前 set、循环后无 set),使 src/persist.ts 与 master 逐字节一致、diff 归空。两种形态功能等价(同一数组引用、map 只 set 一次、读点都在循环后),取 master 版是因为修复已在 master,本 PR 不应再重复引入同一改动。

  4. PR 现在的实际净变更:对 master 只剩 1 个文件 —— tests/issue6662-bounded-boot-file-index.test.ts(58 行回归门禁)。src/persist.ts 的修复改动已归零(在 master 上)。scripts/bench-bounded-boot-index.mjs 也已由 [PR] perf(storage): build bounded boot file indexes incrementally #6663 带上 master,非本 PR 贡献。

  5. 如何验证的:① node --import tsx --test tests/issue6662-bounded-boot-file-index.test.ts 本地通过(40k 文件 boot 远低于 1000ms 上限);② tsc --noEmit 干净;③ git merge-tree --write-tree 确认与 master 可干净合并(exit 0,无冲突)。

  6. 是否有回归问题:无。本 PR 对 master 的净变更是新增一个测试文件,不改任何生产代码;src/persist.ts 与 master 逐字节一致。既有行为零触碰。

  7. 剩余 / 需要你注意的:

This branch has not been deployed

No deployments
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Projects

None yet

Development

Successfully merging this pull request may close these issues.

[Performance] Bounded boot repeatedly copies the growing history-file index before listen

1 participant