terraphim / terraphim/terraphim-ai
arch: Apply faer-style Rust optimisation patterns to core search pipeline
Dieses Issue hat noch niemand übernommen.
- Vorherrschende Sprache
- Rust
- Sterne
- 62
- Forks
- 5
- Ø Merge
- 2 Std. 27 Min.
- Gemergte PRs (30 T.)
- 1
Beschreibung
Architecture Optimisation: Apply faer-style Rust Patterns
Context
Evaluation of faer (pure Rust linear algebra library) identified several advanced Rust optimisation patterns that are directly applicable to terraphim-ai's core search pipeline. These patterns complement the existing performance epic (#193) and automata refactor (#201) with concrete, proven techniques.
KB reference: cto-executive-system/knowledge/faer-rust-linear-algebra.md
Patterns to Evaluate and Apply
1. Move+Reborrow for Mutable Views (MatMut pattern)
faer's MatMut uses move semantics + reborrow instead of Copy to prevent aliased mutable access at compile time. This pattern could improve:
AutomataPathand mutable graph traversal interraphim_automata- Mutable search context objects passed through the pipeline
- Any
&mutaliasing that currently relies on runtime checks (RefCell, Mutex)
Benefit: Compile-time exclusive access guarantees without runtime overhead.
2. Runtime SIMD Dispatch
faer uses std::arch for runtime CPU feature detection (AVX-512/AVX2/SSE) with automatic dispatch to optimal codepaths. Applicable to:
- Aho-Corasick pattern matching hot path (currently delegated to
aho-corasickcrate, but custom SIMD could help with pre/post processing) - Hash computation for knowledge graph lookups
- String comparison in thesaurus matching
Benefit: 2-4x throughput on SIMD-capable hardware without losing portability.
3. Zero-Copy View Types
faer's MatRef/MatMut provide zero-copy slicing with lifetime-bound subviews. Applicable to:
- Search result slices passed between pipeline stages
- Knowledge graph node/edge views (avoid cloning during traversal)
- Automata state views during matching (connects to #201 cache efficiency goal)
Benefit: Eliminate allocations in hot paths identified in #196 and #382.
4. Trait-Based Generic Configuration (Auto trait)
faer's Auto trait provides algorithm-specific hyperparameters tuned per scalar type. Applicable to:
- Search scoring functions that currently use runtime type checks or enum dispatch
- Automata builder configuration (different strategies for different corpus sizes)
perf-warnstyle compile-time warnings when suboptimal configurations are used
Benefit: Zero-cost abstractions for configurable behaviour.
Relationship to Existing Issues
| Existing Issue | How This Connects |
|---|---|
| #193 (Performance Epic) | Adds concrete Rust patterns to the roadmap |
| #201 (Automata Refactor) | Move+reborrow and SIMD patterns directly applicable |
| #196 (Excessive Cloning) | Zero-copy views are the solution pattern |
| #382 (HTTP client allocations) | Zero-copy patterns reduce allocation pressure |
| #200 (Search Relevance) | Trait-based config for scoring function dispatch |
Implementation Approach
- Audit: Profile current hot paths with
cargo flamegraphto identify where these patterns give most ROI - Prototype: Apply move+reborrow pattern to one mutable context type, measure impact
- Evaluate SIMD: Check if
aho-corasickcrate's SIMD is sufficient or if custom dispatch adds value - Zero-copy views: Introduce
SearchResultRef/SearchResultMutview types for pipeline stages
Not In Scope
- Numerical linear algebra (faer's core domain) -- not relevant to terraphim
- Sparse matrix operations
- Rayon parallelism (already evaluated separately)
References
- faer docs: https://docs.rs/faer/latest/faer/
- faer repo: https://github.com/sarah-ek/faer-rs
- Rust Performance Book: https://nnethercote.github.io/perf-book/
Beitragsleitfaden
Erste Schritte
- Lies das ganze Issue und danach den Beitragsleitfaden des Projekts.
- Schreib ins Issue, dass du es übernimmst — das erspart doppelte Arbeit.
- Forke das Repository und arbeite in einem Branch.
- Öffne einen Pull Request, der die Issue-Nummer nennt.
Rechercherichtung
Start by profiling the core search pipeline with cargo flamegraph, then compare the hot paths with the move+reborrow, SIMD dispatch, zero-copy view, and trait-based configuration patterns described here. Review the related issues #193, #201, #196, #382, and #200; completion would require a scoped prototype and measured evidence showing which pattern provides useful benefit.
Vom Indexierungsmodell aus dem Issue-Text verfasst.
Bewertung
- Tech-Stack
- rust
- Bereich
- backend, performance
- Issue-Typ
- Refactoring
- Schwierigkeit
- 5/5
- Geschätzter Aufwand
- Über eine Woche
- Aktivitätsstatus
- Veraltet
- Klarheit
- Muss geklärt werden
- Anfängerfreundlichkeit
- 20/100