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

+11 -4
View File
@@ -113,8 +113,8 @@ mod tests {
use super::*;
use crate::router::MemoryHandler;
use crate::state::MemoryState;
use std::sync::atomic::AtomicUsize;
use std::sync::RwLock;
use std::sync::atomic::AtomicUsize;
use tempfile::tempdir;
#[tokio::test]
@@ -137,7 +137,9 @@ mod tests {
block: None,
reason: None,
};
let res_set = gate_set_handler(State(app_state.clone()), Json(set_req)).await.unwrap();
let res_set = gate_set_handler(State(app_state.clone()), Json(set_req))
.await
.unwrap();
assert_eq!(res_set.into_response().status(), axum::http::StatusCode::OK);
// Verify the gate (and consume it)
@@ -148,8 +150,13 @@ mod tests {
params: HashMap::new(),
consume: true,
};
let res_verify = gate_verify_handler(State(app_state.clone()), Query(verify_req)).await.unwrap();
assert_eq!(res_verify.into_response().status(), axum::http::StatusCode::OK);
let res_verify = gate_verify_handler(State(app_state.clone()), Query(verify_req))
.await
.unwrap();
assert_eq!(
res_verify.into_response().status(),
axum::http::StatusCode::OK
);
// Verify again should fail since it was consumed
let verify_req2 = GateVerifyReq {
+8 -8
View File
@@ -201,16 +201,16 @@ pub fn create_router(app_state: Arc<AppState>) -> Router {
#[cfg(test)]
mod tests {
use super::*;
use crate::AppState;
use crate::router::MemoryHandler;
use crate::state::MemoryState;
use crate::AppState;
use axum::http::Request;
use axum::body::Body;
use std::sync::atomic::AtomicUsize;
use std::sync::RwLock;
use axum::http::Request;
use std::collections::HashMap;
use tower::ServiceExt;
use std::sync::RwLock;
use std::sync::atomic::AtomicUsize;
use tempfile::tempdir;
use tower::ServiceExt;
#[tokio::test]
async fn test_create_router_health() {
@@ -221,15 +221,15 @@ mod tests {
clients: RwLock::new(HashMap::new()),
next_id: AtomicUsize::new(1),
});
let app = create_router(app_state);
// Test health endpoint
let request = Request::builder()
.uri("/health")
.body(Body::empty())
.unwrap();
let response = app.oneshot(request).await.unwrap();
assert_eq!(response.status(), 200);
}
+7 -3
View File
@@ -146,10 +146,10 @@ pub async fn handle_socket(socket: WebSocket, state: Arc<AppState>, _client_type
#[cfg(test)]
mod tests {
use super::*;
use std::sync::atomic::AtomicUsize;
use std::sync::RwLock;
use crate::router::MemoryHandler;
use crate::state::MemoryState;
use std::sync::RwLock;
use std::sync::atomic::AtomicUsize;
use tempfile::tempdir;
#[tokio::test]
@@ -163,7 +163,11 @@ mod tests {
});
// Insert a dummy client
app_state.clients.write().unwrap().insert("test-session".to_string(), tokio::sync::mpsc::channel(1).0);
app_state
.clients
.write()
.unwrap()
.insert("test-session".to_string(), tokio::sync::mpsc::channel(1).0);
let send_task = tokio::spawn(async {});
let recv_task = tokio::spawn(async {});