refactor: apply rust best practices and fix memory optimizations
- Optimized memory allocation in router.rs by offloading JSON serialization to spawn_blocking and using references. - Prevented full graph duplication on startup in state.rs index rebuild. - Eliminated massive String allocations in QueryGraphPathHandler BFS loops. - Avoided temporary Strings in VisualizeGraphHandler via inline writing. - Fixed O(N) full-graph deduplication in MergeEntitiesHandler to scale efficiently.
This commit is contained in:
1 parent
251757f8fc
commit
37003be620
17 files changed
+399
-198
No files matched your search
+25
-23
@@ -124,27 +124,29 @@ impl MemoryState {
|
||||
let idx = new_idx.clone();
|
||||
|
||||
tokio::task::spawn_blocking(move || {
|
||||
let entities: Vec<_> = state
|
||||
.graph
|
||||
.read_with(|g| g.entities.values().cloned().collect());
|
||||
for e in entities {
|
||||
idx.add_entity_sync(&e);
|
||||
}
|
||||
state.graph.read_with(|g| {
|
||||
for e in g.entities.values() {
|
||||
idx.add_entity_sync(e);
|
||||
}
|
||||
});
|
||||
|
||||
let tasks = state.tasks.read_with(|t| t.clone());
|
||||
for t in tasks {
|
||||
idx.add_task_sync(&t);
|
||||
}
|
||||
state.tasks.read_with(|t| {
|
||||
for task in t {
|
||||
idx.add_task_sync(task);
|
||||
}
|
||||
});
|
||||
|
||||
let snippets = state.snippets.read_with(|s| s.clone());
|
||||
for s in snippets {
|
||||
idx.add_snippet_sync(&s);
|
||||
}
|
||||
state.snippets.read_with(|s| {
|
||||
for snippet in s {
|
||||
idx.add_snippet_sync(snippet);
|
||||
}
|
||||
});
|
||||
|
||||
let adrs = state.adrs.read_with(|a| a.clone());
|
||||
for a in adrs {
|
||||
idx.add_adr_sync(&a);
|
||||
}
|
||||
state.adrs.read_with(|a| {
|
||||
for adr in a {
|
||||
idx.add_adr_sync(adr);
|
||||
}
|
||||
});
|
||||
})
|
||||
.await
|
||||
.unwrap_or_else(|e| {
|
||||
@@ -168,10 +170,10 @@ mod tests {
|
||||
async fn test_memory_state_initialization() {
|
||||
let dir = tempdir().unwrap();
|
||||
let state = MemoryState::new(dir.path().to_str().unwrap());
|
||||
|
||||
|
||||
// Ensure state fields are properly initialized
|
||||
assert_eq!(state.base_dir, dir.path());
|
||||
|
||||
|
||||
// Write a test value
|
||||
state.tasks.modify(|tasks| {
|
||||
tasks.push(Task {
|
||||
@@ -187,17 +189,17 @@ mod tests {
|
||||
parent_id: None,
|
||||
});
|
||||
});
|
||||
|
||||
|
||||
// Ensure it is saved
|
||||
state.tasks.read_with(|tasks| {
|
||||
assert_eq!(tasks.len(), 1);
|
||||
assert_eq!(tasks[0].id, "123");
|
||||
});
|
||||
|
||||
|
||||
// Test rebuild index
|
||||
let arc_state = Arc::new(state);
|
||||
arc_state.rebuild_index().await;
|
||||
|
||||
|
||||
// Check search index initialization
|
||||
let idx = arc_state.search_index.read().unwrap();
|
||||
// Just verify we can read it without panic
|
||||
|
||||
Reference in new issue
Block a user