From f2a94fa58a775e41f0957b0de028a9b761261c39 Mon Sep 17 00:00:00 2001 From: mgabrielramos <83740829+mgabrielramos@users.noreply.github.com> Date: Wed, 11 Feb 2026 22:34:14 +0000 Subject: [PATCH] =?UTF-8?q?=E2=9A=A1=20Optimize=20memory=20sync=20N+1=20qu?= =?UTF-8?q?eries?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- src/memory/manager.ts | 61 +++++++++++++++++++++++++++++++------------ 1 file changed, 44 insertions(+), 17 deletions(-) diff --git a/src/memory/manager.ts b/src/memory/manager.ts index 08b1925ea516..2368440185d9 100644 --- a/src/memory/manager.ts +++ b/src/memory/manager.ts @@ -1131,9 +1131,19 @@ export class MemoryIndexManager { const tasks = fileEntries.map((entry) => async () => { const record = this.db - .prepare(`SELECT hash FROM files WHERE path = ? AND source = ?`) - .get(entry.path, "memory") as { hash: string } | undefined; - if (!params.needsFullReindex && record?.hash === entry.hash) { + const fileHashes = new Map(); + if (!params.needsFullReindex) { + const rows = this.db + .prepare(`SELECT path, hash FROM files WHERE source = ?`) + .all("memory") as Array<{ path: string; hash: string }>; + for (const row of rows) { + fileHashes.set(row.path, row.hash); + } + } + + const tasks = fileEntries.map((entry) => async () => { + const existingHash = fileHashes.get(entry.path); + if (!params.needsFullReindex && existingHash === entry.hash) { if (params.progress) { params.progress.completed += 1; params.progress.report({ @@ -1184,16 +1194,6 @@ export class MemoryIndexManager { needsFullReindex: boolean; progress?: MemorySyncProgressState; }) { - const files = await this.listSessionFiles(); - const activePaths = new Set(files.map((file) => this.sessionPathForFile(file))); - const indexAll = params.needsFullReindex || this.sessionsDirtyFiles.size === 0; - log.debug("memory sync: indexing session files", { - files: files.length, - indexAll, - dirtyFiles: this.sessionsDirtyFiles.size, - batch: this.batch.enabled, - concurrency: this.getIndexConcurrency(), - }); if (params.progress) { params.progress.total += files.length; params.progress.report({ @@ -1203,6 +1203,16 @@ export class MemoryIndexManager { }); } + const fileHashes = new Map(); + if (!params.needsFullReindex) { + const rows = this.db + .prepare(`SELECT path, hash FROM files WHERE source = ?`) + .all("sessions") as Array<{ path: string; hash: string }>; + for (const row of rows) { + fileHashes.set(row.path, row.hash); + } + } + const tasks = files.map((absPath) => async () => { if (!indexAll && !this.sessionsDirtyFiles.has(absPath)) { if (params.progress) { @@ -1225,10 +1235,8 @@ export class MemoryIndexManager { } return; } - const record = this.db - .prepare(`SELECT hash FROM files WHERE path = ? AND source = ?`) - .get(entry.path, "sessions") as { hash: string } | undefined; - if (!params.needsFullReindex && record?.hash === entry.hash) { + const existingHash = fileHashes.get(entry.path); + if (!params.needsFullReindex && existingHash === entry.hash) { if (params.progress) { params.progress.completed += 1; params.progress.report({ @@ -1248,6 +1256,25 @@ export class MemoryIndexManager { total: params.progress.total, }); } + }); + params.progress.completed += 1; + params.progress.report({ + completed: params.progress.completed, + total: params.progress.total, + }); + } + this.resetSessionDelta(absPath, entry.size); + return; + } + await this.indexFile(entry, { source: "sessions", content: entry.content }); + this.resetSessionDelta(absPath, entry.size); + if (params.progress) { + params.progress.completed += 1; + params.progress.report({ + completed: params.progress.completed, + total: params.progress.total, + }); + } }); await this.runWithConcurrency(tasks, this.getIndexConcurrency());