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

+18 -9
View File
@@ -164,14 +164,14 @@ impl McpTool for GetEnvironmentDetailsHandler {
#[cfg(test)]
mod tests {
use super::*;
use tempfile::tempdir;
use serde_json::json;
use tempfile::tempdir;
#[tokio::test]
async fn test_env_fingerprint() {
let dir = tempdir().unwrap();
let state = Arc::new(MemoryState::new(dir.path().to_str().unwrap()));
let update_handler = UpdateEnvFingerprintHandler;
let args = json!({
"namespace": "global",
@@ -179,12 +179,15 @@ mod tests {
"rustc": "1.70.0"
}
});
let res = update_handler.execute(args, state.clone()).await.unwrap();
assert_eq!(res, "Env fingerprint updated");
let read_handler = ReadEnvFingerprintHandler;
let res2 = read_handler.execute(json!({"namespace": "global"}), state.clone()).await.unwrap();
let res2 = read_handler
.execute(json!({"namespace": "global"}), state.clone())
.await
.unwrap();
assert!(res2.contains("rustc"));
assert!(res2.contains("1.70.0"));
}
@@ -193,7 +196,7 @@ mod tests {
async fn test_env_details() {
let dir = tempdir().unwrap();
let state = Arc::new(MemoryState::new(dir.path().to_str().unwrap()));
// Ensure namespace is present in test setup
state.environments.modify(|e| {
e.push(crate::models::EnvironmentDetail {
@@ -207,8 +210,11 @@ mod tests {
});
let handler = GetEnvironmentDetailsHandler;
let res = handler.execute(json!({"namespace": "global"}), state.clone()).await.unwrap();
let res = handler
.execute(json!({"namespace": "global"}), state.clone())
.await
.unwrap();
assert!(res.contains("global"));
}
@@ -241,8 +247,11 @@ mod tests {
assert_eq!(res2, "Environment registered");
let get_handler = GetEnvironmentDetailsHandler;
let res3 = get_handler.execute(json!({"namespace": "global"}), state.clone()).await.unwrap();
let res3 = get_handler
.execute(json!({"namespace": "global"}), state.clone())
.await
.unwrap();
assert!(res3.contains("prod.local"));
assert!(res3.len() > 0);
assert!(!res3.is_empty());
}
}