From d5983877956db6ea7f0a110f3a0dd0f28d48e3bc Mon Sep 17 00:00:00 2001 From: Natty Date: Sat, 16 Nov 2024 17:43:57 +0100 Subject: [PATCH] Recycle the read buffers for RPC data --- src/rpc_v1/proto.rs | 14 ++++++++------ 1 file changed, 8 insertions(+), 6 deletions(-) diff --git a/src/rpc_v1/proto.rs b/src/rpc_v1/proto.rs index abf011c..22c1cf9 100644 --- a/src/rpc_v1/proto.rs +++ b/src/rpc_v1/proto.rs @@ -396,6 +396,8 @@ impl RpcCallDecoder { async_stream::try_stream! { let mut messages = 0usize; + let mut name_buf = vec![0; 128]; + let mut payload_buf = vec![0; 1024]; loop { let read_fut = async { @@ -429,7 +431,7 @@ impl RpcCallDecoder { )).into_diagnostic(); } - let mut name_buf = vec![0; name_len]; + name_buf.resize(name_len, 0); buf_read.read_exact(&mut name_buf).await.into_diagnostic()?; let payload_len = buf_read.read_u32().await.into_diagnostic()? as usize; @@ -441,20 +443,20 @@ impl RpcCallDecoder { )).into_diagnostic(); } - let mut payload_buf = vec![0; payload_len]; + payload_buf.resize(payload_len, 0); buf_read.read_exact(&mut payload_buf).await.into_diagnostic()?; - miette::Result::<_>::Ok(Some((serial, name_buf, payload_buf))) + miette::Result::<_>::Ok(Some((serial, &name_buf[..], &payload_buf[..]))) }; - let Some((serial, name_buf, payload_buf)) = select! { + let Some((serial, name_slice, payload_slice)) = select! { read_result = read_fut => read_result, _ = &mut cancel => { break; } }? else { break; }; - let name = std::str::from_utf8(&name_buf).into_diagnostic()?; + let name = std::str::from_utf8(name_slice).into_diagnostic()?; let decoder = decoders .get(name) @@ -465,7 +467,7 @@ impl RpcCallDecoder { .ok_or_else(|| miette!("No such RPC call name: {}", name))? .clone(); - let packet = match decoder(&payload_buf) { + let packet = match decoder(payload_slice) { Ok(p) => p, Err(e) => { error!("Failed to parse packet: {e}");