Skip to content

perf(server): High-performance backend optimizations#7

Merged
arvarik merged 2 commits into
mainfrom
perf/backend-optimizations
May 21, 2026
Merged

perf(server): High-performance backend optimizations#7
arvarik merged 2 commits into
mainfrom
perf/backend-optimizations

Conversation

@arvarik

@arvarik arvarik commented May 21, 2026

Copy link
Copy Markdown
Owner

Summary of Accomplishments

This Pull Request implements the approved backend architectural optimizations for contrack to eliminate critical N+1 database bottlenecks and enhance client request resiliency.

1. Connection Abort Propagation (Security & Cost Control)

  • Bound an AbortController to Express req.on("close") in /api/search/semantic.
  • Propagated the signal down to the AI reranking service and adapter facades, allowing immediate socket tear-down during early client exits and saving redundant LLM token spending.

2. O(1) Batch Normalization (Dedupe Performance)

  • Optimized dedupe scanning in normalizeContacts to batch-load tags and interests at start, mapping them via in-memory O(1) lookups.
  • Latency reduced from ~3,000ms down to ~20ms for 1,082 records (a ~150x speedup).

3. High-Performance Contact Bulk Hydration

  • Rewrote hydrateMany in contactRepository.ts to leverage query-parameter safe chunked SQL queries (chunk size of 500) and O(N) map-joins.
  • Keeps individual microsecond cached statements for collections <= 3 contacts to maintain maximum efficiency.
  • Strips contactId fields from child records to guarantee 100% downstream API payload shape parity.
  • Yields a ~6x hydration speedup (from 68ms down to 11ms for 500 records).

4. Validation

  • Run integration benchmark validating perfect structural equivalence between the sequential and bulk hydration pipelines.
  • Fully verified via test harness: 180/180 tests passing successfully.

arvarik added 2 commits May 20, 2026 17:02
…ization, and contact hydration

- Propagated AbortSignal from Express `/api/search/semantic` connection close to AI service & provider adapters to prevent token and compute leaks.
- Replaced loop-nested N+1 queries in `normalizeContacts` with pre-fetched bulk tags/interests lookup Maps, reducing latency by ~150x.
- Refactored `hydrateMany` to chunk queries at 500 parameters (respecting SQLite limit) and use O(N) map-joins with exact single-record schema parity.
- Added dynamic microsecond-range sequential hydration fallback for collections <= 3 contacts.
- Verified zero regression with full Vitest integration suite (180 tests passing).
@arvarik arvarik merged commit b7bce93 into main May 21, 2026
2 checks passed
@arvarik arvarik deleted the perf/backend-optimizations branch May 21, 2026 03:14
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