perf(api): eliminate expensive heap allocations and memory clones by serializing axum response payloads directly from store read locks

This commit is contained in:
Riz Ashraf committed 2026-09-21 19:33:06 +01:00
1 parent af2c4ab046
commit 208c5d448f
2 files changed
+9 -10

No files matched your search

+1
View File
@@ -39,3 +39,4 @@ tempfile = "3.27.0"
[[bin]]
name = "test_rmcp"
path = "src/bin_test.rs"
+8 -10
View File
@@ -297,8 +297,8 @@ async fn run_server(state: Arc<MemoryState>) -> Result<(), Box<dyn std::error::E
get({
let state_clone = app_state.handler.state.clone();
move || async move {
let graph = state_clone.get_full_graph();
axum::Json(graph)
let graph_json = state_clone.read_graph(|g| serde_json::to_string(g).unwrap_or_else(|_| "{}".to_string()));
([(axum::http::header::CONTENT_TYPE, "application/json")], graph_json)
}
}),
)
@@ -324,8 +324,8 @@ async fn run_server(state: Arc<MemoryState>) -> Result<(), Box<dyn std::error::E
get({
let state_clone = app_state.handler.state.clone();
move || async move {
let tasks = state_clone.tasks.read_with(|t| t.clone());
axum::Json(tasks)
let tasks_json = state_clone.tasks.read_with(|t| serde_json::to_string(t).unwrap_or_else(|_| "[]".to_string()));
([(axum::http::header::CONTENT_TYPE, "application/json")], tasks_json)
}
}),
)
@@ -334,8 +334,8 @@ async fn run_server(state: Arc<MemoryState>) -> Result<(), Box<dyn std::error::E
get({
let state_clone = app_state.handler.state.clone();
move || async move {
let sticky = state_clone.sticky.read_with(|s| s.clone());
axum::Json(sticky)
let sticky_json = state_clone.sticky.read_with(|s| serde_json::to_string(s).unwrap_or_else(|_| "[]".to_string()));
([(axum::http::header::CONTENT_TYPE, "application/json")], sticky_json)
}
}),
)
@@ -372,10 +372,8 @@ async fn run_server(state: Arc<MemoryState>) -> Result<(), Box<dyn std::error::E
get({
let state_clone = app_state.handler.state.clone();
move || async move {
let activities = state_clone.recent_activities.read_with(|a| {
a.iter().cloned().collect::<Vec<_>>()
});
axum::Json(serde_json::json!(activities))
let activities_json = state_clone.recent_activities.read_with(|a| serde_json::to_string(a).unwrap_or_else(|_| "[]".to_string()));
([(axum::http::header::CONTENT_TYPE, "application/json")], activities_json)
}
}),
)