Swarm: Performance
Workflow
- Profile current performance with
cargo bench --bench benchmark - Identify hot path from flamegraph or benchmark results
- Implement optimization behind feature flag if experimental
- Benchmark before/after with criterion baseline
- Ensure SIMD has scalar fallback for non-SIMD targets
- Run all gates before claiming improvement
SIMD Implementation
// Note: std::simd requires nightly Rust and #![feature(portable_simd)]
#[cfg(all(feature = "simd", nightly))]
use std::simd::u128x2;
pub fn cosine_similarity_simd(&self, other: &Self) -> f32 {
// For stable Rust, use platform-specific intrinsics (AVX2/NEON)
// as seen in src/hyperdim_simd.rs or src/bundle_simd.rs.
}
Connection Pooling
Use deadpool for async connection pooling, gated for remote Turso only.
Keep per-operation model for local SQLite.
Batch API Pattern
pub async fn inject_concepts(
&self,
concepts: &[(String, HVec10240)]
) -> Result<()> {
// Validate all inputs first
// Batch insert to singularity
// Batch save to persistence
// Single transaction for DB
}
Caching Pattern
- Prefer cached values stored as
Arc<[T]>so cache hits are cheap (Arc::clone). - Avoid keying caches via temporary
Vecmaterializations; hash fixed-size words/arrays directly.
Performance Targets
- Batch similarity: 10k ops/ms
- Connection pool: <1ms acquire time
- Cache hit rate: >80% for repeated access patterns
- Reservoir step: maintain <100μs @ 50k
Test Files
Run performance tests:
cargo test --test <test_name>
LOC Constraint
All files must remain ≤ 500 lines. Refactor to new modules if needed.