switch packet buffer to smallvec

This commit is contained in:
soruh 2023-06-20 13:58:23 +02:00
parent 813e1af396
commit e77fa730dd
6 changed files with 14 additions and 7 deletions

4
Cargo.lock generated
View File

@ -216,6 +216,7 @@ dependencies = [
"once_cell",
"serde",
"serde_json",
"smallvec",
"time",
"tokio",
"tokio-stream",
@ -1088,6 +1089,9 @@ name = "smallvec"
version = "1.10.0"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "a507befe795404456341dfab10cef66ead4c041f62b8b11bbb92bffe5d0953e0"
dependencies = [
"serde",
]
[[package]]
name = "socket2"

View File

@ -30,6 +30,7 @@ zerocopy = "0.6.1"
tokio-stream = { version = "0.1.14", features = ["sync"] }
flate2 = { version = "1.0.26", optional = true }
bytes = "1.4.0"
smallvec = { version = "1.10.0", features = ["serde", "const_generics"] }
[build-dependencies]
minify-js = { version = "0.5.6", optional = true }

View File

@ -34,7 +34,7 @@ pub async fn dyn_ip_update(
kind: PacketKind::DynIpUpdate.raw(),
length: 8,
},
data: Vec::new(),
..Default::default()
};
packet.data.resize(packet.header.length as usize, 0);
@ -55,11 +55,11 @@ pub async fn dyn_ip_update(
packet.recv_into(&mut reader).await?;
let result = match packet.kind() {
PacketKind::DynIpUpdateResponse => Ok(<[u8; 4]>::try_from(packet.data)
.map_err(|err| {
PacketKind::DynIpUpdateResponse => Ok(<[u8; 4]>::try_from(packet.data.as_slice())
.map_err(|_| {
eyre!(
"too little data for ip address. Need 4 bytes got {}",
err.len()
packet.data.len()
)
})?
.into()),

View File

@ -212,7 +212,7 @@ pub async fn peer_query(server: &SocketAddr, number: u32) -> eyre::Result<Option
kind: 3, // Peer Query
length: 4,
},
data: Vec::new(),
..Default::default()
};
packet.data.clear();

View File

@ -14,6 +14,7 @@ use futures::Future;
use http::debug_server;
use packets::{Header, Packet};
use serde::{Deserialize, Deserializer};
use smallvec::SmallVec;
use time::format_description::OwnedFormatItem;
use tokio::{
io::AsyncWriteExt,
@ -293,7 +294,7 @@ async fn connection_handler(
kind: PacketKind::Reject.raw(),
length: 3,
},
data: b"nc\0".to_vec(),
data: SmallVec::from_slice(b"nc\0"),
},
);
}

View File

@ -3,6 +3,7 @@ use std::fmt::Debug;
use bytemuck::{Pod, Zeroable};
use eyre::eyre;
use serde::Serialize;
use smallvec::SmallVec;
use tokio::{
io::{AsyncReadExt, AsyncWriteExt},
net::tcp::{ReadHalf, WriteHalf},
@ -79,7 +80,7 @@ pub struct Header {
#[derive(Serialize, Default, Clone)]
pub struct Packet {
pub header: Header,
pub data: Vec<u8>,
pub data: SmallVec<[u8; 8]>,
}
impl Packet {