perf(llm): use Arc<[AnyProvider]> in RouterProvider, add cost_tiers config#1764
Merged
perf(llm): use Arc<[AnyProvider]> in RouterProvider, add cost_tiers config#1764
Conversation
…onfig (#1724) - Change RouterProvider.providers from Vec<AnyProvider> to Arc<[AnyProvider]>; self.clone() on every LLM request drops from O(N * provider_size) to O(1) for the providers field across all strategies (EMA, Thompson, Cascade) - Cascade chat/chat_stream bypass ordered_providers() and pass &self.providers slice directly, eliminating a Vec allocation per request on the hot path - Add cost_tiers: Option<Vec<String>> to CascadeConfig and CascadeRouterConfig; providers are sorted once at construction time in with_cascade(); providers absent from cost_tiers are appended in original chain order; empty list and None both preserve chain order; unknown names are silently ignored - Wire cost_tiers through bootstrap/provider.rs - Add cost_tiers prompt to --init wizard cascade section - Add MockProvider::with_name() builder for tests requiring distinct names - Add 10 new tests: Arc::ptr_eq clone, cost_tiers ordering, edge cases (empty vec, all listed, duplicates, unknown names, empty router, tools bypass)
Remove .clone() from &router.providers.clone() in chat() and chat_stream() cascade branches — Arc<[T]> derefs to &[T] directly, so the clone caused an unnecessary atomic refcount bump per cascade request.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
Closes #1724.
Summary
providers: Vec<AnyProvider>withArc<[AnyProvider]>inRouterProvider.self.clone()on everychat()/chat_stream()/chat_with_tools()/embed()call now costs O(1) atomic refcount bump instead of O(N × provider_size) heap allocation. The Cascade hot path passes&router.providersdirectly (via Deref) tocascade_chat/cascade_chat_stream, bypassingordered_providers()entirely.cost_tiers: Option<Vec<String>>field onCascadeConfigandCascadeRouterConfig. When set, providers are sorted cheapest-first at construction time (zero per-request cost). Unknown names are silently ignored; unlisted providers are appended in original chain order.--initwizard prompts for cost_tiers only when cascade strategy is selected.Test plan
cargo +nightly fmt --check✓cargo clippy --workspace --features full -- -D warnings✓