Skip to content

perf(core): cache static registry model lookups and eliminate snapshot traversal allocations - #4732

Closed
chilung-cgu wants to merge 687 commits into
lidge-jun:devfrom
chilung-cgu:perf/core-alloc-and-cache
Closed

chilung-cgu wants to merge 687 commits into
lidge-jun:devfrom
chilung-cgu:perf/core-alloc-and-cache

Conversation

@chilung-cgu

@chilung-cgu chilung-cgu commented Sep 15, 2026 •

Copy link
Copy Markdown
Contributor

Summary

  • Caches static PROVIDER_REGISTRY model definitions in src/router.ts into static lookup maps (REGISTRY_BY_ID, REGISTRY_STATIC_MODEL_IDS, REGISTRY_ALIAS_BY_ID), replacing O(N) array scans (.find()) and repeated Object.keys() array allocations with O(1) lookups in knownModelIdsForProvider, routedProviderConfig, and routeModelInternal.
  • Optimizes writeBoundedSnapshot in src/responses/state.ts by replacing [...states].reverse() with reverse-indexed traversal over Array.from(states), and concatenating pre-serialized entries directly ({"version":2,"states":[" + serializedEntries.join(",") + "]}) instead of a second full-tree JSON.stringify, eliminating 24-48 MiB of transient heap allocations during snapshot schedules.
  • Preserves native byte-exactness in request logging metering (Buffer.byteLength(JSON.stringify(entry), "utf8")) based on benchmark verification.
  • Adds regression unit tests in tests/routing/router.test.ts and tests/responses/responses-state-write-amplification.test.ts ensuring static registry cache lookup integrity and snapshot serialization output formatting.

Verification

  • bun run typecheck (passed with 0 errors)
  • bun run privacy:scan (passed)
  • bun test tests/codex-integration/app-owned-memory.test.ts tests/routing/router.test.ts tests/lab/core-lab-boundary.test.ts tests/responses/responses-state-write-amplification.test.ts tests/codex-integration/slug-codec.test.ts (76 pass, 0 fail, 206 expect calls)

Checklist

  • Scope stays focused and avoids unrelated cleanup.
  • Docs or release notes were updated when needed.
  • Security-sensitive changes were reviewed for secrets, auth, and unsafe defaults.

Review readiness checklist

This PR stays in draft until every box below is ticked. Tick all four boxes once the requirements are met:

  • All CI tests are green on my local testing.

  • I pushed my PR to the latest dev commit.

  • I resolved all correct Codex and CodeRabbit findings.

  • My PR is ready for review.

Summary by CodeRabbit

  • Performance

    • Improved snapshot writing efficiency while preserving existing size limits and JSON compatibility.
    • Optimized provider and model lookups to support faster routing.
  • Reliability

    • Ensured generated snapshots remain valid JSON and retain all remembered entries.
    • Confirmed provider model listings include expected NVIDIA registry models.

Copilot AI lite review requested due to automatic review settings September 15, 2026 21:27

Copilot AI left a comment

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Copilot was unable to review this pull request because the user who requested the review has reached their quota limit.

@github-actions github-actions Bot added the intake: hygiene-blocked Deterministic PR hygiene checks failed label Sep 15, 2026
@github-actions

Copy link
Copy Markdown
Contributor

⚠️ Deterministic hygiene checks failed.

  • missing_regression_test — Behavior changed under src/ or gui/src/ without a test change. Add focused coverage or obtain test-exception-approved.

@github-actions github-actions Bot added the enhancement New feature or request label Sep 15, 2026
@coderabbitai

coderabbitai Bot commented Sep 15, 2026 •

Copy link
Copy Markdown
Contributor

Review Change StackReview Change Stack

Important

Draft PR not reviewed

Draft PRs are not automatically reviewed by default.

  • Trigger a manual review

To automatically review draft PRs, update your CodeRabbit configuration:

reviews:
  auto_review:
    drafts: true

