perf: knowledge base sync with many folders no longer times out (#35630)

Before syncing, the sync client asks the server which files and folders changed. To answer, the server worked out each folder's full path by searching the whole folder list again for every parent folder, so the time grew with the square of the folder count. With tens of thousands of folders that took many minutes, blocked the server meanwhile and made the sync time out. The server now looks up each parent directly, so the folder step takes a fraction of a second.

<table>
<tr><th>Folders in the knowledge base (about 5 levels deep)</th><th>Before</th><th>After</th></tr>
<tr><td>5,000</td><td>3.1 s</td><td>5 ms</td></tr>
<tr><td>10,000</td><td>19.8 s</td><td>10 ms</td></tr>
<tr><td>20,000</td><td>219 s</td><td>43 ms</td></tr>
<tr><td>64,000</td><td>stopped after 25 min</td><td>151 ms</td></tr>
</table>
This commit is contained in:
Classic298 authored and GitHub committed 2026-12-05 08:04:01 +04:00
1 parent 1e926668bc
commit 024181dd2d
1 file changed
+2 -1
+2 -1
View File
@@ -1976,13 +1976,14 @@ async def sync_knowledge_diff(
existing_directories = await Knowledges.get_all_directories(id, db=db)
# Build directory path lookups
directory_by_id = {directory.id: directory for directory in existing_directories}
directory_path_by_id: dict[str, str] = {}
directory_id_by_path: dict[str, str] = {}
for directory in existing_directories:
segments = [directory.name]
parent_id = directory.parent_id
while parent_id:
parent = next((d for d in existing_directories if d.id == parent_id), None)
parent = directory_by_id.get(parent_id)
if not parent:
break
segments.insert(0, parent.name)