# ryancodrai/turbovec architecture

> Architecture diagram of the GitHub repository ryancodrai/turbovec, generated by GitDiagram from its file tree, README and a sample of source files (last updated 2026-05-27). It is an AI-made overview: check details against the source.

- Interactive diagram: https://gitdiagram.com/ryancodrai/turbovec
- Repository: https://github.com/ryancodrai/turbovec

## Overview

1. **Repository shape**
   - This is a **Rust-first vector search engine** with a **Python extension package** and a separate **benchmark/evaluation harness**.
   - The architecture is centered on a core Rust crate (`turbovec`) that is reused by the Python wrapper crate (`turbovec-python`).

2. **Core engine boundary**
   - `turbovec/src` is the main subsystem: it owns vector encoding, search, packing, rotation, persistence, and error handling.
   - The public API exposed from `turbovec/src/lib.rs` appears to be built around two index types: **`TurboQuantIndex`** for dense vector indexing and **`IdMapIndex`** for stable external IDs and deletes.

3. **Ingest / encode pipeline**
   - Raw vectors are ingested into the index, then transformed into a compressed representation.
   - The pipeline described in the README is: **normalize → apply a shared random rotation → scalar-quantize each coordinate → bit-pack the codes → store a per-vector length renormalization scalar**.
   - That last scalar is a key architectural detail: it is computed at ingest time and later used during scoring to correct bias without extra query-time storage overhead.

4. **Search pipeline**
   - Queries follow a mirrored path: the query is rotated into the same quantized space, then scored directly against packed codes.
   - Search avoids decompression of the corpus; the hot path is a SIMD scoring kernel that computes approximate inner products from the packed representation.
   - Results are produced through a heap/top-k selection step, returning scores plus either internal slots or external IDs depending on the index type.

5. **Performance-critical implementation**
   - The core search kernels are architecture-specific: **NEON on ARM**, **AVX-512BW on modern x86**, with an **AVX2 fallback**.
   - This is not just a portability detail; it defines the runtime boundary. The crate is compiled with an x86-64-v3 baseline, and AVX-512 is enabled only when detected at runtime.

6. **Filtering and hybrid retrieval**
   - `search()` supports **allowlists / slot bitmasks** so filtering happens inside the SIMD search path, not as a post-filter.
   - Architecturally, this means the vector index can serve as a **second-stage reranker** after an external system narrows candidates by SQL, ACL, tenant, or time window.
   - The design avoids over-fetching and preserves top-k recall within the allowed subset.

7. **Stable IDs and deletion semantics**
   - `IdMapIndex` adds an ID mapping layer on top of the compressed vector storage.
   - Its role is to decouple internal storage slots from user-facing `u64` IDs, supporting **stable lookup, O(1)-style remove semantics, and persistence with external IDs preserved**.
   - This is the main boundary for applications that need document IDs rather than dense slot indices.

8. **Persistence / I/O**
   - The `io` and `dump_state` example indicate that index state can be written to and loaded from disk as a standalone artifact.
   - The README names two persisted formats: one for the plain vector index and one for the ID-mapped variant, which means serialization is part of the core architecture, not an afterthought.

9. **Python distribution layer**
   - `turbovec-python` is a separate crate/package that builds the Python extension module and ships Python-facing wrappers under `python/turbovec`.
   - It exposes the same conceptual split as the Rust API: basic vector index, ID-mapped index, plus integration adapters for **LangChain, LlamaIndex, Haystack, and Agno**.
   - Those adapters are thin compatibility boundaries that let turbovec replace in-memory vector stores in existing agent/RAG frameworks.

10. **Testing and correctness boundary**
   - The Rust tests cover encoding, rotation, codebook behavior, concurrent search, filtering, ID mapping, persistence/versioning, and kernel correctness.
   - The Python tests validate the binding layer and framework integrations, so the repo has separate correctness surfaces for core algorithms and external API compatibility.

11. **Benchmarking and evaluation subsystem**
   - `benchmarks/` is a self-contained measurement harness with scripts for recall, compression, and speed, plus archived JSON outputs and chart generation.
   - It compares turbovec across dimensions, bit widths, and CPU families, and also contains proof-of-concept comparison code against baseline approaches.
   - This subsystem is important architecturally because it validates both the algorithmic claims and the hardware-specific kernel behavior.

