From f9d85117b123697bb9141983a4c94321ef00b6c3 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Joakim=20Frosteg=C3=A5rd?= Date: Sun, 2 Aug 2020 11:34:09 +0200 Subject: [PATCH] aquatic_http_protocol: optimize response peer deserialization Hopefully, now response peer vec only needs to be allocated once --- aquatic_http_protocol/src/utils.rs | 36 ++++++++++++------------------ 1 file changed, 14 insertions(+), 22 deletions(-) diff --git a/aquatic_http_protocol/src/utils.rs b/aquatic_http_protocol/src/utils.rs index f2f8d76..2011cbf 100644 --- a/aquatic_http_protocol/src/utils.rs +++ b/aquatic_http_protocol/src/utils.rs @@ -194,28 +194,24 @@ impl<'de> Visitor<'de> for ResponsePeersIpv4Visitor { fn visit_bytes(self, value: &[u8]) -> Result 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(self, value: &[u8]) -> Result 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) }