Skip to content
This repository was archived by the owner on Jul 30, 2026. It is now read-only.

perf(models): avoid redundant canonicalization work - #139

Merged
makoMakoGo merged 2 commits into
personal/local-clientsfrom
agent/optimize-model-canonicalization
Jul 12, 2026
Merged

perf(models): avoid redundant canonicalization work#139
makoMakoGo merged 2 commits into
personal/local-clientsfrom
agent/optimize-model-canonicalization

Conversation

@makoMakoGo

@makoMakoGo makoMakoGo commented Jul 12, 2026

Copy link
Copy Markdown
Owner

Summary

  • keep global model suffix cleanup borrow-preserving for terminal release and free-channel decorations
  • allocate only when cleanup must remove an interior channel tag or when the final canonical ID is materialized
  • short-circuit already-canonical OpenAI base IDs and stop source dispatch after the OpenAI branch

Root cause

The GPT-5.6 canonicalization fix correctly established the pipeline as terminal normalization, lexical decoration cleanup, then semantic canonicalization. Its lexical stage eagerly converted the normalized model into an owned String, copied the full decorated ID, truncated it in place, and then sent an already-canonical OpenAI base through the remaining parser checks.

That extra allocation work and parsing caused the gpt4o_mini_date CodSpeed benchmark in PR #136 to regress by 16.56%.

Impact

Canonical model behavior remains unchanged, including GPT-5.6 alias idempotence and the existing Claude, GLM, free-channel, release-date, and reasoning-effort identities. Terminal suffix removal now borrows the canonical slice until a final owned ID is required, and canonical OpenAI base IDs avoid redundant parsing.

Validation

  • cargo test — 2,137 passed, 5 ignored
  • cargo clippy --workspace --all-targets -- -D warnings
  • cargo build --release -p tokscale-cli
  • canonical_model_id_cleanup/gpt4o_mini_date repeated local baseline: approximately 273–276 ns
  • same local benchmark after the change: approximately 187–190 ns, about 30% faster
  • CodSpeed simulation: 7.4 µs → 6.4 µs, a 16.03% improvement; all other 26 benchmarks remained unchanged

References

Summary by CodeRabbit

  • Bug Fixes
    • 优化模型标识符的规范化处理,提升对 GPT-5.6、GPT-4.1、GPT-4o 及其他 OpenAI 模型名称的识别准确性。
    • 统一处理 free 等模型后缀及其变体,减少模型名称解析异常。
    • 在不改变现有结果的前提下,降低规范化过程中的额外资源开销,提升处理效率。

Keep lexical suffix cleanup borrow-preserving so dated and free-channel model IDs allocate only their final identity. Short-circuit canonical OpenAI base models and stop dispatch after the OpenAI branch, removing the extra work that regressed gpt4o_mini_date while preserving the terminal-to-lexical-to-semantic pipeline.
@coderabbitai

coderabbitai Bot commented Jul 12, 2026

Copy link
Copy Markdown

Review Change Stack

Warning

Review limit reached

@makoMakoGo, you've reached your PR review limit, so we couldn't start this review.

Next review available in: 41 minutes

Enable usage-based reviews in Billing to review now. Otherwise, wait until the next included review is available.
You're only billed for reviews past your plan's rate limits ($0.25/file).

How can I continue?

After more reviews become available, a review can be triggered using the @coderabbitai review command as a PR comment. Alternatively, push new commits to this PR.

To avoid repeated limits, reduce automatic review volume by pausing incremental auto-reviews earlier, using label-based review opt-in, excluding WIP or generated PR titles, or requesting reviews manually when the PR is ready. If your team needs uninterrupted high-volume reviews, an organization admin can enable usage-based reviews.

How do review limits work?

CodeRabbit enforces per-developer PR review limits for each organization. Most developers receive the normal plan review availability.

For paid Pro and Pro+ PR reviews, CodeRabbit uses adaptive limits for sustained high-volume activity. When a developer's recent PR review activity reaches the 95th percentile or higher among CodeRabbit users, additional reviews become available more gradually as earlier reviews age out of the rolling window.

Please refer docs for additional details.

Review details
⚙️ Run configuration

Configuration used: Organization UI

Review profile: CHILL

Plan: Pro

Run ID: 5d950e96-a820-4fc6-a0df-d6ce0b18edf4

📥 Commits

Reviewing files that changed from the base of the PR and between 9bf784f and 248c4b3.

📒 Files selected for processing (1)
  • crates/tokscale-core/src/model_aliases.rs

Walkthrough

本次变更重构模型 ID 的全局后缀清洗,改用 Cow 减少不必要分配,并调整 OpenAI source 模型的 GPT-5.6 映射及基础模型排除顺序。

Changes

模型 ID 规范化

Layer / File(s) Summary
全局后缀词法清洗
crates/tokscale-core/src/model_aliases.rs
canonicalize_model_id 通过 Cow 传递清洗结果,并在必要时才分配 String-free:free(free) 及相关变体统一使用截断或替换策略。
OpenAI source 模型处理
crates/tokscale-core/src/model_aliases.rs
简化 gpt- 分支返回路径,前移 GPT-5.6 映射,并直接排除 gpt-4.1gpt-4o 及通用 OpenAI source 基础模型。

