open-webui/backend/open_webui
Classic298 243a39dc9d
perf: read the model pool with one HGETALL instead of one HGET per model (#27821)
`request.app.state.MODELS` is a `RedisDict` when Redis is configured. Unpacking it with `{**pool}` makes Python call `keys()` and then `__getitem__` once per key, which is one HKEYS plus one HGET per model, issued sequentially through a synchronous client. At 200 models that is 201 blocking Redis round trips per call.

`RedisDict.items()` is a single HGETALL, so `dict(pool.items())` fetches the same data in one round trip. `utils/chat.py:184` already does exactly this and carries a comment explaining why; these ten call sites were missed.

They are on the direct-connection branch of the task endpoints (title, tags, follow-up, autocomplete, query generation and the rest), of `chat_completed`, and of context compaction, so they run for background tasks fired on ordinary chat turns.

Behaviour is unchanged. The merged mapping is identical, the explicitly added direct model still overrides any pool entry with the same id, and when Redis is not configured the pool is a plain dict where `dict(d.items())` and `{**d}` are equivalent.

It also closes a race. `RedisDict.set` writes with HSET and then HDELs the stale keys, so a key returned by HKEYS could be deleted before its HGET arrived, raising `KeyError` out of the dict literal and failing the request mid model refresh. The old path could likewise observe a mix of pre- and post-refresh entries. HGETALL is atomic, so the caller now always sees one coherent snapshot.
2026-07-31 17:25:53 -04:00
..
data refac: mv backend files to /open_webui dir 2024-09-04 16:54:48 +02:00
internal perf: cut per-request database session overhead (#27385) 2026-07-23 17:49:48 -05:00
migrations chore: format 2026-07-27 04:46:45 -04:00
models chore: remove dead json imports (#27815) 2026-07-31 17:25:40 -04:00
retrieval chore: remove dead json imports (#27815) 2026-07-31 17:25:40 -04:00
routers perf: read the model pool with one HGETALL instead of one HGET per model (#27821) 2026-07-31 17:25:53 -04:00
socket perf: optional orjson JSON codec behind ENABLE_ORJSON (#27583) 2026-07-27 03:45:37 -04:00
static refac 2026-07-27 19:39:36 -04:00
storage refac: modernize type annotations (PEP 604 / PEP 585) 2026-05-12 17:10:15 +09:00
tools chore: remove dead json imports (#27815) 2026-07-31 17:25:40 -04:00
utils perf: read the model pool with one HGETALL instead of one HGET per model (#27821) 2026-07-31 17:25:53 -04:00
__init__.py refac 2026-07-27 19:39:36 -04:00
alembic.ini fix: Alembic CLI commands from failing 2025-08-15 04:17:47 -04:00
config.py refac 2026-07-27 19:39:36 -04:00
constants.py refac 2026-07-27 19:39:36 -04:00
env.py refac 2026-07-27 19:39:36 -04:00
events.py refac 2026-07-27 19:39:36 -04:00
functions.py refac 2026-07-23 03:39:56 -04:00
main.py refac 2026-07-27 19:39:36 -04:00
tasks.py refac 2026-07-26 22:45:11 -04:00