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 <noreply@anthropic.com>
This commit is contained in:
Classic298
2026-04-17 05:28:34 +02:00
committed by GitHub
parent 349ea4ea9e
commit 2dca850cee

View File

@@ -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