Skip to content

Commit 4dbc069

Browse files
fix(vector): Correct FAISS index training and search locking
This commit addresses two critical issues in the `faiss_manager.rs` file to improve correctness and performance. First, it fixes a bug where the FAISS index was not being trained before vectors were added. This is a required step for many index types (e.g., IVF) and would lead to runtime crashes. The code now checks if the index is trained and, if not, trains it on the first batch of vectors that meets the training threshold. Second, it resolves a performance bottleneck in concurrent search scenarios. The `search` function was taking a write lock on the index, which unnecessarily serialized all search operations. This has been changed to a read lock, allowing for concurrent searches and significantly improving throughput in multi-threaded environments.
1 parent bac41e8 commit 4dbc069

File tree

1 file changed

+14
-4
lines changed

1 file changed

+14
-4
lines changed

crates/codegraph-vector/src/faiss_manager.rs

Lines changed: 14 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -75,16 +75,25 @@ impl SimpleFaissManager {
7575
return Ok(());
7676
}
7777

78-
// Prepare flat vector array for FAISS
7978
let flat_vectors: Vec<f32> = vectors
8079
.iter()
8180
.flat_map(|(_, embedding)| embedding.iter().cloned())
8281
.collect();
8382

84-
// Add to FAISS index
8583
let mut index_guard = self.index.write();
8684
let index = index_guard.as_mut().unwrap();
8785

86+
// CRITICAL FIX: Train the index if it's not already trained
87+
if !index.is_trained() && vectors.len() >= self.config.training_threshold {
88+
info!(
89+
"Training FAISS index with {} vectors...",
90+
flat_vectors.len() / self.config.dimension
91+
);
92+
index
93+
.train(&flat_vectors)
94+
.map_err(|e| CodeGraphError::Vector(format!("Failed to train index: {}", e)))?;
95+
}
96+
8897
index
8998
.add(&flat_vectors)
9099
.map_err(|e| CodeGraphError::Vector(format!("Failed to add vectors: {}", e)))?;
@@ -119,9 +128,10 @@ impl SimpleFaissManager {
119128
)));
120129
}
121130

122-
let mut index_guard = self.index.write();
131+
// Use a read lock for concurrent searches
132+
let index_guard = self.index.read();
123133
let index = index_guard
124-
.as_mut()
134+
.as_ref()
125135
.ok_or_else(|| CodeGraphError::Vector("Index not initialized".to_string()))?;
126136

127137
let search_result = index

0 commit comments

Comments
 (0)