12. **Tooling and release flow**
   - The repo is managed as a Rust workspace with Python packaging via `maturin`, plus GitHub Actions for releasing both **crates.io** and **PyPI** artifacts.
   - That means the deliverable boundary is dual: a native Rust library for systems users and a Python package for application-level integration.

13. **What to draw in an architecture diagram**
   - Show three main zones: **core Rust indexing engine**, **Python binding/integration layer**, and **benchmark/documentation tooling**.
   - Inside the core engine, depict the flow from **vector ingest → quantized storage → SIMD search → top-k output**, with side branches for **ID mapping**, **filtering**, and **persistence**.
   - Annotate the search kernel with its **CPU-specific SIMD implementations** and the Python layer as a wrapper around the same Rust core rather than a separate implementation.

Main entry points: [`turbovec/src/lib.rs`](https://github.com/ryancodrai/turbovec/blob/main/turbovec/src/lib.rs) (turbovec), [`turbovec-python/python/turbovec/__init__.py`](https://github.com/ryancodrai/turbovec/blob/main/turbovec-python/python/turbovec/__init__.py) (Python API), [`turbovec/src/search.rs`](https://github.com/ryancodrai/turbovec/blob/main/turbovec/src/search.rs) (Search), [`turbovec/src/encode.rs`](https://github.com/ryancodrai/turbovec/blob/main/turbovec/src/encode.rs) (Encode)

## Diagram (Mermaid)

```mermaid
flowchart TD

subgraph group_core["Core Rust engine"]
  node_lib["turbovec<br/>Rust crate<br/>[lib.rs]"]
  node_encode["Encode<br/>ingest pipeline<br/>[encode.rs]"]
  node_rotation["Rotation<br/>shared transform<br/>[rotation.rs]"]
  node_codebook["Codebook<br/>quantization<br/>[codebook.rs]"]
  node_pack["Packing<br/>[pack.rs]"]
  node_search["Search<br/>query engine<br/>[search.rs]"]
  node_idmap["Id map<br/>ID layer<br/>[id_map.rs]"]
  node_io["Persistence<br/>serialization<br/>[io.rs]"]
  node_error["Errors<br/>error types<br/>[error.rs]"]
  node_kernels{{"SIMD kernels<br/>native kernels"}}
end

subgraph group_python["Python package"]
  node_pyext["Python ext<br/>PyO3 module<br/>[lib.rs]"]
  node_pyapi["Python API<br/>[__init__.py]"]
  node_langchain["LangChain<br/>adapter<br/>[langchain.py]"]
  node_llama["LlamaIndex<br/>adapter<br/>[llama_index.py]"]
  node_haystack["Haystack<br/>adapter<br/>[haystack.py]"]
  node_agno["Agno<br/>adapter<br/>[agno.py]"]
end

subgraph group_bench["Benchmarks"]
  node_bench_suite["Bench suite<br/>evaluation harness"]
  node_bench_results["Results<br/>outputs"]
  node_bench_poc["PoC compare<br/>baseline analysis"]
end

node_lib -->|"uses"| node_encode
node_encode -->|"applies"| node_rotation
node_encode -->|"quantizes"| node_codebook
node_codebook -->|"packs"| node_pack
node_lib -->|"exposes"| node_search
node_search -->|"mirrors"| node_rotation
node_search -->|"scans"| node_pack
node_search -->|"dispatches"| node_kernels
node_lib -->|"wraps"| node_idmap
node_idmap -->|"returns ids"| node_search
node_idmap -->|"persists"| node_io
node_lib -->|"serializes"| node_io
node_lib -->|"reports"| node_error
node_pyext -->|"binds"| node_lib
node_pyapi -->|"imports"| node_pyext
node_pyapi -->|"adapts"| node_langchain
node_pyapi -->|"adapts"| node_llama
node_pyapi -->|"adapts"| node_haystack
node_pyapi -->|"adapts"| node_agno
node_bench_suite -->|"measures"| node_lib
node_bench_suite -->|"writes"| node_bench_results
node_bench_poc -->|"compares"| node_lib

click node_lib "https://github.com/ryancodrai/turbovec/blob/main/turbovec/src/lib.rs"
click node_encode "https://github.com/ryancodrai/turbovec/blob/main/turbovec/src/encode.rs"
click node_rotation "https://github.com/ryancodrai/turbovec/blob/main/turbovec/src/rotation.rs"
click node_codebook "https://github.com/ryancodrai/turbovec/blob/main/turbovec/src/codebook.rs"
click node_pack "https://github.com/ryancodrai/turbovec/blob/main/turbovec/src/pack.rs"
click node_search "https://github.com/ryancodrai/turbovec/blob/main/turbovec/src/search.rs"
click node_idmap "https://github.com/ryancodrai/turbovec/blob/main/turbovec/src/id_map.rs"
click node_io "https://github.com/ryancodrai/turbovec/blob/main/turbovec/src/io.rs"
click node_error "https://github.com/ryancodrai/turbovec/blob/main/turbovec/src/error.rs"
click node_pyext "https://github.com/ryancodrai/turbovec/blob/main/turbovec-python/src/lib.rs"
click node_pyapi "https://github.com/ryancodrai/turbovec/blob/main/turbovec-python/python/turbovec/__init__.py"
click node_langchain "https://github.com/ryancodrai/turbovec/blob/main/turbovec-python/python/turbovec/langchain.py"
click node_llama "https://github.com/ryancodrai/turbovec/blob/main/turbovec-python/python/turbovec/llama_index.py"
click node_haystack "https://github.com/ryancodrai/turbovec/blob/main/turbovec-python/python/turbovec/haystack.py"
click node_agno "https://github.com/ryancodrai/turbovec/blob/main/turbovec-python/python/turbovec/agno.py"
click node_bench_suite "https://github.com/ryancodrai/turbovec/tree/main/benchmarks/suite"
click node_bench_results "https://github.com/ryancodrai/turbovec/tree/main/benchmarks/results"
click node_bench_poc "https://github.com/ryancodrai/turbovec/tree/main/benchmarks/rabitq_poc"

classDef toneNeutral fill:#f8fafc,stroke:#334155,stroke-width:1.5px,color:#0f172a
classDef toneBlue fill:#dbeafe,stroke:#2563eb,stroke-width:1.5px,color:#172554
classDef toneAmber fill:#fef3c7,stroke:#d97706,stroke-width:1.5px,color:#78350f
classDef toneMint fill:#dcfce7,stroke:#16a34a,stroke-width:1.5px,color:#14532d
classDef toneRose fill:#ffe4e6,stroke:#e11d48,stroke-width:1.5px,color:#881337
classDef toneIndigo fill:#e0e7ff,stroke:#4f46e5,stroke-width:1.5px,color:#312e81
classDef toneTeal fill:#ccfbf1,stroke:#0f766e,stroke-width:1.5px,color:#134e4a
class node_lib,node_encode,node_rotation,node_codebook,node_pack,node_search,node_idmap,node_io,node_error,node_kernels toneBlue
class node_pyext,node_pyapi,node_langchain,node_llama,node_haystack,node_agno toneAmber
class node_bench_suite,node_bench_results,node_bench_poc toneMint
```

## Components

### Core Rust engine

Main vector indexing engine and native search pipeline.

- [turbovec](https://github.com/ryancodrai/turbovec/blob/main/turbovec/src/lib.rs): Rust crate · Top-level Rust library exposing the public indexing API. · `turbovec/src/lib.rs`
- [Encode](https://github.com/ryancodrai/turbovec/blob/main/turbovec/src/encode.rs): ingest pipeline · Normalizes vectors and converts them into quantized codes. · `turbovec/src/encode.rs`
- [Rotation](https://github.com/ryancodrai/turbovec/blob/main/turbovec/src/rotation.rs): shared transform · Applies the shared random rotation used by ingest and query paths. · `turbovec/src/rotation.rs`
- [Codebook](https://github.com/ryancodrai/turbovec/blob/main/turbovec/src/codebook.rs): quantization · Defines scalar quantization behavior for compressed vector codes. · `turbovec/src/codebook.rs`
- [Packing](https://github.com/ryancodrai/turbovec/blob/main/turbovec/src/pack.rs): bit packing · Stores quantized codes in packed form for compact search. · `turbovec/src/pack.rs`
- [Search](https://github.com/ryancodrai/turbovec/blob/main/turbovec/src/search.rs): query engine · Runs approximate search over packed vectors and produces top-k results. · `turbovec/src/search.rs`
- [Id map](https://github.com/ryancodrai/turbovec/blob/main/turbovec/src/id_map.rs): ID layer · Maps stable external u64 IDs onto internal storage slots and handles removals. · `turbovec/src/id_map.rs`
- [Persistence](https://github.com/ryancodrai/turbovec/blob/main/turbovec/src/io.rs): serialization · Loads and writes persisted index state for standalone use. · `turbovec/src/io.rs`
- [Errors](https://github.com/ryancodrai/turbovec/blob/main/turbovec/src/error.rs): error types · Shared error handling for the Rust core API. · `turbovec/src/error.rs`
- **SIMD kernels**: native kernels · Architecture-specific scoring implementations for ARM and x86.

### Python package

Python extension module and framework adapters built on the Rust core.

- [Python ext](https://github.com/ryancodrai/turbovec/blob/main/turbovec-python/src/lib.rs): PyO3 module · Native extension module that wraps the Rust library for Python. · `turbovec-python/src/lib.rs`
- [Python API](https://github.com/ryancodrai/turbovec/blob/main/turbovec-python/python/turbovec/__init__.py): package · Python-facing package exposing the index types and bindings. · `turbovec-python/python/turbovec/__init__.py`
- [LangChain](https://github.com/ryancodrai/turbovec/blob/main/turbovec-python/python/turbovec/langchain.py): adapter · Compatibility adapter for LangChain integrations. · `turbovec-python/python/turbovec/langchain.py`
- [LlamaIndex](https://github.com/ryancodrai/turbovec/blob/main/turbovec-python/python/turbovec/llama_index.py): adapter · Compatibility adapter for LlamaIndex integrations. · `turbovec-python/python/turbovec/llama_index.py`
- [Haystack](https://github.com/ryancodrai/turbovec/blob/main/turbovec-python/python/turbovec/haystack.py): adapter · Compatibility adapter for Haystack integrations. · `turbovec-python/python/turbovec/haystack.py`
- [Agno](https://github.com/ryancodrai/turbovec/blob/main/turbovec-python/python/turbovec/agno.py): adapter · Compatibility adapter for Agno integrations. · `turbovec-python/python/turbovec/agno.py`

### Benchmarks

Measurement and evaluation harness for recall, compression, and speed.

- [Bench suite](https://github.com/ryancodrai/turbovec/tree/main/benchmarks/suite): evaluation harness · Benchmark scripts for recall, compression, and speed measurements. · `benchmarks/suite`
- [Results](https://github.com/ryancodrai/turbovec/tree/main/benchmarks/results): outputs · Archived benchmark outputs and comparison artifacts. · `benchmarks/results`
- [PoC compare](https://github.com/ryancodrai/turbovec/tree/main/benchmarks/rabitq_poc): baseline analysis · Prototype comparison code and baseline result sets. · `benchmarks/rabitq_poc`

## Connections

- turbovec → Encode: uses · Public index API feeds vectors into the ingest pipeline.
- Encode → Rotation: applies · Ingest rotates vectors into the shared quantized space.
- Encode → Codebook: quantizes · Coordinates are scalar-quantized into compact codes.
- Codebook → Packing: packs · Quantized codes are bit-packed for storage and search.
- turbovec → Search: exposes · Search is the primary query entry point of the crate.
- Search → Rotation: mirrors · Queries are rotated before scoring against packed codes.
- Search → Packing: scans · Hot path scores directly from packed representation.
- Search → SIMD kernels: dispatches · CPU-specific SIMD kernels implement the scoring hot path.
- turbovec → Id map: wraps · ID-mapped index builds stable external IDs on top of core storage.
- Id map → Search: returns ids · Search can emit external IDs instead of internal slots.
- Id map → Persistence: persists · Mapped IDs are preserved in serialized index state.
- turbovec → Persistence: serializes · Core index state can be dumped and restored from disk.
- turbovec → Errors: reports · Shared error types define the crate boundary.
- Python ext → turbovec: binds · Python extension wraps the Rust core rather than reimplementing it.
- Python API → Python ext: imports · Python package exposes the native extension to users.
- Python API → LangChain: adapts · Framework adapter for LangChain.
- Python API → LlamaIndex: adapts · Framework adapter for LlamaIndex.
- Python API → Haystack: adapts · Framework adapter for Haystack.
- Python API → Agno: adapts · Framework adapter for Agno.
- Bench suite → turbovec: measures · Benchmark suite exercises the Rust engine across recall, speed, and compression.
- Bench suite → Results: writes · Benchmark runs produce archived JSON outputs and charts.
- PoC compare → turbovec: compares · Prototype analysis compares turbovec against baseline approaches.
