perf(nvim-core): eliminate heavy string allocations for JSON-RPC msgids in internal request mapping
This commit is contained in:
1 parent
05672cc884
commit
8adef5e216
1 file changed
+15
-11
+15
-11
@@ -111,7 +111,7 @@ use std::sync::LazyLock;
|
|||||||
use tokio::sync::{mpsc, oneshot};
|
use tokio::sync::{mpsc, oneshot};
|
||||||
|
|
||||||
pub struct NvimRequest {
|
pub struct NvimRequest {
|
||||||
pub msgid_str: String,
|
pub msgid: u64,
|
||||||
pub req: rmpv::Value,
|
pub req: rmpv::Value,
|
||||||
pub reply: oneshot::Sender<Result<rmpv::Value, String>>,
|
pub reply: oneshot::Sender<Result<rmpv::Value, String>>,
|
||||||
}
|
}
|
||||||
@@ -153,7 +153,7 @@ async fn get_nvim_connection() -> Result<mpsc::Sender<NvimRequest>, String> {
|
|||||||
let (mut read_half, mut write_half) = tokio::io::split(stream);
|
let (mut read_half, mut write_half) = tokio::io::split(stream);
|
||||||
let (tx, mut rx) = mpsc::channel::<NvimRequest>(32);
|
let (tx, mut rx) = mpsc::channel::<NvimRequest>(32);
|
||||||
type PendingRequestsMap =
|
type PendingRequestsMap =
|
||||||
Arc<std::sync::Mutex<HashMap<String, oneshot::Sender<Result<rmpv::Value, String>>>>>;
|
Arc<std::sync::Mutex<HashMap<u64, oneshot::Sender<Result<rmpv::Value, String>>>>>;
|
||||||
let pending_requests: PendingRequestsMap = Arc::new(std::sync::Mutex::new(HashMap::new()));
|
let pending_requests: PendingRequestsMap = Arc::new(std::sync::Mutex::new(HashMap::new()));
|
||||||
|
|
||||||
// Write task
|
// Write task
|
||||||
@@ -169,7 +169,7 @@ async fn get_nvim_connection() -> Result<mpsc::Sender<NvimRequest>, String> {
|
|||||||
pending_clone
|
pending_clone
|
||||||
.lock()
|
.lock()
|
||||||
.unwrap()
|
.unwrap()
|
||||||
.insert(req.msgid_str.clone(), req.reply);
|
.insert(req.msgid, req.reply);
|
||||||
|
|
||||||
if write_half.write_all(&buf).await.is_err() {
|
if write_half.write_all(&buf).await.is_err() {
|
||||||
tracing::error!("Failed to write to Neovim socket");
|
tracing::error!("Failed to write to Neovim socket");
|
||||||
@@ -193,13 +193,15 @@ async fn get_nvim_connection() -> Result<mpsc::Sender<NvimRequest>, String> {
|
|||||||
|
|
||||||
if let rmpv::Value::Array(ref arr) = val {
|
if let rmpv::Value::Array(ref arr) = val {
|
||||||
if arr.len() >= 4 && arr[0] == rmpv::Value::Integer(1.into()) {
|
if arr.len() >= 4 && arr[0] == rmpv::Value::Integer(1.into()) {
|
||||||
let msgid = &arr[1];
|
let msgid = match &arr[1] {
|
||||||
let msgid_str = format!("{msgid:?}");
|
rmpv::Value::Integer(i) => i.as_u64().unwrap_or(0),
|
||||||
|
_ => 0,
|
||||||
|
};
|
||||||
|
|
||||||
if let Some(reply_sender) = pending_clone2
|
if let Some(reply_sender) = pending_clone2
|
||||||
.lock()
|
.lock()
|
||||||
.unwrap_or_else(|e| e.into_inner())
|
.unwrap_or_else(|e| e.into_inner())
|
||||||
.remove(&msgid_str)
|
.remove(&msgid)
|
||||||
{
|
{
|
||||||
let _ = reply_sender.send(Ok(val));
|
let _ = reply_sender.send(Ok(val));
|
||||||
}
|
}
|
||||||
@@ -279,20 +281,22 @@ async fn get_nvim_connection() -> Result<mpsc::Sender<NvimRequest>, String> {
|
|||||||
async fn call_nvim(req: rmpv::Value) -> Result<rmpv::Value, String> {
|
async fn call_nvim(req: rmpv::Value) -> Result<rmpv::Value, String> {
|
||||||
let msgid = if let rmpv::Value::Array(ref arr) = req {
|
let msgid = if let rmpv::Value::Array(ref arr) = req {
|
||||||
if arr.len() > 1 {
|
if arr.len() > 1 {
|
||||||
arr[1].clone()
|
match &arr[1] {
|
||||||
|
rmpv::Value::Integer(i) => i.as_u64().unwrap_or(0),
|
||||||
|
_ => 0,
|
||||||
|
}
|
||||||
} else {
|
} else {
|
||||||
rmpv::Value::Nil
|
0
|
||||||
}
|
}
|
||||||
} else {
|
} else {
|
||||||
rmpv::Value::Nil
|
0
|
||||||
};
|
};
|
||||||
|
|
||||||
let msgid_str = format!("{msgid:?}");
|
|
||||||
let tx = get_nvim_connection().await?;
|
let tx = get_nvim_connection().await?;
|
||||||
let (reply_tx, reply_rx) = oneshot::channel();
|
let (reply_tx, reply_rx) = oneshot::channel();
|
||||||
|
|
||||||
tx.send(NvimRequest {
|
tx.send(NvimRequest {
|
||||||
msgid_str,
|
msgid,
|
||||||
req,
|
req,
|
||||||
reply: reply_tx,
|
reply: reply_tx,
|
||||||
})
|
})
|
||||||
|
|||||||
Reference in new issue
Block a user