perf(search): eliminate full database cloning during tantivy index rebuilds by moving iteration into a synchronized blocking thread

This commit is contained in:
Riz Ashraf committed 2026-09-21 16:42:14 +01:00
1 parent 8934857635
commit 1af21b3aec
2 files changed
+70 -69

No files matched your search

+27 -9
View File
@@ -3,7 +3,7 @@ use crate::search::MemoryIndex;
use crate::store::Store;
use std::collections::HashMap;
use std::path::PathBuf;
use std::sync::RwLock;
use std::sync::{Arc, RwLock};
pub struct MemoryState {
pub base_dir: PathBuf,
@@ -60,15 +60,33 @@ impl MemoryState {
self.graph.modify(update_fn);
}
pub async fn rebuild_index(&self) {
pub async fn rebuild_index(self: &Arc<Self>) {
if let Ok(new_idx) = MemoryIndex::new(&self.base_dir) {
let entities: Vec<Entity> = self.graph.read_with(|g| g.entities.values().cloned().collect());
let tasks = self.tasks.read_with(|t| t.clone());
let snippets = self.snippets.read_with(|s| s.clone());
let adrs = self.adrs.read_with(|a| a.clone());
let handle = new_idx.index_batch(&entities, &tasks, &snippets, &adrs);
let _ = handle.await;
let state = Arc::clone(self);
let idx = new_idx.clone();
tokio::task::spawn_blocking(move || {
state.graph.read_with(|g| {
for e in g.entities.values() {
idx.add_entity_sync(e);
}
});
state.tasks.read_with(|tasks| {
for t in tasks {
idx.add_task_sync(t);
}
});
state.snippets.read_with(|snippets| {
for s in snippets {
idx.add_snippet_sync(s);
}
});
state.adrs.read_with(|adrs| {
for a in adrs {
idx.add_adr_sync(a);
}
});
}).await.unwrap();
let _ = new_idx.commit().await;
if let Ok(mut w) = self.search_index.write() {