Skip to content

Commit 4b197cd

Browse files
committed
fix: convert db.prepare() calls to StmtCache/cachedStmt pattern in exportsFileImpl (#558)
1 parent 5819775 commit 4b197cd

1 file changed

Lines changed: 19 additions & 5 deletions

File tree

src/domain/analysis/exports.ts

Lines changed: 19 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,7 @@ import {
66
findNodesByFile,
77
openReadonlyOrFail,
88
} from '../../db/index.js';
9+
import { cachedStmt } from '../../db/repository/cached-stmt.js';
910
import { loadConfig } from '../../infrastructure/config.js';
1011
import { debug } from '../../infrastructure/logger.js';
1112
import { isTestFile } from '../../infrastructure/test-filter.js';
@@ -15,11 +16,16 @@ import {
1516
extractSummary,
1617
} from '../../shared/file-utils.js';
1718
import { paginateResult } from '../../shared/paginate.js';
18-
import type { BetterSqlite3Database, NodeRow } from '../../types.js';
19+
import type { BetterSqlite3Database, NodeRow, StmtCache } from '../../types.js';
1920

2021
/** Cache the schema probe for the `exported` column per db handle. */
2122
const _hasExportedColCache: WeakMap<BetterSqlite3Database, boolean> = new WeakMap();
2223

24+
const _exportedNodesStmtCache: StmtCache<NodeRow> = new WeakMap();
25+
const _consumersStmtCache: StmtCache<{ name: string; file: string; line: number }> = new WeakMap();
26+
const _reexportsFromStmtCache: StmtCache<{ file: string }> = new WeakMap();
27+
const _reexportsToStmtCache: StmtCache<{ file: string }> = new WeakMap();
28+
2329
export function exportsData(
2430
file: string,
2531
customDbPath: string,
@@ -128,18 +134,26 @@ function exportsFileImpl(
128134
_hasExportedColCache.set(db, hasExportedCol);
129135
}
130136

131-
const exportedNodesStmt = db.prepare(
137+
const exportedNodesStmt = cachedStmt(
138+
_exportedNodesStmtCache,
139+
db,
132140
"SELECT * FROM nodes WHERE file = ? AND kind != 'file' AND exported = 1 ORDER BY line",
133141
);
134-
const consumersStmt = db.prepare(
142+
const consumersStmt = cachedStmt(
143+
_consumersStmtCache,
144+
db,
135145
`SELECT n.name, n.file, n.line FROM edges e JOIN nodes n ON e.source_id = n.id
136146
WHERE e.target_id = ? AND e.kind = 'calls'`,
137147
);
138-
const reexportsFromStmt = db.prepare(
148+
const reexportsFromStmt = cachedStmt(
149+
_reexportsFromStmtCache,
150+
db,
139151
`SELECT DISTINCT n.file FROM edges e JOIN nodes n ON e.source_id = n.id
140152
WHERE e.target_id = ? AND e.kind = 'reexports'`,
141153
);
142-
const reexportsToStmt = db.prepare(
154+
const reexportsToStmt = cachedStmt(
155+
_reexportsToStmtCache,
156+
db,
143157
`SELECT DISTINCT n.file FROM edges e JOIN nodes n ON e.target_id = n.id
144158
WHERE e.source_id = ? AND e.kind = 'reexports'`,
145159
);

0 commit comments

Comments
 (0)