Estimated code review effort: 4 (Complex) | ~45 minutes

Possibly related PRs

Poem

小兔挥爪洗模型,
Cow 借来少分配;
GPT-5.6 找到新归宿,
free 尾巴轻轻褪。
OpenAI 分支路更清,
规范 ID 蹦蹦归队。

🚥 Pre-merge checks | ✅ 5
✅ Passed checks (5 passed)
Check name Status Explanation
Description Check ✅ Passed Check skipped - CodeRabbit’s high-level summary is enabled.
Title check ✅ Passed 标题准确概括了本次以减少模型规范化重复工作为目标的性能优化。
Docstring Coverage ✅ Passed No functions found in the changed files to evaluate docstring coverage. Skipping docstring coverage check.
Linked Issues check ✅ Passed Check skipped because no linked issues were found for this pull request.
Out of Scope Changes check ✅ Passed Check skipped because no linked issues were found for this pull request.
✨ Finishing Touches
🧪 Generate unit tests (beta)
  • Create PR with unit tests
  • Commit unit tests in branch agent/optimize-model-canonicalization

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.

@codspeed-hq

codspeed-hq Bot commented Jul 12, 2026

Copy link
Copy Markdown

Merging this PR will improve performance by 16.16%

⚠️ Different runtime environments detected

Some benchmarks with significant performance changes were compared across different runtime environments,
which may affect the accuracy of the results.

Open the report in CodSpeed to investigate

⚡ 1 improved benchmark
✅ 26 untouched benchmarks

Performance Changes

Benchmark BASE HEAD Efficiency
canonical_model_id_cleanup[gpt4o_mini_date] 7.4 µs 6.4 µs +16.16%

Tip

Curious why this is faster? Comment @codspeedbot explain why this is faster on this PR, or directly use the CodSpeed MCP with your agent.


Comparing agent/optimize-model-canonicalization (248c4b3) with personal/local-clients (802bb29)

Open in CodSpeed

@makoMakoGo
makoMakoGo marked this pull request as ready for review July 12, 2026 07:51

@cubic-dev-ai cubic-dev-ai Bot 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.

No issues found across 1 file

Re-trigger cubic

@coderabbitai coderabbitai Bot 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.

🧹 Nitpick comments (1)
crates/tokscale-core/src/model_aliases.rs (1)

234-243: 📐 Maintainability & Code Quality | 🔵 Trivial | ⚡ Quick win

canonical_gpt_5_6_base 尾部兜底分支已变为死代码。

第 234-236 行新增的早返回已经处理了 canonical_gpt_5_6_base(model) 返回 Some 的所有情况(无论是否与 model 相同都会 return)。函数体内 model(第 232 行绑定后)此后从未被重新赋值,因此第 274-277 行再次调用 canonical_gpt_5_6_base(model) 时必然返回 None(否则第 234 行早已 return),.filter(...).map(...) 也永远不会产生 Some。这段代码是本次重构留下的死代码,与本 PR「避免冗余分配/计算」的目标本身相悖。

建议直接移除该冗余尾部分支,用显式 None 收尾,减少误导性代码。

♻️ 移除死代码
-
-    canonical_gpt_5_6_base(model)
-        .filter(|canonical| *canonical != model)
-        .map(str::to_string)
+
+    None
 }

Also applies to: 274-277

🤖 Prompt for AI Agents
Verify each finding against current code. Fix only still-valid issues, skip the
rest with a brief reason, keep changes minimal, and validate.

In `@crates/tokscale-core/src/model_aliases.rs` around lines 234 - 243, Remove the
redundant trailing canonical_gpt_5_6_base(model) fallback after the existing
early return, since it can never produce Some. End the function explicitly with
None while preserving the preceding gpt-4.1 and OpenAI base-model handling.
🤖 Prompt for all review comments with AI agents
Verify each finding against current code. Fix only still-valid issues, skip the
rest with a brief reason, keep changes minimal, and validate.

Nitpick comments:
In `@crates/tokscale-core/src/model_aliases.rs`:
- Around line 234-243: Remove the redundant trailing
canonical_gpt_5_6_base(model) fallback after the existing early return, since it
can never produce Some. End the function explicitly with None while preserving
the preceding gpt-4.1 and OpenAI base-model handling.

ℹ️ Review info
⚙️ Run configuration

Configuration used: Organization UI

Review profile: CHILL

Plan: Pro

Run ID: 31131c77-f39c-48e1-8249-7b65a5bac3cf

📥 Commits

Reviewing files that changed from the base of the PR and between 802bb29 and 9bf784f.

📒 Files selected for processing (1)
  • crates/tokscale-core/src/model_aliases.rs

@makoMakoGo
makoMakoGo merged commit 3f06750 into personal/local-clients Jul 12, 2026
10 checks passed
@makoMakoGo
makoMakoGo deleted the agent/optimize-model-canonicalization branch July 12, 2026 09:39
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant