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:
Riz Ashraf committed 2026-09-22 22:01:38 +01:00
1 parent 251757f8fc
commit 37003be620
17 files changed
+399 -198

No files matched your search

+46 -13
View File
@@ -368,14 +368,14 @@ use crate::handlers::utils::*;
#[cfg(test)]
mod tests {
use super::*;
use tempfile::tempdir;
use serde_json::json;
use tempfile::tempdir;
#[tokio::test]
async fn test_workspace_lifecycle() {
let dir = tempdir().unwrap();
let state = Arc::new(MemoryState::new(dir.path().to_str().unwrap()));
let save_handler = SaveContextWorkspaceHandler;
let args = json!({
"name": "wsl-session",
@@ -383,12 +383,15 @@ mod tests {
"pinned_files": ["src/main.rs"],
"active_task_ids": ["123"]
});
let res = save_handler.execute(args, state.clone()).await.unwrap();
assert_eq!(res, "Context workspace saved");
let list_handler = ListContextWorkspacesHandler;
let res2 = list_handler.execute(json!({"namespace": "global"}), state.clone()).await.unwrap();
let res2 = list_handler
.execute(json!({"namespace": "global"}), state.clone())
.await
.unwrap();
assert!(res2.contains("wsl-session"));
assert!(res2.contains("src/main.rs"));
}
@@ -397,7 +400,7 @@ mod tests {
async fn test_snippets_and_pr_checklists() {
let dir = tempdir().unwrap();
let state = Arc::new(MemoryState::new(dir.path().to_str().unwrap()));
let store_handler = StoreSnippetHandler;
let args_snip = json!({
"name": "init_db",
@@ -406,11 +409,20 @@ mod tests {
"code": "SELECT 1;",
"namespace": "global"
});
let res1 = store_handler.execute(args_snip, state.clone()).await.unwrap();
let res1 = store_handler
.execute(args_snip, state.clone())
.await
.unwrap();
assert_eq!(res1, "Snippet 'init_db' stored.");
let search_handler = SearchSnippetsHandler;
let _res2 = search_handler.execute(json!({"query": "SELECT", "namespace": "global"}), state.clone()).await.unwrap();
let _res2 = search_handler
.execute(
json!({"query": "SELECT", "namespace": "global"}),
state.clone(),
)
.await
.unwrap();
// Skip assertion since it requires index rebuild
let pr_handler = AddPrChecklistItemHandler;
@@ -422,25 +434,46 @@ mod tests {
assert_eq!(res3, "PR checklist item added");
let get_pr = GetPrChecklistHandler;
let res4 = get_pr.execute(json!({"namespace": "global"}), state.clone()).await.unwrap();
let res4 = get_pr
.execute(json!({"namespace": "global"}), state.clone())
.await
.unwrap();
assert!(res4.contains("Check coverage"));
// Pin lifecycle
let pin = PinFileHandler;
let res5 = pin.execute(json!({"file_path": "src/lib.rs", "namespace": "global"}), state.clone()).await.unwrap();
let res5 = pin
.execute(
json!({"file_path": "src/lib.rs", "namespace": "global"}),
state.clone(),
)
.await
.unwrap();
assert_eq!(res5, "File pinned");
let list_pins = ListPinnedFilesHandler;
let res6 = list_pins.execute(json!({"namespace": "global"}), state.clone()).await.unwrap();
let res6 = list_pins
.execute(json!({"namespace": "global"}), state.clone())
.await
.unwrap();
assert!(res6.contains("src/lib.rs"));
let unpin = UnpinFileHandler;
let res7 = unpin.execute(json!({"file_path": "src/lib.rs", "namespace": "global"}), state.clone()).await.unwrap();
let res7 = unpin
.execute(
json!({"file_path": "src/lib.rs", "namespace": "global"}),
state.clone(),
)
.await
.unwrap();
assert_eq!(res7, "File unpinned");
// Clear PR
let clear_pr = ClearPrChecklistHandler;
let res8 = clear_pr.execute(json!({"namespace": "global"}), state.clone()).await.unwrap();
let res8 = clear_pr
.execute(json!({"namespace": "global"}), state.clone())
.await
.unwrap();
assert_eq!(res8, "PR checklist cleared");
}
}