Skip to content

Commit 9d064ab

Browse files
perf: optimize O(N) list iteration in _check_model_exists (#717)
Replaces the O(N) list iteration in _check_model_exists with an O(1) dictionary lookup by reusing the model description cache. Introduces a private _ensure_model_cache method to consolidate cache initialization logic and ensure consistent lookup behavior. Co-authored-by: google-labs-jules[bot] <161369871+google-labs-jules[bot]@users.noreply.github.com>
1 parent b4abe65 commit 9d064ab

1 file changed

Lines changed: 16 additions & 12 deletions

File tree

qwen3_embed/common/model_management.py

Lines changed: 16 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -91,16 +91,23 @@ def _clear_model_cache(cls) -> None:
9191
if "_model_description_cache" in cls.__dict__:
9292
delattr(cls, "_model_description_cache")
9393

94+
@classmethod
95+
def _ensure_model_cache(cls) -> dict[str, T]:
96+
cache = cls.__dict__.get("_model_description_cache")
97+
if cache is None:
98+
# ⚡ Bolt: Using dictionary lookup for O(1) model checks and descriptions
99+
cache = {model.model.lower(): model for model in cls._list_supported_models()}
100+
cls._model_description_cache = cache
101+
return cache
102+
94103
@classmethod
95104
def _check_model_exists(cls, model: str) -> None:
96-
registered_models = cls._list_supported_models()
97-
model_lower = model.lower()
98-
for registered_model in registered_models:
99-
if model_lower == registered_model.model.lower():
100-
raise ValueError(
101-
f"Model {model} is already registered in {cls.__name__}, if you still want to add this model, "
102-
f"please use another model name"
103-
)
105+
cache = cls._ensure_model_cache()
106+
if model.lower() in cache:
107+
raise ValueError(
108+
f"Model {model} is already registered in {cls.__name__}, if you still want to add this model, "
109+
f"please use another model name"
110+
)
104111

105112
@classmethod
106113
def _get_model_description(cls, model_name: str) -> T:
@@ -116,10 +123,7 @@ def _get_model_description(cls, model_name: str) -> T:
116123
Returns:
117124
T: The model description.
118125
"""
119-
cache = cls.__dict__.get("_model_description_cache")
120-
if cache is None:
121-
cache = {model.model.lower(): model for model in cls._list_supported_models()}
122-
cls._model_description_cache = cache
126+
cache = cls._ensure_model_cache()
123127

124128
model_name_lower = model_name.lower()
125129
model = cache.get(model_name_lower)

0 commit comments

Comments
 (0)