Skip to content

Commit b4f4ce2

Browse files
committed
fix: correct misleading comments and cache role UPDATE stmts (#606)
- Fix misleading "single nodeIdMap pass" comment in insertChildrenAndEdges (actually two passes: one before and one after batchInsertNodes) - Cache role UPDATE prepared statements per chunk size in classifyNodeRoles, consistent with WeakMap-based caching pattern used in helpers.ts Impact: 2 functions changed, 4 affected
1 parent 828157d commit b4f4ce2

2 files changed

Lines changed: 10 additions & 4 deletions

File tree

src/domain/graph/builder/stages/insert-nodes.ts

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -86,7 +86,7 @@ function insertDefinitionsAndExports(
8686
}
8787
}
8888

89-
// ── Phase 2+3: Insert children and containment edges (single nodeIdMap pass) ──
89+
// ── Phase 2+3: Insert children and containment edges (two nodeIdMap passes) ──
9090

9191
function insertChildrenAndEdges(
9292
db: BetterSqlite3.Database,
@@ -96,7 +96,7 @@ function insertChildrenAndEdges(
9696
const edgeRows: unknown[][] = [];
9797

9898
for (const [relPath, symbols] of allSymbols) {
99-
// Single bulkNodeIdsByFile call per file, shared across children + edges
99+
// First pass: collect file→def edges and child rows
100100
const nodeIdMap = new Map<string, number>();
101101
for (const row of bulkNodeIdsByFile(db, relPath)) {
102102
nodeIdMap.set(`${row.name}|${row.kind}|${row.line}`, row.id);

src/features/structure.ts

Lines changed: 8 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -485,16 +485,22 @@ export function classifyNodeRoles(db: BetterSqlite3Database): RoleSummary {
485485

486486
// Batch UPDATE: one statement per role instead of one per node
487487
const ROLE_CHUNK = 500;
488+
const roleStmtCache = new Map<number, SqliteStatement>();
488489
db.transaction(() => {
489490
db.prepare('UPDATE nodes SET role = NULL').run();
490491
for (const [role, ids] of idsByRole) {
491492
for (let i = 0; i < ids.length; i += ROLE_CHUNK) {
492493
const end = Math.min(i + ROLE_CHUNK, ids.length);
493494
const chunkSize = end - i;
494-
const placeholders = Array.from({ length: chunkSize }, () => '?').join(',');
495+
let stmt = roleStmtCache.get(chunkSize);
496+
if (!stmt) {
497+
const placeholders = Array.from({ length: chunkSize }, () => '?').join(',');
498+
stmt = db.prepare(`UPDATE nodes SET role = ? WHERE id IN (${placeholders})`);
499+
roleStmtCache.set(chunkSize, stmt);
500+
}
495501
const vals: unknown[] = [role];
496502
for (let j = i; j < end; j++) vals.push(ids[j]);
497-
db.prepare(`UPDATE nodes SET role = ? WHERE id IN (${placeholders})`).run(...vals);
503+
stmt.run(...vals);
498504
}
499505
}
500506
})();

0 commit comments

Comments
 (0)