mirror of
https://github.com/willmiao/ComfyUI-Lora-Manager.git
synced 2026-05-06 16:36:45 -03:00
perf(check-model-exists): eliminate SQLite connection-per-query overhead and skip redundant history checks
Root cause: 231 concurrent /check-model-exists requests on 175K-lora library caused ~9.4s wall clock time. The bottleneck was two-fold: 1. DownloadedVersionHistoryService opened a new sqlite3.connect() for every query under asyncio.Lock. With a large WAL from 175K entries, each connect() took ~8ms. Serialized by the lock across 231 requests, the 230th request waited ~1848ms just for lock acquisition. 2. check_model_exists always queried download history even when the model was found locally. The history result (hasBeenDownloaded / downloadedVersionIds) is only used by the UI when the model is NOT found locally; when found, the 'in library' indicator takes priority. Changes: - downloaded_version_history_service.py: added persistent _get_conn() that creates the SQLite connection once and reuses it across all queries - misc_handlers.py: early-return from check_model_exists when the model exists locally, bypassing the history service entirely (lock skipped) Expected: per-request wait time drops from ~1912ms to <3ms, wall clock from ~9.4s to <0.3s for the 175K-lora user's 231-card page.
This commit is contained in:
@@ -1791,29 +1791,33 @@ class ModelLibraryHandler:
|
||||
exists = True
|
||||
model_type = "embedding"
|
||||
|
||||
if exists:
|
||||
return web.json_response(
|
||||
{
|
||||
"success": True,
|
||||
"exists": True,
|
||||
"modelType": model_type,
|
||||
"hasBeenDownloaded": False,
|
||||
}
|
||||
)
|
||||
|
||||
history_service = await self._get_download_history_service()
|
||||
has_been_downloaded = False
|
||||
history_type = model_type
|
||||
if history_type:
|
||||
has_been_downloaded = await history_service.has_been_downloaded(
|
||||
history_type,
|
||||
history_type = None
|
||||
for candidate_type in ("lora", "checkpoint", "embedding"):
|
||||
if await history_service.has_been_downloaded(
|
||||
candidate_type,
|
||||
model_version_id,
|
||||
)
|
||||
else:
|
||||
for candidate_type in ("lora", "checkpoint", "embedding"):
|
||||
if await history_service.has_been_downloaded(
|
||||
candidate_type,
|
||||
model_version_id,
|
||||
):
|
||||
has_been_downloaded = True
|
||||
history_type = candidate_type
|
||||
break
|
||||
):
|
||||
has_been_downloaded = True
|
||||
history_type = candidate_type
|
||||
break
|
||||
|
||||
return web.json_response(
|
||||
{
|
||||
"success": True,
|
||||
"exists": exists,
|
||||
"modelType": model_type if exists else history_type,
|
||||
"exists": False,
|
||||
"modelType": history_type,
|
||||
"hasBeenDownloaded": has_been_downloaded,
|
||||
}
|
||||
)
|
||||
@@ -1833,40 +1837,46 @@ class ModelLibraryHandler:
|
||||
model_type = None
|
||||
versions = []
|
||||
downloaded_version_ids = []
|
||||
history_service = await self._get_download_history_service()
|
||||
if lora_versions:
|
||||
model_type = "lora"
|
||||
versions = self._with_downloaded_flag(lora_versions)
|
||||
downloaded_version_ids = await history_service.get_downloaded_version_ids(
|
||||
model_type,
|
||||
model_id,
|
||||
return web.json_response(
|
||||
{
|
||||
"success": True,
|
||||
"modelType": "lora",
|
||||
"versions": self._with_downloaded_flag(lora_versions),
|
||||
"downloadedVersionIds": [],
|
||||
}
|
||||
)
|
||||
elif checkpoint_versions:
|
||||
model_type = "checkpoint"
|
||||
versions = self._with_downloaded_flag(checkpoint_versions)
|
||||
downloaded_version_ids = await history_service.get_downloaded_version_ids(
|
||||
model_type,
|
||||
model_id,
|
||||
if checkpoint_versions:
|
||||
return web.json_response(
|
||||
{
|
||||
"success": True,
|
||||
"modelType": "checkpoint",
|
||||
"versions": self._with_downloaded_flag(checkpoint_versions),
|
||||
"downloadedVersionIds": [],
|
||||
}
|
||||
)
|
||||
elif embedding_versions:
|
||||
model_type = "embedding"
|
||||
versions = self._with_downloaded_flag(embedding_versions)
|
||||
downloaded_version_ids = await history_service.get_downloaded_version_ids(
|
||||
model_type,
|
||||
model_id,
|
||||
if embedding_versions:
|
||||
return web.json_response(
|
||||
{
|
||||
"success": True,
|
||||
"modelType": "embedding",
|
||||
"versions": self._with_downloaded_flag(embedding_versions),
|
||||
"downloadedVersionIds": [],
|
||||
}
|
||||
)
|
||||
else:
|
||||
for candidate_type in ("lora", "checkpoint", "embedding"):
|
||||
candidate_downloaded_version_ids = (
|
||||
await history_service.get_downloaded_version_ids(
|
||||
candidate_type,
|
||||
model_id,
|
||||
)
|
||||
|
||||
history_service = await self._get_download_history_service()
|
||||
for candidate_type in ("lora", "checkpoint", "embedding"):
|
||||
candidate_downloaded_version_ids = (
|
||||
await history_service.get_downloaded_version_ids(
|
||||
candidate_type,
|
||||
model_id,
|
||||
)
|
||||
if candidate_downloaded_version_ids:
|
||||
model_type = candidate_type
|
||||
downloaded_version_ids = candidate_downloaded_version_ids
|
||||
break
|
||||
)
|
||||
if candidate_downloaded_version_ids:
|
||||
model_type = candidate_type
|
||||
downloaded_version_ids = candidate_downloaded_version_ids
|
||||
break
|
||||
|
||||
return web.json_response(
|
||||
{
|
||||
|
||||
Reference in New Issue
Block a user