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:32:11 +01:00
1 parent af2c4ab046
commit 27a4f3f320
4 files changed
+36 -10

No files matched your search

+5
View File
@@ -39,3 +39,8 @@ tempfile = "3.27.0"
[[bin]] [[bin]]
name = "test_rmcp" name = "test_rmcp"
path = "src/bin_test.rs" path = "src/bin_test.rs"
[[bin]]
name = "bin_test_axum"
path = "src/bin_test_axum.rs"
+11
View File
@@ -0,0 +1,11 @@
use axum::response::IntoResponse;
fn test_response() -> impl IntoResponse {
let tasks_json = "[]".to_string();
(
[(axum::http::header::CONTENT_TYPE, "application/json")],
tasks_json,
).into_response()
}
fn main() {}
+12
View File
@@ -0,0 +1,12 @@
fn main() {
let init = rmcp::model::InitializeResult::new(
rmcp::model::ServerCapabilities::builder()
.enable_tools()
.build(),
)
.with_server_info(rmcp::model::Implementation::new(
"gemini-mcp-memory",
"3.0.0",
));
println!("{:?}", serde_json::to_string(&init));
}
+8 -10
View File
@@ -297,8 +297,8 @@ async fn run_server(state: Arc<MemoryState>) -> Result<(), Box<dyn std::error::E
get({ get({
let state_clone = app_state.handler.state.clone(); let state_clone = app_state.handler.state.clone();
move || async move { move || async move {
let graph = state_clone.get_full_graph(); let graph_json = state_clone.read_graph(|g| serde_json::to_string(g).unwrap_or_else(|_| "{}".to_string()));
axum::Json(graph) ([(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({ get({
let state_clone = app_state.handler.state.clone(); let state_clone = app_state.handler.state.clone();
move || async move { move || async move {
let tasks = state_clone.tasks.read_with(|t| t.clone()); let tasks_json = state_clone.tasks.read_with(|t| serde_json::to_string(t).unwrap_or_else(|_| "[]".to_string()));
axum::Json(tasks) ([(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({ get({
let state_clone = app_state.handler.state.clone(); let state_clone = app_state.handler.state.clone();
move || async move { move || async move {
let sticky = state_clone.sticky.read_with(|s| s.clone()); let sticky_json = state_clone.sticky.read_with(|s| serde_json::to_string(s).unwrap_or_else(|_| "[]".to_string()));
axum::Json(sticky) ([(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({ get({
let state_clone = app_state.handler.state.clone(); let state_clone = app_state.handler.state.clone();
move || async move { move || async move {
let activities = state_clone.recent_activities.read_with(|a| { let activities_json = state_clone.recent_activities.read_with(|a| serde_json::to_string(a).unwrap_or_else(|_| "[]".to_string()));
a.iter().cloned().collect::<Vec<_>>() ([(axum::http::header::CONTENT_TYPE, "application/json")], activities_json)
});
axum::Json(serde_json::json!(activities))
} }
}), }),
) )