From 476aed3fd66009a190d43d27e887bc6e6e49e05f Mon Sep 17 00:00:00 2001 From: Ryan Crabbe Date: Fri, 20 Feb 2026 09:32:06 -0800 Subject: [PATCH] perf: use frozenset for enum membership checks to avoid O(n) iteration Replace O(n) list/enum iteration with O(1) frozenset lookups: - route_checks.py: frozenset for LiteLLMRoutes._member_names_ - types/utils.py: cache DynamicPromptManagementParamLiteral.list_all_params() - utils.py: use existing LlmProvidersSet instead of _member_map_.values() --- litellm/proxy/auth/route_checks.py | 6 ++++-- litellm/types/utils.py | 7 ++++++- litellm/utils.py | 2 +- 3 files changed, 11 insertions(+), 4 deletions(-) diff --git a/litellm/proxy/auth/route_checks.py b/litellm/proxy/auth/route_checks.py index a00401008fc..751027975de 100644 --- a/litellm/proxy/auth/route_checks.py +++ b/litellm/proxy/auth/route_checks.py @@ -12,6 +12,8 @@ from litellm.proxy._types import ( UserAPIKeyAuth, ) +_LITELLM_ROUTES_MEMBER_NAMES: frozenset = frozenset(LiteLLMRoutes._member_names_) + from .auth_checks_organization import _user_is_org_admin @@ -61,11 +63,11 @@ class RouteChecks: ## check if 'allowed_route' is a field name in LiteLLMRoutes if any( - allowed_route in LiteLLMRoutes._member_names_ + allowed_route in _LITELLM_ROUTES_MEMBER_NAMES for allowed_route in valid_token.allowed_routes ): for allowed_route in valid_token.allowed_routes: - if allowed_route in LiteLLMRoutes._member_names_: + if allowed_route in _LITELLM_ROUTES_MEMBER_NAMES: if RouteChecks.check_route_access( route=route, allowed_routes=LiteLLMRoutes._member_map_[allowed_route].value, diff --git a/litellm/types/utils.py b/litellm/types/utils.py index 9228b25b03e..9befa9dd39f 100644 --- a/litellm/types/utils.py +++ b/litellm/types/utils.py @@ -3430,7 +3430,12 @@ class DynamicPromptManagementParamLiteral(str, Enum): @classmethod def list_all_params(cls): - return [param.value for param in cls] + return _DYNAMIC_PROMPT_MANAGEMENT_PARAMS + + +_DYNAMIC_PROMPT_MANAGEMENT_PARAMS: frozenset = frozenset( + param.value for param in DynamicPromptManagementParamLiteral +) class CallbacksByType(TypedDict): diff --git a/litellm/utils.py b/litellm/utils.py index 241b9d217b7..a4bdf38e742 100644 --- a/litellm/utils.py +++ b/litellm/utils.py @@ -3167,7 +3167,7 @@ def get_optional_params_embeddings( # noqa: PLR0915 optional_params = {} if ( custom_llm_provider is not None - and custom_llm_provider in LlmProviders._member_map_.values() + and custom_llm_provider in LlmProvidersSet ): provider_config = ProviderConfigManager.get_provider_embedding_config( model=model,