Skip to content

[PR] perf: root-cause fix for billing-registry-lookup-perf (Fixes #7276, #7278, #7284) - #7363

Closed
ranxianglei wants to merge 4 commits into
masterfrom
2026-11-20_rc-billing-registry-lookup-perf
Closed

ranxianglei wants to merge 4 commits into
masterfrom
2026-11-20_rc-billing-registry-lookup-perf

Conversation

@ranxianglei

Copy link
Copy Markdown
Owner

根因修复:billing-registry-lookup-perf(3 个 issue,一个 PR)

本 PR 是根因汇聚单 [Root Cause] billing-registry-lookup-perf 的唯一根因修复 PR:cherry-pick 三个
已有 PR 的全部 commit(保留原作者署名,-x 标注来源),一次闭环。

原 PR commit 作者 修什么
#7279 4ebcc229859 Afloat #7276:resolveBillingPrice 每次调用重建整张 override 归一化表 → 改为 setBillingOverrides 一次构建、之后复用
#7282 4521b10954c + a5b32a22777 ework-daemon #7278:costLowerIndex 单槽 identity 缓存被「每次扫两张表」互踢 → 改为 cap=2 的 LRU Map
#7285 cee5b4e0810 Afloat #7284:relay 后缀查找每次全表 endsWith 扫描 → 首次 relay fallback 时懒建 slash 边界后缀桶

三个 commit 同基于 04ecbb42;master 自那以后未触碰 src/billing.ts / src/registry.ts 及相关测试,
无冲突按序 cherry-pick(7279 → 7282 → 7285)。

机制(为什么是一个根因)

计费/registry 查找路径上的三处「每次查找重复建表 / 建索引 / 全表扫描」:

  1. override 表归一化放在查找侧、且每次新建表身份(src/billing.ts overrideCosts);
  2. 索引缓存是单槽,而计费路径每次扫两张表(overrides → registry),互踢、两张都重建(src/registry.ts costLowerIndex);
  3. relay 后缀匹配没有索引,两处 scan 点每次 O(rows) 全表扫(src/registry.ts)。

三者叠加:一次 overrides-miss 的 relay 模型查找 = 重建 override 表 + 重建 registry 成本索引 + 全表
后缀扫描(成本行 + 模型行各一遍)。表越大、配置模型越多,每次查找越贵。

验证

  • 定向测试 52/52 绿:tests/billing-prices.test.ts / tests/issue7278-registry-cost-index.test.ts /
    tests/registry-price-profile.test.ts / tests/registry.test.ts;
  • npm run typecheck + npm run build 绿;
  • 基准 scripts/bench-registry-suffix-index.ts(捆绑真实 roster:7126 成本行 / 428 模型):
    500 名 × 5000 次 CPU 65 ms(同机修复前 ~400 ms);未收录模型 100 名 × 5000 次 19 ms(修复前 ~251 ms);
    输出 SHA-256 与 Relay registry lookups scan every row for each model suffix #7284 审查记录一致(7aeaa317… / 93a07756…)——解析结果字节级不变;
  • 全量回归交 CI(推送头)。

与既有 PR 的关系

行为变更

无:无配置面 / 线协议 / 持久化 / kernel / 模型可见文本变更;解析出的价格、窗口、输出上限逐字节不变
(基准 digest 钉住)。

Fixes #7276
Fixes #7278
Fixes #7284

Afloat16 and others added 4 commits November 20, 2026 14:39
…ling path scans (#7278)

The billing price path scans two cost tables per lookup (the overrides
table, then the registry fallback). costLowerIndex kept only ONE cached
index, so an overrides-miss lookup evicted the overrides index for the
registry index and back again — rebuilding the (large) registry cost
index on every lookup, O(registry rows).

Make costLowerIndex a small LRU Map (cap 2: the two tables the path
scans), refreshing recency on hit and evicting the oldest on overflow.
The registry index is now built once and reused; bounded memory.

The model-registry index (regLowerIdx) stays a single slot — it has only
one table. Regression test counts index builds via a Proxy ownKeys trap
and pins the registry index to exactly one build across repeated
overrides-miss lookups (fails on the old single-slot cache).

(cherry picked from commit 4521b10)
The committed test exercised stable identities for both cost tables (the
post-#7276 shape). At the PR head the real path (overrideCosts, pre-#7276)
hands scanRegistryCostRow a fresh overrides identity per lookup; a FIFO
cache would evict the stable registry index every other lookup and rebuild
it, yet the stable-identity test cannot see that. Add a fresh-identity case
that pins the recency-refresh: passes on the LRU, fails (25x rebuilds) on a
FIFO variant.

(cherry picked from commit a5b32a2)
@ranxianglei ranxianglei changed the title perf: root-cause fix for billing-registry-lookup-perf (Fixes #7276, #7278, #7284) [PR] perf: root-cause fix for billing-registry-lookup-perf (Fixes #7276, #7278, #7284) Nov 20, 2026
@ranxianglei

Copy link
Copy Markdown
Owner Author

🤖 Powered by ework · qwen3.8-27b

[bot] 🏷 (ai: vllm-qwen/qwen3.8-27b)

审核结论:可合(CI 全量回归进行中,当前 0 失败)

独立复核了 PR #7363(镜像 #7366)的全部声明——本地实测,非转述 issue 结论:

验证记录

代码审查(独立读 diff)

  • 后缀桶化与旧 endsWith 扫描等价:两处 suffix 恒以 / 开头(/${name}),桶键 = 每个 / 起的子串,bySuffix.get(suffix) 与 lower.endsWith(suffix) 判定完全一致;桶内顺序 = entries 顺序,冲突告警的 seen/parts 顺序不变
  • LRU cap=2 正确:计费路径只流两张表(costCache + overrideCostRows),cap=2 让两张都常驻;registry 重载只触发一次新表建索引,不 thrash
  • overrideCostRows 无过期风险:setBillingOverrides 是 overridePrices 唯一写者,且每条 reload 路径(startup / web Apply / /__bili/config)都走它,同步重建
  • 回归测试有牙:issue7278-registry-cost-index.test.ts 用 Proxy ownKeys 陷阱数索引构建次数,第 3 个 case 专门钉死「LRU 而非 FIFO」(fresh-override-identity 路径,FIFO 会 25× 重建 registry 索引)

一处 PR 正文小误(非代码问题):#7282 的 git author 实为 ci-check <ci-check@local>(原 commit 即此身份),正文写「ework-daemon」;cherry-pick 已忠实保留原署名(patch-id 一致),不影响。

结构化八问

  1. 是什么问题:计费/registry 查找路径上三处「每次查找重复建表 / 建索引 / 全表扫描」——表越大,每次查找越贵。
  2. 什么原因导致的:① override 归一化表放在查找侧、且每次新建表身份(Billing price lookups rebuild every override row and lowercase index #7276);② 索引缓存单槽,而计费路径每次扫两张表(overrides → registry),互踢、两张都重建([Drive-by] Registry cost-row index rebuilt per lookup on overrides-miss (single-slot cache thrash) — separate from #7276 #7278);③ relay 后缀匹配没有索引,两处 scan 点每次 O(rows) 全表扫(Relay registry lookups scan every row for each model suffix #7284)。
  3. 影响范围与严重程度:Billing price lookups rebuild every override row and lowercase index #7276/[Drive-by] Registry cost-row index rebuilt per lookup on overrides-miss (single-slot cache thrash) — separate from #7276 #7278 波及所有计费/registry 成本查找(每次 resolveBillingPrice);Relay registry lookups scan every row for each model suffix #7284 额外波及 relay(非已知 provider) 模型的后缀查找。特定条件下性能问题(表越大、配置模型越多越明显),非可用性、非缓存泄露 → p2 合理。
  4. 怎么修复的:① override 表在 setBillingOverrides 一次构建、之后复用;② 索引缓存改 cap=2 的 LRU Map(两张表都常驻,新表逐出最久未用);③ 后缀查找懒建 slash 边界桶(首次 relay fallback 建,之后 O(1) 命中)。
  5. 是否有回归问题:无。解析出的价格/窗口/输出上限逐字节不变(digest 钉住,独立复跑两侧一致);冲突告警文本与顺序不变;无 fail-fast 变吞错、无阈值/时序/输出格式漂移。
  6. 是否破坏缓存:不涉及 LLM 前缀缓存 / 重试 / 增量游标 / 去重键。内部索引缓存的失效策略仍按表身份(_resetForTest/_setForTest swap 自动失效),未变;LRU 只影响「哪张表的索引常驻」,不影响正确性。
  7. 是否改了设计方向:无。仍是「查找侧建索引 + 身份缓存」的原架构,只把单槽换 LRU、把全表扫换懒建桶。
  8. 是否引入了新的配置:无。无新 env / config / 默认值。

判定:可合(定向测试 + typecheck + build 本地绿,解析结果字节级不变,代码审查无回归)。全量回归以 CI 为准,当前进行中(0 失败);若 CI 红我再定向排查。

合并后 #7279 / #7282 / #7285 / #7280 变冗余,请 owner 关闭(agent 不关 PR)。

@ranxianglei

Copy link
Copy Markdown
Owner Author

已收编进 batch-A PR #7448(已合并,2026-11-21)。本 PR 的 billing override 复用、cost-index LRU 两个 commit 已 cherry-pick 进 #7448 并保留原作者署名;suffix-index 那个 commit 此前已经 #7285 单独合入 master,无需重复。后续以 #7448 为准,本 PR 关闭。

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

Projects

None yet

2 participants