No actionable comments were generated in the recent review. 🎉

ℹ️ Recent review info
⚙️ Run configuration

Configuration used: Repository: lidge-jun/opencodex/.coderabbit.yaml

Review profile: ASSERTIVE

Plan: Advanced

Run ID: 5555cb42-a3c2-4ae6-894b-ec6607bd1b89

📥 Commits

Reviewing files that changed from the base of the PR and between 8f5a334 and e70c08c.

📒 Files selected for processing (2)
  • src/responses/state.ts
  • src/router.ts

Included review availability: Your plan provides up to 10 included reviews per hour; 8 remain after this review.


📝 Walkthrough

Walkthrough

The change optimizes bounded snapshot serialization and provider registry access. Snapshot entries are serialized once during backward selection. Router paths use precomputed maps for provider, model-ID, and alias lookups. Tests validate snapshot parsing and static model-ID discovery.

Changes

Runtime efficiency improvements

Layer / File(s) Summary
Bounded snapshot serialization
src/responses/state.ts, tests/responses/responses-state-write-amplification.test.ts
Snapshot generation walks entries backward, serializes each selected entry once, measures UTF-8 size, applies the existing limits, and reverses the serialized strings before building the JSON payload. The test validates the version, JSON structure, and remembered entry IDs.
Provider registry lookup maps
src/router.ts, tests/routing/router.test.ts
The router precomputes provider, static model-ID, and alias maps. Model-ID discovery, provider configuration, and alias fallback use these maps instead of repeated registry scans. The test verifies NVIDIA static model IDs include moonshotai/kimi-k2.6.

Priority: ⬇️ Low

Estimated code review effort: 2 (Simple) | ~15 minutes

Change: Refactor

🚥 Pre-merge checks | ✅ 4 | ❌ 2

❌ Failed checks (1 warning, 1 inconclusive)

