From 8adef5e2163ec805c5f693233b8b209ce937a818 Mon Sep 17 00:00:00 2001 From: Riz Ashraf Date: Tue, 22 Sep 2026 04:50:30 +0100 Subject: [PATCH] perf(nvim-core): eliminate heavy string allocations for JSON-RPC msgids in internal request mapping --- nvim-core/src/lib.rs | 26 +++++++++++++++----------- 1 file changed, 15 insertions(+), 11 deletions(-) diff --git a/nvim-core/src/lib.rs b/nvim-core/src/lib.rs index 4d3f103..05de299 100644 --- a/nvim-core/src/lib.rs +++ b/nvim-core/src/lib.rs @@ -111,7 +111,7 @@ use std::sync::LazyLock; use tokio::sync::{mpsc, oneshot}; pub struct NvimRequest { - pub msgid_str: String, + pub msgid: u64, pub req: rmpv::Value, pub reply: oneshot::Sender>, } @@ -153,7 +153,7 @@ async fn get_nvim_connection() -> Result, String> { let (mut read_half, mut write_half) = tokio::io::split(stream); let (tx, mut rx) = mpsc::channel::(32); type PendingRequestsMap = - Arc>>>>; + Arc>>>>; let pending_requests: PendingRequestsMap = Arc::new(std::sync::Mutex::new(HashMap::new())); // Write task @@ -169,7 +169,7 @@ async fn get_nvim_connection() -> Result, String> { pending_clone .lock() .unwrap() - .insert(req.msgid_str.clone(), req.reply); + .insert(req.msgid, req.reply); if write_half.write_all(&buf).await.is_err() { tracing::error!("Failed to write to Neovim socket"); @@ -193,13 +193,15 @@ async fn get_nvim_connection() -> Result, String> { if let rmpv::Value::Array(ref arr) = val { if arr.len() >= 4 && arr[0] == rmpv::Value::Integer(1.into()) { - let msgid = &arr[1]; - let msgid_str = format!("{msgid:?}"); + let msgid = match &arr[1] { + rmpv::Value::Integer(i) => i.as_u64().unwrap_or(0), + _ => 0, + }; if let Some(reply_sender) = pending_clone2 .lock() .unwrap_or_else(|e| e.into_inner()) - .remove(&msgid_str) + .remove(&msgid) { let _ = reply_sender.send(Ok(val)); } @@ -279,20 +281,22 @@ async fn get_nvim_connection() -> Result, String> { async fn call_nvim(req: rmpv::Value) -> Result { let msgid = if let rmpv::Value::Array(ref arr) = req { if arr.len() > 1 { - arr[1].clone() + match &arr[1] { + rmpv::Value::Integer(i) => i.as_u64().unwrap_or(0), + _ => 0, + } } else { - rmpv::Value::Nil + 0 } } else { - rmpv::Value::Nil + 0 }; - let msgid_str = format!("{msgid:?}"); let tx = get_nvim_connection().await?; let (reply_tx, reply_rx) = oneshot::channel(); tx.send(NvimRequest { - msgid_str, + msgid, req, reply: reply_tx, })