Skip to content

Conversation

@Laisky
Copy link
Owner

@Laisky Laisky commented Feb 6, 2026

💡 What:

  • Modified relay/pricing/global.go to use GetGlobalModelConfig in GetModelRatioWithThreeLayers, GetCompletionRatioWithThreeLayers, and GetVideoPricingWithThreeLayers.
  • Modified relay/controller/image.go to use GetGlobalModelConfig for image pricing resolution.

🎯 Why:

GetGlobalModelPricing() clones the entire global pricing map and all its entries (potentially thousands of models) on every call. In a high-throughput gateway, this causes massive CPU and memory allocation overhead for every request, as it was being called twice or more per request.

📊 Impact:

  • Reduces memory allocations and CPU usage by avoiding redundant map cloning.
  • If N is the number of models, reduces complexity from O(N) with cloning to O(1) with single entry cloning.

🔬 Measurement:

Verified via go test and go vet. Performance improvement is architectural and avoids a known O(N) bottleneck in a hot path.


PR created automatically by Jules for task 5915013610921063642 started by @Laisky

Summary by CodeRabbit

Release Notes

Performance Improvements

  • Optimized pricing configuration data retrieval mechanisms to significantly reduce computational overhead and improve response times for all pricing calculations.
  • Enhanced pricing data access efficiency by streamlining lookup patterns for improved overall system responsiveness and performance.

Documentation

  • Added performance optimization best practices and guidelines to prevent inefficiencies in critical system execution paths.

Co-authored-by: Laisky <4532436+Laisky@users.noreply.github.com>
@google-labs-jules
Copy link

👋 Jules, reporting for duty! I'm here to lend a hand with this pull request.

When you start a review, I'll add a 👀 emoji to each comment to let you know I've read it. I'll focus on feedback directed at me and will do my best to stay out of conversations between you and other bots or reviewers to keep the noise down.

I'll push a commit with your requested changes shortly after. Please note there might be a delay between these steps, but rest assured I'm on the job!

For more direct control, you can switch me to Reactive Mode. When this mode is on, I will only act on comments where you specifically mention me with @jules. You can find this option in the Pull Request section of your global Jules UI settings. You can always switch back!

New to Jules? Learn more at jules.google/docs.


For security, I will only act on instructions from the user who triggered this task.

Copilot AI review requested due to automatic review settings February 6, 2026 19:12
@coderabbitai
Copy link

coderabbitai bot commented Feb 6, 2026

📝 Walkthrough

Walkthrough

This pull request refactors the pricing configuration retrieval mechanism by replacing direct map indexing with dedicated function calls. The changes introduce GetGlobalModelConfig() as the primary interface for accessing model pricing configuration across multiple layers, and remove some redundant cloning operations in video pricing retrieval. Documentation updates outline performance considerations for map cloning in hot paths.

Changes

Cohort / File(s) Summary
Documentation
.jules/bolt.md
Added documentation section describing performance concerns from cloning large maps in hot paths and recommending targeted lookup functions or efficient caching mechanisms.
Pricing Retrieval Refactoring
relay/pricing/global.go
Refactored three pricing resolver functions (GetModelRatioWithThreeLayers, GetCompletionRatioWithThreeLayers, GetVideoPricingWithThreeLayers) to use GetGlobalModelConfig() instead of direct map indexing. Removed cloning in video pricing retrieval when returning global config data.
API Usage Update
relay/controller/image.go
Updated pricing config retrieval from GetGlobalModelPricing()[imageModel] to GetGlobalModelConfig(imageModel), altering the retrieval mechanism from map lookup to direct function call.

Estimated code review effort

🎯 3 (Moderate) | ⏱️ ~20 minutes

Poem

🐰 Hoppity hop, maps we'll not clone in haste,
Functions now fetch what's needed, no waste!
Through pricing's layers we bound with delight,
Config retrieval made swift and made right! 🎯

🚥 Pre-merge checks | ✅ 2 | ❌ 1
❌ Failed checks (1 warning)
Check name Status Explanation Resolution
Docstring Coverage ⚠️ Warning Docstring coverage is 75.00% which is insufficient. The required threshold is 80.00%. Write docstrings for the functions missing them to satisfy the coverage threshold.
✅ Passed checks (2 passed)
Check name Status Explanation
Description Check ✅ Passed Check skipped - CodeRabbit’s high-level summary is enabled.
Title check ✅ Passed The title directly addresses the main change: optimizing global pricing lookup performance by avoiding map cloning. It matches the PR's core objective.

