# Swarm Performance

> SIMD optimization, connection pooling, batch APIs, and caching. Use when improving throughput or reducing latency.

- Skill: `d-o-hub/swarm-performance` (Agent Skill)
- Install (CLI): `npx skillmds@latest add d-o-hub/swarm-performance`
- Raw SKILL.md: https://api.skillmd.com/api/skills/d-o-hub/swarm-performance/raw
- Safety review: pending
- Works with: Claude Code, Claude.ai, OpenAI Codex
- Category: Coding & Dev Tools
- Author: d-o-hub (https://skillmd.com/u/d-o-hub)
- Updated: 2026-09-17
- Page: https://skillmd.com/skills/d-o-hub/swarm-performance

---


# Swarm: Performance

## Workflow

1. Profile current performance with `cargo bench --bench benchmark`
2. Identify hot path from flamegraph or benchmark results
3. Implement optimization behind feature flag if experimental
4. Benchmark before/after with criterion baseline
5. Ensure SIMD has scalar fallback for non-SIMD targets
6. Run all gates before claiming improvement

## SIMD Implementation

```rust
// 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

```rust
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 `Vec` materializations; 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:

```bash
cargo test --test <test_name>
```

## LOC Constraint

All files must remain ≤ 500 lines. Refactor to new modules if needed.

