perf: Optimize SQL queries and reduce allocations in hot paths

Change detection queries (embedding/change_detector.rs):
- Replace triple-EXISTS subquery pattern with LEFT JOIN + NULL check
- SQLite now scans embedding_metadata once instead of three times
- Semantically identical: returns docs needing embedding when no
  embedding exists, hash changed, or config mismatch

Count queries (cli/commands/count.rs):
- Consolidate 3 separate COUNT queries for issues into single query
  using conditional aggregation (CASE WHEN state = 'x' THEN 1)
- Same optimization for MRs: 5 queries reduced to 1

Search filter queries (search/filters.rs):
- Replace N separate EXISTS clauses for label filtering with single
  IN() clause with COUNT/GROUP BY HAVING pattern
- For multi-label AND queries, this reduces N subqueries to 1

FTS tokenization (search/fts.rs):
- Replace collect-into-Vec-then-join pattern with direct String building
- Pre-allocate capacity hint for result string

Discussion truncation (documents/truncation.rs):
- Calculate total length without allocating concatenated string first
- Only allocate full string when we know it fits within limit

Embedding pipeline (embedding/pipeline.rs):
- Add Vec::with_capacity hints for chunk work and cleared_docs hashset
- Reduces reallocations during embedding batch processing

Backoff calculation (core/backoff.rs):
- Replace unchecked addition with saturating_add to prevent overflow
- Add test case verifying overflow protection

Co-Authored-By: Claude Opus 4.5 <noreply@anthropic.com>
This commit is contained in:
Taylor Eernisse
2026-02-05 11:21:28 -05:00
parent 9c04b7fb1b
commit 72f1cafdcf
7 changed files with 96 additions and 101 deletions

View File

@@ -41,18 +41,15 @@ pub fn run_count(config: &Config, entity: &str, type_filter: Option<&str>) -> Re
}
fn count_issues(conn: &Connection) -> Result<CountResult> {
let count: i64 = conn.query_row("SELECT COUNT(*) FROM issues", [], |row| row.get(0))?;
let opened: i64 = conn.query_row(
"SELECT COUNT(*) FROM issues WHERE state = 'opened'",
// Single query with conditional aggregation instead of 3 separate queries
let (count, opened, closed): (i64, i64, i64) = conn.query_row(
"SELECT
COUNT(*),
COALESCE(SUM(CASE WHEN state = 'opened' THEN 1 ELSE 0 END), 0),
COALESCE(SUM(CASE WHEN state = 'closed' THEN 1 ELSE 0 END), 0)
FROM issues",
[],
|row| row.get(0),
)?;
let closed: i64 = conn.query_row(
"SELECT COUNT(*) FROM issues WHERE state = 'closed'",
[],
|row| row.get(0),
|row| Ok((row.get(0)?, row.get(1)?, row.get(2)?)),
)?;
Ok(CountResult {
@@ -69,30 +66,25 @@ fn count_issues(conn: &Connection) -> Result<CountResult> {
}
fn count_mrs(conn: &Connection) -> Result<CountResult> {
let count: i64 = conn.query_row("SELECT COUNT(*) FROM merge_requests", [], |row| row.get(0))?;
let opened: i64 = conn.query_row(
"SELECT COUNT(*) FROM merge_requests WHERE state = 'opened'",
// Single query with conditional aggregation instead of 5 separate queries
let (count, opened, merged, closed, locked): (i64, i64, i64, i64, i64) = conn.query_row(
"SELECT
COUNT(*),
COALESCE(SUM(CASE WHEN state = 'opened' THEN 1 ELSE 0 END), 0),
COALESCE(SUM(CASE WHEN state = 'merged' THEN 1 ELSE 0 END), 0),
COALESCE(SUM(CASE WHEN state = 'closed' THEN 1 ELSE 0 END), 0),
COALESCE(SUM(CASE WHEN state = 'locked' THEN 1 ELSE 0 END), 0)
FROM merge_requests",
[],
|row| row.get(0),
)?;
let merged: i64 = conn.query_row(
"SELECT COUNT(*) FROM merge_requests WHERE state = 'merged'",
[],
|row| row.get(0),
)?;
let closed: i64 = conn.query_row(
"SELECT COUNT(*) FROM merge_requests WHERE state = 'closed'",
[],
|row| row.get(0),
)?;
let locked: i64 = conn.query_row(
"SELECT COUNT(*) FROM merge_requests WHERE state = 'locked'",
[],
|row| row.get(0),
|row| {
Ok((
row.get(0)?,
row.get(1)?,
row.get(2)?,
row.get(3)?,
row.get(4)?,
))
},
)?;
Ok(CountResult {