perf: replace inefficient memory cloning vectors with fast zero-allocation read iterators

This commit is contained in:
Riz Ashraf committed 2026-09-21 12:51:26 +01:00
1 parent 5da422d23a
commit 3c31aeec1f
6 files changed
+108 -69

No files matched your search

+27 -20
View File
@@ -263,15 +263,18 @@ impl McpTool for ListActiveTasksHandler {
async fn execute(&self, args: Value, state: Arc<MemoryState>) -> Result<String, String> {
let req: ListActiveTasksTool = serde_json::from_value(args).map_err(|e| e.to_string())?;
let mut tasks = state.tasks.read();
tasks.retain(|t| t.status != "done");
if let Some(branch) = req.git_branch {
tasks.retain(|t| {
t.git_branch.is_none() || t.git_branch.as_deref() == Some(branch.as_str())
});
}
let data = serde_json::to_string(&tasks).unwrap_or_default();
Ok(data.to_string())
let data = state.tasks.read_with(|tasks| {
let filtered: Vec<_> = tasks.iter().filter(|t| {
let status_match = t.status != "done";
let branch_match = match &req.git_branch {
Some(branch) => t.git_branch.is_none() || t.git_branch.as_deref() == Some(branch.as_str()),
None => true,
};
status_match && branch_match
}).collect();
serde_json::to_string(&filtered).unwrap_or_default()
});
Ok(data)
}
}
@@ -313,7 +316,7 @@ impl McpTool for SetAcceptanceCriteriaHandler {
}
});
if success {
Ok(vec!["Acceptance criteria set successfully.".to_string()][0].clone())
Ok("Acceptance criteria set successfully.".to_string())
} else {
Ok("Task not found.".to_string())
}
@@ -360,15 +363,14 @@ impl McpTool for VerifyAcceptanceCriteriaHandler {
}
});
if success {
Ok(vec![format!(
Ok(format!(
"Acceptance criteria verified with proof: {}",
req.proof
)][0]
.clone())
))
} else if already_met {
Ok("Acceptance criteria was already met.".to_string())
} else {
Ok(vec!["Acceptance criteria or task not found.".to_string()][0].clone())
Ok("Acceptance criteria or task not found.".to_string())
}
}
}
@@ -446,11 +448,16 @@ impl McpTool for ListMilestonesHandler {
async fn execute(&self, args: Value, state: Arc<MemoryState>) -> Result<String, String> {
let req: ListMilestonesTool = serde_json::from_value(args).map_err(|e| e.to_string())?;
let mut items = state.milestones.read();
if let Some(ns) = req.namespace {
items.retain(|i| i.namespace == ns);
}
let data = serde_json::to_string(&items).unwrap_or_default();
Ok(data.to_string())
let data = state.milestones.read_with(|items| {
let filtered: Vec<_> = items.iter().filter(|i| {
if let Some(ns) = &req.namespace {
&i.namespace == ns
} else {
true
}
}).collect();
serde_json::to_string(&filtered).unwrap_or_default()
});
Ok(data)
}
}