Skip to content

Reuse prepared queries for hot lookups and pruning - #5115

Merged
ymichael merged 1 commit into
mainfrom
bb/perf-pr-cache-prepared-sqlite-statements-thr_9xnzyfyt2j
Oct 7, 2026
Merged

ymichael merged 1 commit into
mainfrom
bb/perf-pr-cache-prepared-sqlite-statements-thr_9xnzyfyt2j

Conversation

@ymichael

@ymichael ymichael commented Oct 7, 2026

Copy link
Copy Markdown
Collaborator

Human comments

What was wrong

Hot entity lookups and event-pruning support reads rebuilt Drizzle SQL and prepared the same SQLite statements on every call, spending server CPU on repeated compilation.

What changed

Reuse explicit parameterized Drizzle prepared queries for host, thread, project and environment lookups and five pruning support queries. A connection-owned registry shares these queries with transaction handles and clears them on close. Reads continue to observe writes and rollbacks. Uses the existing prepared-query API without a general SQLite statement wrapper or automatic statement LRU.

How you verified

  • pnpm exec turbo run test --filter=@bb/db -- test/prepared-queries.test.ts test/query-plans.test.ts test/data/thread-pruning.test.ts test/connection.test.ts: 84 tests passed.
  • pnpm exec turbo run typecheck --filter=@bb/db: passed.
  • Regression guards verify warm lookup SQL is compiled once, transaction reuse, connection independence, write visibility and rollback. Existing query-plan tests verify indexed, bounded pruning support probes.
  • Seven-sample snapshot benchmarks: entity lookup CPU fell 86–91%; pruning sweep CPU fell from 2,904 to 1,721 ms (41%), and live pruning from 128 to 112 ms (13%). Lookup results and pruning membership hashes, removal counts and advance counts matched. Scripts and raw results are retained outside the checkout.
  • Started the optimized worktree server; server and daemon health passed, the host connected, and repeated project reads observed a rename after warming the query.

AGENT GENERATED

@ymichael
ymichael merged commit 86c09ae into main Oct 7, 2026
30 checks passed
@ymichael
ymichael deleted the bb/perf-pr-cache-prepared-sqlite-statements-thr_9xnzyfyt2j branch October 7, 2026 18:54
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant