From 3efee96230ba859720868b2449c0c267fd4c66d8 Mon Sep 17 00:00:00 2001 From: Riz Ashraf Date: Sun, 4 Oct 2026 02:17:37 +0100 Subject: [PATCH] perf: debounce store writes and add retry loop on Redb transaction collisions --- .cargo/config.toml | 3 --- justfile | 7 ++++++- server/src/store.rs | 21 ++++++++++++++++----- 3 files changed, 22 insertions(+), 9 deletions(-) diff --git a/.cargo/config.toml b/.cargo/config.toml index 69c509e..923844c 100644 --- a/.cargo/config.toml +++ b/.cargo/config.toml @@ -1,6 +1,3 @@ -[build] -rustc-wrapper = "sccache" - [target.x86_64-pc-windows-msvc] linker = "rust-lld" diff --git a/justfile b/justfile index 2d3a12e..e34e950 100644 --- a/justfile +++ b/justfile @@ -78,14 +78,16 @@ deploy-server: deploy-stub-win: @Write-Host "Deploying Windows Stub..." -ForegroundColor Cyan -Stop-Process -Name mcp-memory-stub -Force -ErrorAction SilentlyContinue + -if (Test-Path "C:\Users\reazul.ashraf\.local\bin\mcp-memory-stub.exe") { Move-Item -Force "C:\Users\reazul.ashraf\.local\bin\mcp-memory-stub.exe" "C:\Users\reazul.ashraf\.local\bin\mcp-memory-stub.exe.old" -ErrorAction SilentlyContinue; Remove-Item -Force "C:\Users\reazul.ashraf\.local\bin\mcp-memory-stub.exe.old" -ErrorAction SilentlyContinue } Copy-Item -Force target\release\mcp-memory-stub.exe "C:\Users\reazul.ashraf\.local\bin\" - deploy-nvim-win: @Write-Host "Deploying Windows Nvim..." -ForegroundColor Cyan -Stop-Process -Name mcp-memory-nvim -Force -ErrorAction SilentlyContinue + -if (Test-Path "C:\Users\reazul.ashraf\.local\bin\mcp-memory-nvim.exe") { Move-Item -Force "C:\Users\reazul.ashraf\.local\bin\mcp-memory-nvim.exe" "C:\Users\reazul.ashraf\.local\bin\mcp-memory-nvim.exe.old" -ErrorAction SilentlyContinue; Remove-Item -Force "C:\Users\reazul.ashraf\.local\bin\mcp-memory-nvim.exe.old" -ErrorAction SilentlyContinue } Copy-Item -Force target\release\mcp-memory-nvim.exe "C:\Users\reazul.ashraf\.local\bin\" + deploy-rules-win: @Write-Host "Deploying global Agent rules (Windows)..." -ForegroundColor Cyan if (!(Test-Path "C:\Users\reazul.ashraf\.gemini\config\rules")) { New-Item -ItemType Directory -Force -Path "C:\Users\reazul.ashraf\.gemini\config\rules" | Out-Null } @@ -109,6 +111,9 @@ build-nvim: @Write-Host "Building WSL Nvim natively..." -ForegroundColor Cyan wsl.exe -d Ubuntu -e bash -c 'export PATH="/home/riz/.cargo/bin:$PATH" && cd /mnt/c/Users/reazul.ashraf/workspace/rust/mcp-memory && cargo build --release -p mcp-memory-nvim' + + + deploy-nvim: @Write-Host "Deploying WSL Nvim natively..." -ForegroundColor Cyan wsl.exe -d Ubuntu -e bash -c 'export PATH="/home/riz/.cargo/bin:$PATH" && cd /mnt/c/Users/reazul.ashraf/workspace/rust/mcp-memory && cp target/release/mcp-memory-nvim /home/riz/.local/bin/' diff --git a/server/src/store.rs b/server/src/store.rs index dfe28c8..bd73d34 100644 --- a/server/src/store.rs +++ b/server/src/store.rs @@ -21,7 +21,9 @@ impl tokio::spawn(async move { while rx.recv().await.is_some() { - // Drain any other pending notifications so we batch writes + // Debounce window: wait 150ms to batch rapid sequential mutations + tokio::time::sleep(tokio::time::Duration::from_millis(150)).await; + // Drain any pending notifications accumulated during the debounce window while rx.try_recv().is_ok() {} let db_inner = db_clone.clone(); @@ -35,11 +37,20 @@ impl if let Some(json_data) = json_data { let _ = tokio::task::spawn_blocking(move || { - if let Ok(write_txn) = db_inner.begin_write() { - if let Ok(mut table) = write_txn.open_table(STORE_TABLE) { - let _ = table.insert(key_inner.as_str(), json_data.as_slice()); + // Retry up to 10 times with 30ms backoff if another Store holds write transaction + for _ in 0..10 { + match db_inner.begin_write() { + Ok(write_txn) => { + if let Ok(mut table) = write_txn.open_table(STORE_TABLE) { + let _ = table.insert(key_inner.as_str(), json_data.as_slice()); + } + let _ = write_txn.commit(); + break; + } + Err(_) => { + std::thread::sleep(std::time::Duration::from_millis(30)); + } } - let _ = write_txn.commit(); } }) .await;