mirror of
https://github.com/YGGverse/aquatic.git
synced 2026-03-31 17:55:36 +00:00
aquatic_http_protocol: optimize response peer deserialization
Hopefully, now response peer vec only needs to be allocated once
This commit is contained in:
parent
b289e1f590
commit
f9d85117b1
1 changed files with 14 additions and 22 deletions
|
|
@ -194,28 +194,24 @@ impl<'de> Visitor<'de> for ResponsePeersIpv4Visitor {
|
|||
fn visit_bytes<E>(self, value: &[u8]) -> Result<Self::Value, E>
|
||||
where E: ::serde::de::Error,
|
||||
{
|
||||
let mut peers = Vec::new();
|
||||
|
||||
let mut ip_bytes = [0u8; 4];
|
||||
let mut port_bytes = [0u8; 2];
|
||||
|
||||
let chunks = value.chunks_exact(6);
|
||||
|
||||
if !chunks.remainder().is_empty(){
|
||||
return Err(::serde::de::Error::custom("trailing bytes"));
|
||||
}
|
||||
|
||||
for chunk in chunks {
|
||||
let mut ip_bytes = [0u8; 4];
|
||||
let mut port_bytes = [0u8; 2];
|
||||
|
||||
let peers = chunks.into_iter().map(|chunk | {
|
||||
ip_bytes.copy_from_slice(&chunk[0..4]);
|
||||
port_bytes.copy_from_slice(&chunk[4..6]);
|
||||
|
||||
let peer = ResponsePeer {
|
||||
ResponsePeer {
|
||||
ip_address: Ipv4Addr::from(u32::from_be_bytes(ip_bytes)),
|
||||
port: u16::from_be_bytes(port_bytes),
|
||||
};
|
||||
|
||||
peers.push(peer);
|
||||
}
|
||||
}
|
||||
}).collect();
|
||||
|
||||
Ok(peers)
|
||||
}
|
||||
|
|
@ -246,28 +242,24 @@ impl<'de> Visitor<'de> for ResponsePeersIpv6Visitor {
|
|||
fn visit_bytes<E>(self, value: &[u8]) -> Result<Self::Value, E>
|
||||
where E: ::serde::de::Error,
|
||||
{
|
||||
let mut peers = Vec::new();
|
||||
|
||||
let mut ip_bytes = [0u8; 16];
|
||||
let mut port_bytes = [0u8; 2];
|
||||
|
||||
let chunks = value.chunks_exact(18);
|
||||
|
||||
if !chunks.remainder().is_empty(){
|
||||
return Err(::serde::de::Error::custom("trailing bytes"));
|
||||
}
|
||||
|
||||
for chunk in chunks {
|
||||
let mut ip_bytes = [0u8; 16];
|
||||
let mut port_bytes = [0u8; 2];
|
||||
|
||||
let peers = chunks.into_iter().map(|chunk| {
|
||||
ip_bytes.copy_from_slice(&chunk[0..16]);
|
||||
port_bytes.copy_from_slice(&chunk[16..18]);
|
||||
|
||||
let peer = ResponsePeer {
|
||||
ResponsePeer {
|
||||
ip_address: Ipv6Addr::from(u128::from_be_bytes(ip_bytes)),
|
||||
port: u16::from_be_bytes(port_bytes),
|
||||
};
|
||||
|
||||
peers.push(peer);
|
||||
}
|
||||
}
|
||||
}).collect();
|
||||
|
||||
Ok(peers)
|
||||
}
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue