ferrovec 0.3.4

A durable, incremental HNSW vector store built to compile to WebAssembly — the in-browser index that persists to OPFS and stays consistent across tabs. Also a plain native crate.
Documentation
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
use std::cmp::{Ordering, Reverse};
use std::collections::{BinaryHeap, HashMap, HashSet};

use serde::{Deserialize, Serialize};

use crate::config::{Config, Metric};
use crate::error::Error;

/// Magic bytes prepended to every serialized index.
const MAGIC: &[u8; 4] = b"FVEC";
/// On-disk serialization format version.
const FORMAT_VERSION: u32 = 1;
/// Maximum layer a node can be assigned.
const MAX_LEVEL: usize = 32;

/// A single search result: an id and its distance to the query.
///
/// Distances follow the "smaller is closer" convention of the index metric.
#[derive(Clone, Debug, PartialEq)]
pub struct Neighbor {
    /// The user-supplied id of the matched vector.
    pub id: String,
    /// Distance from the query to this vector under the index metric.
    pub distance: f32,
}

/// A graph node. Tombstoned (`deleted`) nodes remain in the graph for
/// connectivity but are filtered out of search results.
#[derive(Serialize, Deserialize, Clone, Debug)]
struct Node {
    id: String,
    /// `layers[l]` holds neighbor node-indices at layer `l`.
    /// The node's top layer is `layers.len() - 1`.
    layers: Vec<Vec<u32>>,
    deleted: bool,
}

/// Internal candidate used inside the priority queues.
#[derive(Clone, Copy, Debug)]
struct Candidate {
    dist: f32,
    node: u32,
}

impl PartialEq for Candidate {
    fn eq(&self, other: &Self) -> bool {
        self.node == other.node && self.dist.total_cmp(&other.dist) == Ordering::Equal
    }
}
impl Eq for Candidate {}
impl Ord for Candidate {
    fn cmp(&self, other: &Self) -> Ordering {
        self.dist
            .total_cmp(&other.dist)
            .then_with(|| self.node.cmp(&other.node))
    }
}
impl PartialOrd for Candidate {
    fn partial_cmp(&self, other: &Self) -> Option<Ordering> {
        Some(self.cmp(other))
    }
}

/// A hand-rolled HNSW (Hierarchical Navigable Small World) vector index.
///
/// The index supports upsert-style [`insert`](Hnsw::insert), tombstoning
/// [`remove`](Hnsw::remove), approximate nearest-neighbor
/// [`search`](Hnsw::search), and byte (de)serialization via
/// [`to_bytes`](Hnsw::to_bytes) / [`from_bytes`](Hnsw::from_bytes).
#[derive(Serialize, Deserialize, Clone)]
pub struct Hnsw {
    dims: usize,
    config: Config,
    nodes: Vec<Node>,
    /// Flat vector store: node `i` occupies `[i*dims .. (i+1)*dims]`.
    vectors: Vec<f32>,
    entry_point: Option<u32>,
    max_layer: usize,
    /// Rebuilt after deserialization; never persisted.
    #[serde(skip)]
    id_map: HashMap<String, u32>,
    live_count: usize,
    rng_state: u64,
    /// Level-generation multiplier `1 / ln(max_connections)`; never persisted.
    #[serde(skip)]
    m_l: f64,
}

impl Hnsw {
    /// Create a new index for `dims`-dimensional vectors using the default
    /// [`Config`].
    pub fn new(dims: usize) -> Self {
        Self::with_config(dims, Config::default())
    }

    /// Create a new index for `dims`-dimensional vectors with an explicit
    /// [`Config`].
    pub fn with_config(dims: usize, config: Config) -> Self {
        let m_l = compute_m_l(config.max_connections);
        let rng_state = config.seed;
        Hnsw {
            dims,
            config,
            nodes: Vec::new(),
            vectors: Vec::new(),
            entry_point: None,
            max_layer: 0,
            id_map: HashMap::new(),
            live_count: 0,
            rng_state,
            m_l,
        }
    }

    /// Number of live (non-tombstoned) vectors in the index.
    pub fn len(&self) -> usize {
        self.live_count
    }

    /// Returns `true` if there are no live vectors.
    pub fn is_empty(&self) -> bool {
        self.live_count == 0
    }

    /// Dimensionality this index was created with.
    pub fn dims(&self) -> usize {
        self.dims
    }

    /// The active configuration.
    pub fn config(&self) -> &Config {
        &self.config
    }

    // --- PRNG (deterministic splitmix64) -----------------------------------

    fn next_u64(&mut self) -> u64 {
        self.rng_state = self.rng_state.wrapping_add(0x9E37_79B9_7F4A_7C15);
        let mut z = self.rng_state;
        z = (z ^ (z >> 30)).wrapping_mul(0xBF58_476D_1CE4_E5B9);
        z = (z ^ (z >> 27)).wrapping_mul(0x94D0_49BB_1331_11EB);
        z ^ (z >> 31)
    }

    fn next_f64(&mut self) -> f64 {
        (self.next_u64() >> 11) as f64 / (1u64 << 53) as f64
    }

    fn random_level(&mut self) -> usize {
        let r = self.next_f64().max(f64::MIN_POSITIVE);
        let lvl = (-r.ln() * self.m_l).floor();
        if lvl <= 0.0 {
            0
        } else if lvl as usize > MAX_LEVEL {
            MAX_LEVEL
        } else {
            lvl as usize
        }
    }

    // --- Distance helpers --------------------------------------------------

    #[inline]
    fn vector(&self, i: u32) -> &[f32] {
        let start = i as usize * self.dims;
        &self.vectors[start..start + self.dims]
    }

    fn distance(&self, a: &[f32], b: &[f32]) -> f32 {
        use crate::simd::{dot, l2_sq};
        match self.config.metric {
            Metric::Dot => 1.0 - dot(a, b),
            Metric::Cosine => {
                let d = dot(a, b);
                let na = dot(a, a).sqrt();
                let nb = dot(b, b).sqrt();
                if na == 0.0 || nb == 0.0 {
                    1.0
                } else {
                    1.0 - d / (na * nb)
                }
            }
            Metric::L2 => l2_sq(a, b),
        }
    }

    #[inline]
    fn dist_query(&self, query: &[f32], node: u32) -> f32 {
        self.distance(query, self.vector(node))
    }

    #[inline]
    fn dist_nodes(&self, a: u32, b: u32) -> f32 {
        self.distance(self.vector(a), self.vector(b))
    }

    // --- Core HNSW routines ------------------------------------------------

    /// Standard HNSW SEARCH-LAYER. Returns up to `ef` nearest candidates found
    /// on `layer`, reachable from `entry_points`.
    fn search_layer(
        &self,
        query: &[f32],
        entry_points: &[u32],
        ef: usize,
        layer: usize,
    ) -> Vec<Candidate> {
        let mut visited: HashSet<u32> = HashSet::with_capacity(entry_points.len() * 8);
        // Min-heap of candidates still to expand (nearest first).
        let mut candidates: BinaryHeap<Reverse<Candidate>> = BinaryHeap::new();
        // Max-heap of best results found so far (farthest first), capped at ef.
        let mut results: BinaryHeap<Candidate> = BinaryHeap::new();

        for &ep in entry_points {
            if visited.insert(ep) {
                let d = self.dist_query(query, ep);
                let c = Candidate { dist: d, node: ep };
                candidates.push(Reverse(c));
                results.push(c);
            }
        }
        while results.len() > ef {
            results.pop();
        }

        while let Some(Reverse(current)) = candidates.pop() {
            if let Some(farthest) = results.peek() {
                if results.len() >= ef && current.dist > farthest.dist {
                    break;
                }
            }

            let neighbors = {
                let node = &self.nodes[current.node as usize];
                if layer < node.layers.len() {
                    node.layers[layer].clone()
                } else {
                    Vec::new()
                }
            };

            for nb in neighbors {
                if visited.insert(nb) {
                    let d = self.dist_query(query, nb);
                    let admit = results.len() < ef
                        || results.peek().is_none_or(|far| d < far.dist);
                    if admit {
                        let c = Candidate { dist: d, node: nb };
                        candidates.push(Reverse(c));
                        results.push(c);
                        while results.len() > ef {
                            results.pop();
                        }
                    }
                }
            }
        }

        results.into_vec()
    }

    /// HNSW neighbor-selection heuristic. Keeps a candidate `e` only if it is
    /// closer to `base` than to every already-selected neighbor. Returns at
    /// most `m` node indices.
    fn select_neighbors(&self, base: u32, candidate_nodes: &[u32], m: usize) -> Vec<u32> {
        let mut cands: Vec<Candidate> = Vec::with_capacity(candidate_nodes.len());
        let mut seen: HashSet<u32> = HashSet::with_capacity(candidate_nodes.len());
        for &n in candidate_nodes {
            if n == base || !seen.insert(n) {
                continue;
            }
            cands.push(Candidate {
                dist: self.dist_nodes(base, n),
                node: n,
            });
        }
        cands.sort_unstable();

        let mut selected: Vec<u32> = Vec::with_capacity(m);
        for c in cands {
            if selected.len() >= m {
                break;
            }
            let mut keep = true;
            for &r in &selected {
                // Reject e if it is closer to an already-kept r than to base.
                if self.dist_nodes(c.node, r) < c.dist {
                    keep = false;
                    break;
                }
            }
            if keep {
                selected.push(c.node);
            }
        }
        selected
    }

    // --- Public mutation / query ------------------------------------------

    /// Insert (or upsert) a vector under `id`.
    ///
    /// If `id` is already present, the existing node is tombstoned and a fresh
    /// node is inserted, so only one live entry per id ever remains.
    ///
    /// # Errors
    /// Returns [`Error::DimensionMismatch`] if `vector.len() != self.dims()`.
    pub fn insert(&mut self, id: impl Into<String>, vector: &[f32]) -> Result<(), Error> {
        let id: String = id.into();
        if vector.len() != self.dims {
            return Err(Error::DimensionMismatch {
                expected: self.dims,
                got: vector.len(),
            });
        }

        // Upsert: tombstone any existing live node with this id.
        if let Some(&old) = self.id_map.get(&id) {
            let node = &mut self.nodes[old as usize];
            if !node.deleted {
                node.deleted = true;
                self.live_count -= 1;
            }
            self.id_map.remove(&id);
        }

        let new_idx = self.nodes.len() as u32;
        self.vectors.extend_from_slice(vector);
        let level = self.random_level();
        self.nodes.push(Node {
            id: id.clone(),
            layers: vec![Vec::new(); level + 1],
            deleted: false,
        });
        self.id_map.insert(id, new_idx);
        self.live_count += 1;

        // First node ever: becomes the entry point.
        let entry = match self.entry_point {
            Some(e) => e,
            None => {
                self.entry_point = Some(new_idx);
                self.max_layer = level;
                return Ok(());
            }
        };

        let max_layer = self.max_layer;
        let mut ep = entry;

        // Phase 1: descend from the top down to just above the new node's level,
        // greedily following the single nearest neighbor.
        let mut lc = max_layer;
        while lc > level {
            let found = self.search_layer(vector, &[ep], 1, lc);
            if let Some(nearest) = found.iter().min_by(|a, b| a.cmp(b)) {
                ep = nearest.node;
            }
            lc -= 1;
        }

        // Phase 2: from min(max_layer, level) down to 0, connect the new node.
        let mut entry_points = vec![ep];
        let mut lc = max_layer.min(level) as isize;
        while lc >= 0 {
            let layer = lc as usize;
            let found = self.search_layer(vector, &entry_points, self.config.ef_construction, layer);
            let cand_nodes: Vec<u32> = found.iter().map(|c| c.node).collect();
            let selected = self.select_neighbors(new_idx, &cand_nodes, self.config.max_connections);

            // Connect bidirectionally at this layer.
            for &nb in &selected {
                self.nodes[new_idx as usize].layers[layer].push(nb);
                self.nodes[nb as usize].layers[layer].push(new_idx);
            }

            // Prune each touched neighbor back down to its cap.
            let cap = if layer == 0 {
                2 * self.config.max_connections
            } else {
                self.config.max_connections
            };
            for &nb in &selected {
                let current = self.nodes[nb as usize].layers[layer].clone();
                if current.len() > cap {
                    let pruned = self.select_neighbors(nb, &current, cap);
                    self.nodes[nb as usize].layers[layer] = pruned;
                }
            }

            // Carry the found candidates down as entry points for the next layer.
            entry_points = if cand_nodes.is_empty() {
                vec![ep]
            } else {
                cand_nodes
            };
            lc -= 1;
        }

        // Grow the graph height if this node reached a new top level.
        if level > self.max_layer {
            self.max_layer = level;
            self.entry_point = Some(new_idx);
        }

        Ok(())
    }

    /// Tombstone the vector stored under `id`.
    ///
    /// Returns `true` if a live entry existed and was removed, `false`
    /// otherwise. The node stays in the graph for connectivity but is excluded
    /// from future search results.
    pub fn remove(&mut self, id: &str) -> bool {
        if let Some(&idx) = self.id_map.get(id) {
            let node = &mut self.nodes[idx as usize];
            if !node.deleted {
                node.deleted = true;
                self.live_count -= 1;
            }
            self.id_map.remove(id);
            true
        } else {
            false
        }
    }

    /// Returns `true` if a live vector is stored under `id`.
    ///
    /// Tombstoned (removed or upserted-over) ids report `false`.
    pub fn contains(&self, id: &str) -> bool {
        self.id_map.contains_key(id)
    }

    /// Reset the index to empty, keeping [`dims`](Hnsw::dims) and the active
    /// [`Config`].
    ///
    /// All nodes and vectors are dropped, the entry point is cleared, and the
    /// internal PRNG is rewound to `config.seed` so a subsequent rebuild is
    /// deterministic. After `clear`, [`len`](Hnsw::len) is `0` and the index can
    /// be inserted into again.
    pub fn clear(&mut self) {
        self.nodes.clear();
        self.vectors.clear();
        self.entry_point = None;
        self.max_layer = 0;
        self.id_map.clear();
        self.live_count = 0;
        self.rng_state = self.config.seed;
    }

    /// Rebuild the index in place, dropping all tombstoned nodes.
    ///
    /// [`remove`](Hnsw::remove) and upserting [`insert`](Hnsw::insert) only
    /// tombstone the old node; it lingers in the graph and vector store to
    /// preserve connectivity, so memory grows under churn. `compact` reclaims
    /// that space: it collects every live vector (in insertion order), resets
    /// the graph, and re-inserts each one through the normal build path.
    ///
    /// The PRNG is rewound to `config.seed` first, so compacting is
    /// deterministic and yields the same graph a fresh build of the surviving
    /// vectors (inserted in the same order) would. [`len`](Hnsw::len) is
    /// unchanged, previously-removed ids stay gone, and search results for live
    /// data remain correct — while the internal node and vector counts drop to
    /// exactly the live set.
    pub fn compact(&mut self) {
        // Snapshot the live set in insertion order before tearing down state.
        let mut live: Vec<(String, Vec<f32>)> = Vec::with_capacity(self.live_count);
        for i in 0..self.nodes.len() {
            if !self.nodes[i].deleted {
                let id = self.nodes[i].id.clone();
                let v = self.vector(i as u32).to_vec();
                live.push((id, v));
            }
        }

        self.clear();

        for (id, v) in live {
            // Re-inserting an already-accepted vector cannot fail: dimensions
            // are unchanged and each live id is unique.
            debug_assert_eq!(v.len(), self.dims);
            let _ = self.insert(id, &v);
        }
    }

    /// Search for the `k` nearest neighbors of `query`.
    ///
    /// Results are sorted ascending by distance (nearest first), exclude
    /// tombstoned nodes, and contain at most `k` entries.
    ///
    /// # Errors
    /// Returns [`Error::DimensionMismatch`] if `query.len() != self.dims()`.
    pub fn search(&self, query: &[f32], k: usize) -> Result<Vec<Neighbor>, Error> {
        if query.len() != self.dims {
            return Err(Error::DimensionMismatch {
                expected: self.dims,
                got: query.len(),
            });
        }
        if k == 0 {
            return Ok(Vec::new());
        }
        let mut ep = match self.entry_point {
            Some(e) => e,
            None => return Ok(Vec::new()),
        };

        // Greedy descent through the upper layers.
        let mut lc = self.max_layer;
        while lc >= 1 {
            let found = self.search_layer(query, &[ep], 1, lc);
            if let Some(nearest) = found.iter().min_by(|a, b| a.cmp(b)) {
                ep = nearest.node;
            }
            lc -= 1;
        }

        let ef = self.config.ef_search.max(k);
        let found = self.search_layer(query, &[ep], ef, 0);

        let mut cands: Vec<Candidate> = found
            .into_iter()
            .filter(|c| !self.nodes[c.node as usize].deleted)
            .collect();
        cands.sort_unstable();
        cands.truncate(k);

        Ok(cands
            .into_iter()
            .map(|c| Neighbor {
                id: self.nodes[c.node as usize].id.clone(),
                distance: c.dist,
            })
            .collect())
    }

    // --- Serialization -----------------------------------------------------

    /// Serialize the index to bytes: an 8-byte header (`b"FVEC"` + LE format
    /// version) followed by the postcard-encoded payload.
    pub fn to_bytes(&self) -> Result<Vec<u8>, Error> {
        let payload: Vec<u8> =
            postcard::to_allocvec(self).map_err(|e| Error::Serialize(e.to_string()))?;
        let mut out = Vec::with_capacity(8 + payload.len());
        out.extend_from_slice(MAGIC);
        out.extend_from_slice(&FORMAT_VERSION.to_le_bytes());
        out.extend_from_slice(&payload);
        Ok(out)
    }

    /// Deserialize an index previously produced by [`to_bytes`](Hnsw::to_bytes).
    ///
    /// Validates the magic header and format version, then rebuilds the derived
    /// `id_map` and `m_l` fields.
    ///
    /// # Errors
    /// - [`Error::BadFormat`] if the blob is too short or has a bad magic.
    /// - [`Error::VersionMismatch`] if the format version is unsupported.
    /// - [`Error::Deserialize`] if the payload fails to decode.
    pub fn from_bytes(data: &[u8]) -> Result<Self, Error> {
        if data.len() < 8 || &data[0..4] != MAGIC {
            return Err(Error::BadFormat);
        }
        let version = u32::from_le_bytes([data[4], data[5], data[6], data[7]]);
        if version != FORMAT_VERSION {
            return Err(Error::VersionMismatch(version));
        }

        let mut index: Hnsw =
            postcard::from_bytes(&data[8..]).map_err(|e| Error::Deserialize(e.to_string()))?;

        // Rebuild derived, non-persisted state.
        index.id_map = HashMap::with_capacity(index.nodes.len());
        for (i, node) in index.nodes.iter().enumerate() {
            if !node.deleted {
                index.id_map.insert(node.id.clone(), i as u32);
            }
        }
        index.m_l = compute_m_l(index.config.max_connections);

        Ok(index)
    }
}

/// Level-generation multiplier `1 / ln(max_connections)`, guarded so a
/// `max_connections` of 0 or 1 does not produce a non-finite result.
fn compute_m_l(max_connections: usize) -> f64 {
    if max_connections <= 1 {
        1.0
    } else {
        1.0 / (max_connections as f64).ln()
    }
}

#[cfg(test)]
mod tests {
    use super::*;

    #[test]
    fn splitmix_is_deterministic() {
        let mut a = Hnsw::new(4);
        let mut b = Hnsw::new(4);
        for _ in 0..100 {
            assert_eq!(a.next_u64(), b.next_u64());
        }
    }

    #[test]
    fn next_f64_in_unit_interval() {
        let mut h = Hnsw::new(4);
        for _ in 0..10_000 {
            let r = h.next_f64();
            assert!((0.0..1.0).contains(&r));
        }
    }

    #[test]
    fn random_level_is_capped() {
        let mut h = Hnsw::new(4);
        for _ in 0..100_000 {
            assert!(h.random_level() <= MAX_LEVEL);
        }
    }

    #[test]
    fn dot_distance_zero_for_identical_unit() {
        let h = Hnsw::with_config(
            3,
            Config {
                metric: Metric::Dot,
                ..Config::default()
            },
        );
        let v = [1.0, 0.0, 0.0];
        assert!((h.distance(&v, &v) - 0.0).abs() < 1e-6);
    }

    #[test]
    fn cosine_guards_zero_norm() {
        let h = Hnsw::with_config(
            3,
            Config {
                metric: Metric::Cosine,
                ..Config::default()
            },
        );
        let zero = [0.0, 0.0, 0.0];
        let v = [1.0, 2.0, 3.0];
        assert_eq!(h.distance(&zero, &v), 1.0);
        assert_eq!(h.distance(&zero, &zero), 1.0);
    }

    #[test]
    fn l2_is_squared_euclidean() {
        let h = Hnsw::with_config(
            2,
            Config {
                metric: Metric::L2,
                ..Config::default()
            },
        );
        let a = [0.0, 0.0];
        let b = [3.0, 4.0];
        assert!((h.distance(&a, &b) - 25.0).abs() < 1e-6);
    }
}