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

+50 -17
View File
@@ -512,14 +512,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_log_error_fix() {
let dir = tempdir().unwrap();
let state = Arc::new(MemoryState::new(dir.path().to_str().unwrap()));
let handler = LogErrorFixHandler;
let args = json!({
"signature": "IndexOutOfBounds",
@@ -528,7 +528,7 @@ mod tests {
"git_commit": "abcdef",
"git_branch": "main"
});
let res = handler.execute(args, state.clone()).await.unwrap();
assert!(res.contains("Error fix logged"));
}
@@ -537,10 +537,10 @@ mod tests {
async fn test_project_health() {
let dir = tempdir().unwrap();
let state = Arc::new(MemoryState::new(dir.path().to_str().unwrap()));
let handler = GetProjectHealthHandler;
let args = json!({"namespace": "global"});
let res = handler.execute(args, state.clone()).await.unwrap();
assert!(res.contains("unresolved_tech_debt"));
}
@@ -549,7 +549,7 @@ mod tests {
async fn test_log_decision_and_tech_debt() {
let dir = tempdir().unwrap();
let state = Arc::new(MemoryState::new(dir.path().to_str().unwrap()));
let decision_handler = LogDecisionHandler;
let args_dec = json!({
"title": "Architecture",
@@ -557,7 +557,10 @@ mod tests {
"decision": "Use SQLite",
"consequence": "Simple",
});
let res1 = decision_handler.execute(args_dec, state.clone()).await.unwrap();
let res1 = decision_handler
.execute(args_dec, state.clone())
.await
.unwrap();
assert_eq!(res1, "Decision logged as ADR-0001");
let debt_handler = LogTechDebtHandler;
@@ -571,11 +574,20 @@ mod tests {
"git_branch": "main",
"namespace": "global"
});
let res2 = debt_handler.execute(args_debt, state.clone()).await.unwrap();
let res2 = debt_handler
.execute(args_debt, state.clone())
.await
.unwrap();
assert_eq!(res2, "Tech debt logged");
let list_debt = ListTechDebtHandler;
let res3 = list_debt.execute(json!({"namespace": "global", "include_resolved": false}), state.clone()).await.unwrap();
let res3 = list_debt
.execute(
json!({"namespace": "global", "include_resolved": false}),
state.clone(),
)
.await
.unwrap();
assert!(res3.contains("Hardcoded path"));
let pref_handler = LearnPreferenceHandler;
@@ -583,7 +595,10 @@ mod tests {
"key": "formatting",
"value": "use spaces",
});
let res4 = pref_handler.execute(args_pref, state.clone()).await.unwrap();
let res4 = pref_handler
.execute(args_pref, state.clone())
.await
.unwrap();
assert_eq!(res4, "Preference learned");
let read_pref = ReadPreferencesHandler;
@@ -603,10 +618,16 @@ mod tests {
"git_commit": "def",
"git_branch": "main"
});
code_handler.execute(args_code, state.clone()).await.unwrap();
code_handler
.execute(args_code, state.clone())
.await
.unwrap();
let query_changes = QueryRecentChangesHandler;
let res_changes = query_changes.execute(json!({}), state.clone()).await.unwrap();
let res_changes = query_changes
.execute(json!({}), state.clone())
.await
.unwrap();
assert!(res_changes.contains("main.rs"));
let debt_handler = LogTechDebtHandler;
@@ -620,15 +641,27 @@ mod tests {
"git_branch": "main",
"namespace": "global"
});
debt_handler.execute(args_debt, state.clone()).await.unwrap();
debt_handler
.execute(args_debt, state.clone())
.await
.unwrap();
// resolve it
let list_debt = ListTechDebtHandler;
let debt_list = list_debt.execute(json!({"namespace": "global", "include_resolved": false}), state.clone()).await.unwrap();
let debt_list = list_debt
.execute(
json!({"namespace": "global", "include_resolved": false}),
state.clone(),
)
.await
.unwrap();
let uuid_start = debt_list.find("id\":\"").unwrap() + 5;
let uuid = &debt_list[uuid_start..uuid_start+36];
let uuid = &debt_list[uuid_start..uuid_start + 36];
let resolve_debt = ResolveTechDebtHandler;
resolve_debt.execute(json!({"id": uuid}), state.clone()).await.unwrap();
resolve_debt
.execute(json!({"id": uuid}), state.clone())
.await
.unwrap();
}
}