Check name Status Explanation Resolution
Docstring Coverage ⚠️ Warning Docstring coverage is 20.00% which is insufficient. The required threshold is 80.00%. Docstring coverage is scoped to functions touched by this diff. Analyzed 5 functions across 4 files. Write docstrings for the functions missing them to satisfy the coverage threshold.
Linked Issues check ❓ Inconclusive The context identifies PR #4732 but does not provide a linked issue or issue requirements. The linked-issue status cannot be verified from the supplied information. Provide the linked issue reference and confirm that the PR addresses its acceptance criteria, or confirm that no linked issue is required.
✅ Passed checks (4 passed)
Check name Status Explanation
Description Check ✅ Passed Check skipped - CodeRabbit’s high-level summary is enabled.
Description check ✅ Passed The PR summary clearly describes the registry lookup caches, snapshot serialization optimization, preserved behavior, and validation results. It matches the changes in src/router.ts, `src/responses/…
Out of Scope Changes check ✅ Passed The changes remain within the stated performance objective. They optimize provider registry lookups and response-state snapshot serialization, with focused tests and no unrelated public API changes.
Title check ✅ Passed The title accurately and concisely identifies both primary changes: caching static registry lookups and removing snapshot traversal allocations.
✨ Finishing Touches
🧪 Generate unit tests (beta)
  • Create a new PR

Thanks for using CodeRabbit! It's free for OSS, and your support helps us grow. If you like it, consider giving us a shout-out.

❤️ Share

Comment @coderabbitai help to get the list of available commands.

@github-actions

github-actions Bot commented Sep 15, 2026 •

Copy link
Copy Markdown
Contributor

⏳ DRAFT

  • author re-attestation is required for the current head.

What to do

  • Change the first managed item to Required local validation passed; commands, results, and any full-suite exception are documented., clear all four boxes and save. Wait for the bot to acknowledge the cleared checklist before validating and ticking the boxes again.
  • Only a new body edit by the PR author after this notice can advance the checkpoint. If edits share a checkpoint timestamp, make another body edit and save later.

Review readiness checklist

  • ⬜ Required local validation passed; commands, results, and any full-suite exception are documented.
  • ⬜ I pushed my PR to a recent dev commit (at most 10 behind; a maintainer may still ask for the exact tip before merge).
  • ⬜ I resolved all correct Codex and CodeRabbit findings.
  • ⬜ My PR is ready for review.

0/4 boxes ticked.

Current head: 1f32c93b9d373dc7e40ce9938eca119827ce02ad. Existing PR text and checkbox marks were preserved.

@github-actions
github-actions Bot marked this pull request as draft September 15, 2026 21:27
@lidge-jun

Copy link
Copy Markdown
Owner

리뷰 · 우선순위 70 / 80

이 PR은 chilung-cgu가 연 draft이고, 베이스는 지금 dev tip(45cfb04e9, package 2.57.0)에 맞춰져 있다. 바꾸는 파일은 딱 두 개다. src/router.ts와 src/responses/state.ts. 목적은 “기능 추가”가 아니라, 이미 자주 도는 두 길을 더 싸게 만드는 것이다.

첫째, 라우터 쪽. 지금 dev의 knownModelIdsForProvider / routedProviderConfig / routeModelInternal은 호출마다 PROVIDER_REGISTRY.find(...)로 배열을 훑고, 힌트 맵마다 Object.keys로 임시 배열을 만든다. 레지스트리는 모듈 로드 때 이미 고정된 목록이라, 매번 같은 선형 탐색을 반복하는 셈이다. 이 PR은 모듈 상단에 REGISTRY_BY_ID, REGISTRY_STATIC_MODEL_IDS, REGISTRY_ALIAS_BY_ID 세 맵을 미리 만들고, 핫 패스에서는 O(1) lookup만 한다. 슬러그 코덱이 기대하는 “알려진 native id 합집합” 의미는 그대로 두고, 계산만 앞당긴다.

둘째, 응답 스냅샷 쪽. writeBoundedSnapshot은 주기적으로 디스크에 상태를 쓴다. 지금 코드는 [...states].reverse()로 맵을 한 번 뒤집은 뒤, 각 엔트리를 JSON.stringify로 크기를 재고, 마지막에 다시 JSON.stringify({ version: 2, states: entries })로 전체 트리를 한 번 더 직렬화한다. 스냅샷 상한이 24 MiB라서, 큰 홈에서는 “크기 재기용 문자열 + 최종 payload 문자열”이 잠깐 동시에 살아 힙을 크게 쓴다. 이 PR은 (1) Array.from(states)를 인덱스 역순으로 걸어 뒤집기 할당을 줄이고, (2) 이미 만든 엔트리 JSON 문자열을 join,으로 이어 {"version":2,"states":[...]}를 손으로 조립한다. 바이트 길이·digest 비교 로직은 그대로라서, “이전에 쓴 것과 같으면 다시 안 쓴다”는 절약도 유지하려는 모양이다. 로컬에서 같은 엔트리 배열로 비교하면 손조립 payload와 JSON.stringify 결과가 바이트 단위로 같다.

지금 dev가 바쁘게 가는 축은 godfile facade 분할, send-budget/#4546, 2.56.0→2.57.0 릴리즈 레일이다. 이 PR은 그 축을 건드리지 않는 작은 perf 정리라서, 방향과 싸우지 않는다. 다만 CI hygiene이 missing_regression_test로 막혀 있고, enforce-target도 같은 이유로 실패했으며, 봇이 draft로 붙잡아 둔 상태다. 작성자가 로컬에서 router/state 관련 테스트를 돌렸다고 적었지만, 이 PR 자체에는 테스트 파일 변경이 없다. 게이트는 “src를 바꿨으면 테스트도 같이”를 본다.

src/router.ts - REGISTRY_* 맵은 모듈 로드 시점에만 채워진다. PROVIDER_REGISTRY가 readonly 상수라서 지금은 맞지만, 나중에 런타임에 레지스트리 항목을 붙이는 길이 생기면 이 캐시는 낡은 값이 된다. 지금은 문제 없고, “정적 전제”를 주석으로 남겨 두면 이후 기여자가 덜 헷갈린다.

src/router.ts knownModelIdsForProvider - 예전 코드는 transport guard를 통과한 뒤에야 registry models + hint map keys를 모았다. 새 코드도 guard 안에서 STATIC_MODEL_IDS만 읽으므로 의미는 같다. 다만 기존 slug-codec 테스트(tests/codex-integration/slug-codec.test.ts의 knownModelIdsForProvider 합집합 케이스)가 이미 있고, 이 PR은 그 파일을 건드리지 않아 hygiene에 걸린다. 캐시 경로를 고정하는 한 줄짜리 회귀(예: zenmux/nvidia hint-map id가 여전히 포함되는지)를 추가하는 편이 게이트와 리뷰어 모두에게 싸다.

src/responses/state.ts writeBoundedSnapshot - Array.from(states)도 여전히 O(N) 엔트리 배열을 만든다. 진짜 이득은 reverse 복사 제거보다, 전체 트리를 두 번 stringify하지 않는 쪽에 가깝다. PR 설명의 “24–48 MiB transient” 주장은 이 경로를 겨냥한 것이고, 방향은 타당하다.

src/responses/state.ts payload 조립 - 손조립 JSON이 digest/skip 로직의 입력이라, 바이트가 한 글자라도 어긋나면 “같다” 판정이 깨지거나 반대로 잘못된 skip이 난다. 로컬 등가성은 확인됐지만, tests/responses/responses-state.test.ts에 “flush 후 파일 내용이 version:2 + states 배열 형태이고, 같은 상태에서 두 번 flush하면 digest skip이 유지된다” 수준의 고정 테스트가 이 PR에 없다. hygiene가 가리키는 바로 그 구멍이다.

intake: hygiene-blocked / draft - 작성자 checklist는 4/4로 찍혀 있지만, 게이트는 여전히 missing_regression_test로 DRAFT다. “로컬에서 기존 테스트 통과”와 “이 PR이 테스트를 추가했다”는 다른 조건이다.

메인테이너의 판단이 필요한 지점

너의 추천
머지하지 말고 draft 유지. 작성자에게 (1) tests/routing 또는 tests/codex-integration/slug-codec.test.ts에 REGISTRY 캐시 경로를 고정하는 회귀 한두 개, (2) tests/responses/responses-state.test.ts에 writeBoundedSnapshot payload/digest 등가(또는 이중 flush skip) 고정을 추가하라고 요청한 뒤, hygiene·enforce-target 초록이 되면 Ready로 올려 리뷰한다. 코드 방향 자체는 dev와 충돌하지 않으니, 게이트만 풀리면 작은 초록 후보로 보면 된다.

이 댓글은 grok-bot이 작성했습니다

@github-actions github-actions Bot removed the intake: hygiene-blocked Deterministic PR hygiene checks failed label Sep 15, 2026
@chilung-cgu
chilung-cgu marked this pull request as ready for review September 15, 2026 21:31
@github-actions
github-actions Bot marked this pull request as draft September 15, 2026 21:32
@chilung-cgu
chilung-cgu marked this pull request as ready for review September 15, 2026 21:32
@github-actions
github-actions Bot marked this pull request as draft September 15, 2026 21:33
@chilung-cgu
chilung-cgu marked this pull request as ready for review September 15, 2026 21:34
@lidge-jun
lidge-jun force-pushed the perf/core-alloc-and-cache branch from f7ece40 to 8f5a334 Compare September 16, 2026 08:56
@github-actions
github-actions Bot marked this pull request as draft September 16, 2026 08:57
@lidge-jun
lidge-jun force-pushed the perf/core-alloc-and-cache branch 2 times, most recently from 8f86dd2 to eca2882 Compare September 19, 2026 12:39
@chilung-cgu
chilung-cgu force-pushed the perf/core-alloc-and-cache branch from eca2882 to e70c08c Compare September 20, 2026 09:25
@chilung-cgu
chilung-cgu marked this pull request as ready for review September 20, 2026 09:27
@github-actions
github-actions Bot marked this pull request as draft September 20, 2026 09:32
@chilung-cgu
chilung-cgu marked this pull request as ready for review September 20, 2026 14:49
@github-actions
github-actions Bot marked this pull request as draft September 20, 2026 14:49
@chilung-cgu
chilung-cgu marked this pull request as ready for review September 20, 2026 14:50
@github-actions
github-actions Bot marked this pull request as draft September 20, 2026 14:51
github-actions Bot and others added 25 commits September 30, 2026 20:06
…#6317)

Co-authored-by: github-actions[bot] <41898282+github-actions[bot]@users.noreply.github.com>
Co-authored-by: luvs01 <27862058+luvs01@users.noreply.github.com>
…jun#6308)

Co-authored-by: luvs01 <27862058+luvs01@users.noreply.github.com>
* fix(devin): bound held signature type payloads

Carry source commit 3a257a6.
Count signatureType in the held UTF-16 payload budget and cap its
wire bytes before UTF-8 decoding. Preserve the signed retry path below
the budget, with exact byte-boundary and multibyte regression coverage.

* test(devin): verify oversized-type legacy signature replay

---------

Co-authored-by: luvs01 <27862058+luvs01@users.noreply.github.com>
* fix(redaction): bound XML identifying-attribute scans

Carry luvs01#691 at f42e713. Replace wall-clock regression with delimiter-work accounting and preserve malformed/escaped credential coverage.

* fix(redaction): recognize quoted XML tag delimiters

Scan tag terminators outside quoted values, cover credential masking after embedded delimiters, and count manual character work in linear-scaling regressions.
…idge-jun#6326)

Preserve display snapshots and legacy login observations while gating pool policy changes on the captured writer. Cover missing writers in compact and WebSocket paths.
…idge-jun#6328)

Carries lidge-jun#6281 unchanged plus maintainer review fixes; the contributor gate re-drafted the original after the maintainer push.

Co-authored-by: lonefisher <132996955+lonefisher@users.noreply.github.com>
…idge-jun#6329)

Carries lidge-jun#6316 unchanged plus maintainer review fixes; the contributor gate re-drafted the original after the maintainer push.

Co-authored-by: vadymhimself <11277453+vadymhimself@users.noreply.github.com>
…g row (lidge-jun#6330)

Carries lidge-jun#6295 with a maintainer privacy rework: the stored reason is limited to the HTTP status plus an allowlisted Anthropic error type, so upstream message text (which can echo account identifiers or request content) never reaches the request log or usage history. Client responses are unchanged.

Co-authored-by: sh940701 <visioner2168@gmail.com>
… publication (lidge-jun#6331)

Carries lidge-jun#6260 with a maintainer fix: a file-loaded config keeps its file provenance after discovery inventory drift, so stale discovery is refused under the mutation coordinator instead of disabling a model in memory that a later unrelated save would persist over the operator's choice.

Co-authored-by: colthreepv <2657230+colthreepv@users.noreply.github.com>
…licy (lidge-jun#6333)

File-backed inventory drift rejected ordinary model reads with catalog_busy. Identical synthetic inventory succeeds on the same cached roster, ruling out row replacement and cache revision churn in the reproduction.

Allow read callers to receive a detached new-arrival policy projection. Management renders disabled arrivals; other shared-fetch consumers receive only visible projected rows. Keep retained-sync and direct stale-writer refusal contracts, inventory/revision checks, and persisted merge baselines intact.

Verification: eight authorized files, 80 passed / 0 failed; after adding save-safety coverage, runtime-policy and ratchet files, 24 passed / 0 failed. Typecheck and structure checks pass. Full suite and docs build were excluded by the delegated command restrictions.
…jun#6341)

* fix(xai): report a current Grok CLI version on the OAuth path

xAI now answers Grok OAuth requests that report a client version below 1.0.13 with HTTP 426 ("Your Grok CLI version (0.2.93) is outdated"), so every SuperGrok OAuth request failed. Report 1.0.25, the current stable Grok CLI, in x-grok-client-version and the User-Agent.

Carries lidge-jun#6339.

Co-authored-by: unsafe9 <24631203+unsafe9@users.noreply.github.com>

* test(xai): pin Grok compatibility header regression

---------

Co-authored-by: unsafe9 <24631203+unsafe9@users.noreply.github.com>
…idge-jun#6344)

Carries lidge-jun#6299 with the buffering note translated into the ja, ko, ru and zh-cn Claude Code guides.

Co-authored-by: foxytanuki <45069709+foxytanuki@users.noreply.github.com>
…idge-jun#6345)

Carries lidge-jun#6278 with maintainer review fixes: the chip description points at the text-only quota table, and the hover bridge spans both the chip and popover widths so wide chips keep the popover open.

Co-authored-by: colthreepv <2657230+colthreepv@users.noreply.github.com>
…jun#6346)

PUT /api/subagent-models validated pickerOrder against visible routed slugs only, so the
documented complete-picker ordering (a bare native id such as gpt-5.6-sol) could not be saved
through the API. Visible, enabled native catalog rows are now accepted; disabled and unknown
native ids are still rejected.

Closes lidge-jun#6338
…#6351)

Co-authored-by: github-actions[bot] <41898282+github-actions[bot]@users.noreply.github.com>
writeBoundedSnapshot already stringifies every candidate entry once in
selectSnapshotEntries to measure its UTF-8 size, then stringified the whole
selection again to build the payload. Keep the measured strings and join them
instead. The output is byte-identical; a pinned-bytes regression test covers
stubs, residents, multibyte text, escapes, and both byte budgets.

Reworks the snapshot half of lidge-jun#4732 onto the two-pass selector now on dev.

Co-authored-by: WU, CHI-LUNG <chilung-cgu@users.noreply.github.com>
Keeps the author history on the PR branch. The tree is the dev-based rework: the router registry cache is dropped (no measurable routeModel change) and the snapshot change is reimplemented on the two-pass selector.
@github-actions github-actions Bot added the intake: hygiene-blocked Deterministic PR hygiene checks failed label Oct 1, 2026
lidge-jun added a commit that referenced this pull request Oct 1, 2026
…6360)

Carries the snapshot half of #4732: keep the per-entry strings measured by selectSnapshotEntries and join them, so a snapshot write serializes entries once instead of twice. Output is byte-identical; a pinned-bytes regression test covers it.

Co-authored-by: WU, CHI-LUNG <chilung-cgu@users.noreply.github.com>
@lidge-jun

Copy link
Copy Markdown
Owner

Thank you, @chilung-cgu. This landed on dev through the maintainer carry #6360 (squash 0328373fb8), with you credited as co-author.

What was carried: the snapshot change. dev had since reshaped snapshot selection into selectSnapshotEntries, so the idea was reapplied there: the entry strings already serialized to measure their size are now joined into the payload instead of serializing the whole selection a second time. Output stays byte-identical. On a 24 MiB snapshot a standalone comparison against the previous dev code measured the median write going from about 29–30 ms to about 18 ms.

What was not carried: the static PROVIDER_REGISTRY lookup maps in src/router.ts. Measured against current dev, they made no measurable difference to routeModel, so they would add cached state without a benefit.

Closing as carried. Thanks again for finding the double serialization.

@lidge-jun lidge-jun closed this Oct 1, 2026
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

enhancement New feature or request intake: hygiene-blocked Deterministic PR hygiene checks failed priority: P2 Medium: provider/client-specific bug with a workaround, bounded enhancement tied to a tracked issue,

Projects

None yet

Development

Successfully merging this pull request may close these issues.