Skip to content

perf(builder): cache prepared statements in technique-backfill chunk loops (#1952) - #2204

Merged
carlos-alm merged 1 commit into
mainfrom
perf/cache-prepared-statements-backfill-1952
Aug 2, 2026
Merged

carlos-alm merged 1 commit into
mainfrom
perf/cache-prepared-statements-backfill-1952

Conversation

@carlos-alm

Copy link
Copy Markdown
Contributor

Summary

Issue #1768/#1767 fixed the re-prepare-per-chunk anti-pattern in applyEdgeTechniquesAfterNativeInsert (stages/build-edges.ts) using a chunk-size-keyed WeakMap statement cache (getOrCreatePerDbChunkStmt in src/shared/chunked-stmt-cache.ts). That PR explicitly named two sibling functions with the identical pattern as out of scope, filed here per the one-PR-per-concern rule:

  • backfillIncrementalEdgeTechniques (incremental.ts) — same two-statement chunk-of-500 loop
  • backfillEdgeTechniquesAfterNativeOrchestrator (stages/native-orchestrator.ts) — same two-statement chunk loop
  • findOneHopReverseDepFiles (stages/native-orchestrator.ts) — a third chunked loop in the same file, a single re-prepared SELECT per chunk

This PR applies the same fix to all three, reusing the existing getOrCreatePerDbChunkStmt helper (not reinventing it) with a dedicated per-function WeakMap cache, mirroring the naming and structure of the already-merged techniqueBackfillStmtCache/confidenceFloorStmtCache pair in build-edges.ts.

Verification

  • npx tsc --noEmit: clean
  • npm run lint: clean
  • npm test: 260 files, 4197 passed, 30 skipped, 2 todo, 0 failed
  • Direct smoke test: full native build + touch-one-file incremental rebuild against this repo's own src/, confirmed zero calls edges with technique IS NULL and zero ts-native edges below the confidence floor after rebuild — same correctness as before, just fewer redundant db.prepare() calls per chunk

No behavior change — this is purely a caching refactor of internal chunk loops, not urgent per the issue but small and low-risk to land.

Closes #1952

…loops

backfillIncrementalEdgeTechniques (incremental.ts) and
backfillEdgeTechniquesAfterNativeOrchestrator + findOneHopReverseDepFiles
(native-orchestrator.ts) each re-prepared their chunk-loop statements on
every iteration, mirroring the anti-pattern already fixed in
applyEdgeTechniquesAfterNativeInsert (#1768). Reuses the same
getOrCreatePerDbChunkStmt chunk-size-keyed cache helper for all three,
scoped per db via a dedicated WeakMap cache per statement.

No behavior change — verified the technique/confidence-floor backfill
still lands correctly on a real incremental rebuild (zero NULL-technique
and zero below-floor ts-native calls edges after rebuild).

No README.md/CLAUDE.md/ROADMAP.md changes needed, docs check acknowledged.

Closes #1952

Impact: 3 functions changed, 7 affected
@greptile-apps

greptile-apps Bot commented Aug 2, 2026

Copy link
Copy Markdown
Contributor

Greptile Summary

This PR caches chunk-size-specific SQLite statements used by incremental and native-orchestrator technique backfills, avoiding repeated preparation while preserving existing SQL behavior.

  • Adds per-database caches for technique and confidence-floor UPDATE statements.
  • Caches the one-hop reverse-dependency SELECT by database and chunk size.
  • Retains exact placeholder counts and binding order for full and partial chunks.

Confidence Score: 5/5

The PR appears safe to merge, with the cached statements preserving the prior queries, parameter ordering, and per-database lifecycle.

Cache entries are isolated by database object and exact chunk size, while generated placeholders and runtime bindings remain aligned for both complete and partial chunks.

Important Files Changed

Filename Overview
src/domain/graph/builder/incremental.ts Replaces repeated preparation of two chunked backfill UPDATEs with correctly database- and chunk-size-scoped statement caches.
src/domain/graph/builder/stages/native-orchestrator.ts Applies equivalent caching to reverse-dependency lookup and native-orchestrator backfill statements without changing query scope or bindings.

Reviews (1): Last reviewed commit: "perf(builder): cache prepared statements..." | Re-trigger Greptile

@github-actions

github-actions Bot commented Aug 2, 2026

Copy link
Copy Markdown
Contributor

Codegraph Impact Analysis

3 functions changed → 7 callers affected across 4 files

  • backfillIncrementalEdgeTechniques in src/domain/graph/builder/incremental.ts:1201 (3 transitive callers)
  • findOneHopReverseDepFiles in src/domain/graph/builder/stages/native-orchestrator.ts:2077 (3 transitive callers)
  • backfillEdgeTechniquesAfterNativeOrchestrator in src/domain/graph/builder/stages/native-orchestrator.ts:2121 (3 transitive callers)

@carlos-alm
carlos-alm merged commit 51a98b9 into main Aug 2, 2026
27 checks passed
@carlos-alm
carlos-alm deleted the perf/cache-prepared-statements-backfill-1952 branch August 2, 2026 05:50
@github-actions github-actions Bot locked and limited conversation to collaborators Aug 2, 2026
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

Consider caching prepared statements in the other technique-backfill chunk loops (incremental.ts, native-orchestrator.ts)

1 participant