From 2dca850ceed3ef9171c4ed37abd38384927f7c8d Mon Sep 17 00:00:00 2001 From: Classic298 <27028174+Classic298@users.noreply.github.com> Date: Fri, 17 Apr 2026 05:28:34 +0200 Subject: [PATCH] perf: use set for O(1) lookup in insert_chat_files dedupe (#23800) Convert chat_message_file_ids from list to set so the membership test in the comprehension is O(1) instead of O(m), turning the dedupe from O(n*m) into O(n+m). Also replace the redundant set([...]) with a set comprehension. https://claude.ai/code/session_01Le3NnqNhcgaJvFrDZGqmwe Co-authored-by: Claude --- backend/open_webui/models/chats.py | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-) diff --git a/backend/open_webui/models/chats.py b/backend/open_webui/models/chats.py index 0e1c6bef9d..7583f93429 100644 --- a/backend/open_webui/models/chats.py +++ b/backend/open_webui/models/chats.py @@ -1536,11 +1536,11 @@ class ChatTable: if not file_ids: return None - chat_message_file_ids = [ + chat_message_file_ids = { item.id for item in await self.get_chat_files_by_chat_id_and_message_id(chat_id, message_id, db=db) - ] + } # Remove duplicates and existing file_ids - file_ids = list(set([file_id for file_id in file_ids if file_id and file_id not in chat_message_file_ids])) + file_ids = list({file_id for file_id in file_ids if file_id and file_id not in chat_message_file_ids}) if not file_ids: return None