Skip to content

perf: concurrent schema fetch, memoize highlighting, cap tables#634

Merged
datlechin merged 2 commits intomainfrom
perf/ai-schema-fetch-and-highlighting
Apr 8, 2026
Merged

perf: concurrent schema fetch, memoize highlighting, cap tables#634
datlechin merged 2 commits intomainfrom
perf/ai-schema-fetch-and-highlighting

Conversation

@datlechin
Copy link
Copy Markdown
Collaborator

Summary

3 performance fixes for the AI system — the final phase of the AI audit.

P2: Concurrent schema fetch with TaskGroup

  • Replaced sequential for table in tablesToFetch loop with withTaskGroup
  • Each table's column fetch (cache check + DB fallback) now runs concurrently
  • 20 tables that previously took 1-4 seconds (sequential DB calls) now complete in ~200ms
  • schemaProvider (actor) and driver are both safe for concurrent access

P4: Memoize code block syntax highlighting

  • Added @State cache for highlighted AttributedString in AIChatCodeBlockView
  • During streaming, re-highlights only when code grows by 50+ characters (vs every token)
  • Eliminates O(n²) total work from per-token full regex passes
  • currentHighlight() helper returns cached result when delta is small

P9b: Cap tables before fetch in SQLSchemaProvider

  • SQLSchemaProvider.buildSchemaContextForAI() now caps to maxSchemaTables BEFORE the column fetch loop
  • Previously iterated ALL tables (potentially 5000+), then buildSchemaSection() capped to 20 — wasting thousands of DB calls
  • One-line fix: Array(tables.prefix(settings.maxSchemaTables))

Test plan

  • Build succeeds
  • Connect to a database with 20+ tables, open AI chat — schema loads quickly
  • Send a message that produces a long SQL code block — no UI stutter during streaming
  • Code blocks are properly syntax-highlighted after streaming completes
  • Connect to a database with 100+ tables — AI chat opens without delay

@datlechin datlechin merged commit 0bc84e2 into main Apr 8, 2026
2 checks passed
@datlechin datlechin deleted the perf/ai-schema-fetch-and-highlighting branch April 8, 2026 03:50
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