✏️ Tip: You can configure your own custom pre-merge checks in the settings.

✨ Finishing touches
  • 📝 Generate docstrings
🧪 Generate unit tests (beta)
  • Create PR with unit tests
  • Post copyable unit tests in a comment
  • Commit unit tests in branch bolt-optimize-global-pricing-lookup-5915013610921063642

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 and usage tips.

@github-actions
Copy link

github-actions bot commented Feb 6, 2026

Merging this branch will not change overall coverage

Impacted Packages Coverage Δ 🤖
github.com/Laisky/one-api/relay/controller 0.00% (ø)
github.com/Laisky/one-api/relay/pricing 0.00% (ø)

Coverage by file

Changed files (no unit tests)

Changed File Coverage Δ Total Covered Missed 🤖
github.com/Laisky/one-api/relay/controller/image.go 0.00% (ø) 0 0 0
github.com/Laisky/one-api/relay/pricing/global.go 0.00% (ø) 0 0 0

Please note that the "Total", "Covered", and "Missed" counts above refer to code statements instead of lines of code. The value in brackets refers to the test coverage of that file in the old version of the code.

Copy link

Copilot AI left a comment

Choose a reason for hiding this comment

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

Pull request overview

Optimizes hot-path pricing resolution in One-API by avoiding full global pricing map cloning on per-request lookups, switching to targeted single-model retrieval via GetGlobalModelConfig.

Changes:

  • Updated three-layer pricing helpers to use GetGlobalModelConfig instead of cloning the entire global pricing map.
  • Updated image relay billing to use GetGlobalModelConfig for global image pricing resolution.
  • Added an internal Bolt note documenting the “avoid map cloning in hot path” learning.

Reviewed changes

Copilot reviewed 3 out of 3 changed files in this pull request and generated 2 comments.

File Description
relay/pricing/global.go Replaces full-map global pricing retrieval with single-model config lookups in three-layer pricing helpers.
relay/controller/image.go Switches global image pricing fallback to use GetGlobalModelConfig during image quota computation.
.jules/bolt.md Documents performance guidance about avoiding large map clones in request hot paths.

Comment on lines 415 to 423
imagePriceUsd := 0.0
if resolvedConfig.Image != nil {
imagePriceUsd = resolvedConfig.Image.PricePerImageUsd
}
if imagePriceUsd == 0 {
if pm, ok := pricing.GetGlobalModelPricing()[imageModel]; ok && pm.Image != nil {
if pm, ok := pricing.GetGlobalModelConfig(imageModel); ok && pm.Image != nil {
imagePriceUsd = pm.Image.PricePerImageUsd
}
}
Copy link

Copilot AI Feb 6, 2026

Choose a reason for hiding this comment

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

imagePriceUsd fallback only checks global pricing when resolvedConfig.Image is nil/zero, but resolvedConfig comes from ResolveModelConfig, which returns the channel model config as soon as it exists (even if it has no Image metadata). This means provider default per-image pricing can be skipped entirely whenever a channel has a model entry without Image pricing, leading to incorrect billing (per-token path instead of per-image). Consider resolving image pricing with the same 3-layer precedence (channel → provider defaults → global) and using that resolved image pricing consistently for defaults/validation and imagePriceUsd.

Copilot uses AI. Check for mistakes.
Comment on lines 415 to 423
imagePriceUsd := 0.0
if resolvedConfig.Image != nil {
imagePriceUsd = resolvedConfig.Image.PricePerImageUsd
}
if imagePriceUsd == 0 {
if pm, ok := pricing.GetGlobalModelPricing()[imageModel]; ok && pm.Image != nil {
if pm, ok := pricing.GetGlobalModelConfig(imageModel); ok && pm.Image != nil {
imagePriceUsd = pm.Image.PricePerImageUsd
}
}
Copy link

Copilot AI Feb 6, 2026

Choose a reason for hiding this comment

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

There’s no regression test covering the pricing-precedence edge case here (channel has a ModelConfig entry without Image metadata, provider defaults define Image.PricePerImageUsd). Adding a unit test in the existing image billing/controller test suite for this scenario would help prevent future under/over-charging regressions.

Copilot generated this review using guidance from repository custom instructions.
@Laisky Laisky merged commit 74d630f into main Feb 7, 2026
18 checks passed
@Laisky Laisky deleted the bolt-optimize-global-pricing-lookup-5915013610921063642 branch February 7, 2026 03:20
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