Skip to content

Commit 7d6d112

Browse files
committed
perf(sqlite): use existence probes for empty memory search
1 parent f6a0cdc commit 7d6d112

2 files changed

Lines changed: 33 additions & 19 deletions

File tree

‎src/memory/index.test.ts‎

Lines changed: 21 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -216,6 +216,7 @@ describe("memory index", () => {
216216
vectorEnabled?: boolean;
217217
cacheEnabled?: boolean;
218218
minScore?: number;
219+
onSearch?: boolean;
219220
hybrid?: { enabled: boolean; vectorWeight?: number; textWeight?: number };
220221
}): TestCfg {
221222
return {
@@ -229,7 +230,7 @@ describe("memory index", () => {
229230
store: { path: params.storePath, vector: { enabled: params.vectorEnabled ?? false } },
230231
// Perf: keep test indexes to a single chunk to reduce sqlite work.
231232
chunking: { tokens: 4000, overlap: 0 },
232-
sync: { watch: false, onSessionStart: false, onSearch: true },
233+
sync: { watch: false, onSessionStart: false, onSearch: params.onSearch ?? true },
233234
query: {
234235
minScore: params.minScore ?? 0,
235236
hybrid: params.hybrid ?? { enabled: false },
@@ -970,6 +971,25 @@ describe("memory index", () => {
970971
await manager.close?.();
971972
});
972973

974+
it("does not initialize the provider when searching an empty index", async () => {
975+
const cfg = createCfg({
976+
storePath: path.join(workspaceDir, `index-empty-${randomUUID()}.sqlite`),
977+
provider: "gemini",
978+
model: "gemini-embedding-2-preview",
979+
outputDimensionality: 1536,
980+
onSearch: false,
981+
});
982+
983+
const result = await getMemorySearchManager({ cfg, agentId: "main" });
984+
const manager = requireManager(result);
985+
986+
const results = await manager.search("hello");
987+
988+
expect(results).toEqual([]);
989+
expect(providerCalls).toEqual([]);
990+
await manager.close?.();
991+
});
992+
973993
it("reindexes when Gemini outputDimensionality changes", async () => {
974994
const base = createCfg({
975995
storePath: indexModelPath,

‎src/memory/manager.ts‎

Lines changed: 12 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -436,29 +436,23 @@ export class MemoryIndexManager extends MemoryManagerEmbeddingOps implements Mem
436436
}
437437

438438
private hasIndexedContent(): boolean {
439-
const chunks =
440-
(
441-
this.db.prepare(`SELECT COUNT(*) as c FROM chunks`).get() as
442-
| {
443-
c: number;
444-
}
445-
| undefined
446-
)?.c ?? 0;
447-
if (chunks > 0) {
439+
const chunkRow = this.db.prepare(`SELECT 1 as found FROM chunks LIMIT 1`).get() as
440+
| {
441+
found?: number;
442+
}
443+
| undefined;
444+
if (chunkRow?.found === 1) {
448445
return true;
449446
}
450447
if (!this.fts.enabled || !this.fts.available) {
451448
return false;
452449
}
453-
const ftsRows =
454-
(
455-
this.db.prepare(`SELECT COUNT(*) as c FROM ${FTS_TABLE}`).get() as
456-
| {
457-
c: number;
458-
}
459-
| undefined
460-
)?.c ?? 0;
461-
return ftsRows > 0;
450+
const ftsRow = this.db.prepare(`SELECT 1 as found FROM ${FTS_TABLE} LIMIT 1`).get() as
451+
| {
452+
found?: number;
453+
}
454+
| undefined;
455+
return ftsRow?.found === 1;
462456
}
463457

464458
private async searchVector(

0 commit comments

Comments
 (0)