What do you want to change?
Speed up PricingMap::find() by adding a result cache so repeated lookups of the same model name skip expensive fuzzy matching. Also remove a redundant missing-pricing check in the OpenCode adapter.
Why?
bunx ccusage@latest opencode takes over two minutes on my machine.
Profiling showed that 91% of the time goes to PricingMap::find() — ~21 seconds for OpenCode alone, plus similar overhead from other agents.
My OpenCode SQLite database has ~87,000 messages and is 3.6 GB. Despite only 47 unique model names, each message independently repeats the full pricing lookup chain. 36% of lookups miss the exact HashMap and fall through to iterating all 2,200 pricing entries with substring matching — roughly 150 million string comparisons in total.
How? (optional)
Two changes:
- Add a result cache to PricingMap::find() — A OnceLock<Mutex<FxHashMap<String, Option>>> caches lookup results by model name (including misses). With only 47 unique models, the expensive fallback scan would run at most 47 times instead of 67,000+. The cache is invalidated automatically whenever the pricing table is updated (load_json, apply_overrides, etc.), so it stays consistent.
- Skip the redundant missing-pricing check in the OpenCode adapter — Both calculate_open_code_cost and missing_open_code_pricing independently iterate through the same model candidates. When cost calculation already found pricing, the second check can be skipped entirely.
The cache lives in the shared PricingMap layer, so all agent adapters (Claude Code, Codex, Amp, etc.) benefit from it transparently. Memory overhead is negligible (~5 KB for typical usage).
What do you want to change?
Speed up PricingMap::find() by adding a result cache so repeated lookups of the same model name skip expensive fuzzy matching. Also remove a redundant missing-pricing check in the OpenCode adapter.
Why?
bunx ccusage@latest opencodetakes over two minutes on my machine.Profiling showed that 91% of the time goes to PricingMap::find() — ~21 seconds for OpenCode alone, plus similar overhead from other agents.
My OpenCode SQLite database has ~87,000 messages and is 3.6 GB. Despite only 47 unique model names, each message independently repeats the full pricing lookup chain. 36% of lookups miss the exact HashMap and fall through to iterating all 2,200 pricing entries with substring matching — roughly 150 million string comparisons in total.
How? (optional)
Two changes:
The cache lives in the shared PricingMap layer, so all agent adapters (Claude Code, Codex, Amp, etc.) benefit from it transparently. Memory overhead is negligible (~5 KB for typical usage).