Using slice operations instead of loops

This commit is contained in:
Dennis Schwerdel 2016-03-29 14:26:59 +02:00
parent fe7b0f0007
commit 15cd399827
6 changed files with 14 additions and 34 deletions

View File

@ -15,6 +15,7 @@ This project follows [semantic versioning](http://semver.org).
- [changed] Only calling crypto_init once - [changed] Only calling crypto_init once
- [changed] Passing listen address as &str - [changed] Passing listen address as &str
- [changed] Using FNV hash for better performance - [changed] Using FNV hash for better performance
- [changed] Using slice operations instead of loops
- [fixed] Fixed wrong hex address formatting - [fixed] Fixed wrong hex address formatting
- [fixed] Fixed peer exchange for more than 65000 peers - [fixed] Fixed peer exchange for more than 65000 peers
- [fixed] Initializing crypto for benchmarks - [fixed] Initializing crypto for benchmarks

View File

@ -212,9 +212,7 @@ impl Crypto {
match method { match method {
CryptoMethod::ChaCha20 => { CryptoMethod::ChaCha20 => {
let mut crypto_key = [0; crypto_aead_chacha20poly1305_ietf_KEYBYTES]; let mut crypto_key = [0; crypto_aead_chacha20poly1305_ietf_KEYBYTES];
for i in 0..crypto_key.len() { crypto_key.clone_from_slice(&key[..crypto_aead_chacha20poly1305_ietf_KEYBYTES]);
crypto_key[i] = key[i];
}
let mut nonce = [0u8; crypto_aead_chacha20poly1305_ietf_NPUBBYTES]; let mut nonce = [0u8; crypto_aead_chacha20poly1305_ietf_NPUBBYTES];
unsafe { randombytes_buf(nonce.as_mut_ptr(), nonce.len()) }; unsafe { randombytes_buf(nonce.as_mut_ptr(), nonce.len()) };
Crypto::ChaCha20Poly1305{key: crypto_key, nonce: nonce} Crypto::ChaCha20Poly1305{key: crypto_key, nonce: nonce}

View File

@ -32,10 +32,8 @@ impl Protocol for Frame {
let mut dst = [0; 16]; let mut dst = [0; 16];
src[0] = data[pos]; src[1] = data[pos+1]; src[0] = data[pos]; src[1] = data[pos+1];
dst[0] = data[pos]; dst[1] = data[pos+1]; dst[0] = data[pos]; dst[1] = data[pos+1];
for i in 0..6 { src[2..8].clone_from_slice(&src_data);
src[i+2] = src_data[i]; dst[2..8].clone_from_slice(&dst_data);
dst[i+2] = dst_data[i];
}
Ok((Address{data: src, len: 8}, Address{data: dst, len: 8})) Ok((Address{data: src, len: 8}, Address{data: dst, len: 8}))
} else { } else {
let src = try!(Address::read_from_fixed(&src_data, 6)); let src = try!(Address::read_from_fixed(&src_data, 6));

View File

@ -70,9 +70,7 @@ impl Table for RoutingTable {
let group_len = prefix_len as usize / 8; let group_len = prefix_len as usize / 8;
assert!(group_len <= 16); assert!(group_len <= 16);
let mut group_bytes = Vec::with_capacity(group_len); let mut group_bytes = Vec::with_capacity(group_len);
for i in 0..group_len { group_bytes.extend_from_slice(&addr.data[0..group_len]);
group_bytes.push(addr.data[i]);
}
let routing_entry = RoutingEntry{address: address, bytes: addr.data, prefix_len: prefix_len}; let routing_entry = RoutingEntry{address: address, bytes: addr.data, prefix_len: prefix_len};
match self.0.entry(group_bytes) { match self.0.entry(group_bytes) {
hash_map::Entry::Occupied(mut entry) => entry.get_mut().push(routing_entry), hash_map::Entry::Occupied(mut entry) => entry.get_mut().push(routing_entry),

View File

@ -41,9 +41,7 @@ impl Address {
return Err(Error::ParseError("Address too short")); return Err(Error::ParseError("Address too short"));
} }
let mut bytes = [0; 16]; let mut bytes = [0; 16];
for i in 0..len { bytes[0..len].clone_from_slice(&data[0..len]);
bytes[i] = data[i];
}
Ok(Address{data: bytes, len: len as u8}) Ok(Address{data: bytes, len: len as u8})
} }
@ -51,9 +49,8 @@ impl Address {
pub fn write_to(&self, data: &mut[u8]) -> usize { pub fn write_to(&self, data: &mut[u8]) -> usize {
assert!(data.len() >= self.len as usize + 1); assert!(data.len() >= self.len as usize + 1);
data[0] = self.len; data[0] = self.len;
for i in 0..self.len as usize { let len = self.len as usize;
data[i+1] = self.data[i]; data[1..len+1].clone_from_slice(&self.data[0..len]);
}
self.len as usize + 1 self.len as usize + 1
} }
} }
@ -106,9 +103,7 @@ impl FromStr for Address {
if let Ok(addr) = Ipv4Addr::from_str(text) { if let Ok(addr) = Ipv4Addr::from_str(text) {
let ip = addr.octets(); let ip = addr.octets();
let mut res = [0; 16]; let mut res = [0; 16];
for i in 0..4 { res[0..4].clone_from_slice(&ip);
res[i] = ip[i];
}
return Ok(Address{data: res, len: 4}); return Ok(Address{data: res, len: 4});
} }
if let Ok(addr) = Ipv6Addr::from_str(text) { if let Ok(addr) = Ipv6Addr::from_str(text) {

View File

@ -36,9 +36,7 @@ impl TopHeader {
return Err(Error::ParseError("Empty message")); return Err(Error::ParseError("Empty message"));
} }
let mut header = TopHeader::default(); let mut header = TopHeader::default();
for i in 0..3 { header.magic.clone_from_slice(&data[0..3]);
header.magic[i] = data[i];
}
header.version = data[3]; header.version = data[3];
header.crypto_method = data[4]; header.crypto_method = data[4];
header.flags = data[6]; header.flags = data[6];
@ -47,9 +45,7 @@ impl TopHeader {
} }
pub fn write_to(&self, data: &mut [u8]) -> usize { pub fn write_to(&self, data: &mut [u8]) -> usize {
for i in 0..3 { data[0..3].clone_from_slice(&self.magic);
data[i] = self.magic[i];
}
data[3] = self.version; data[3] = self.version;
data[4] = self.crypto_method; data[4] = self.crypto_method;
data[6] = self.flags; data[6] = self.flags;
@ -179,9 +175,7 @@ pub fn decode<'a>(data: &'a mut [u8], crypto: &mut Crypto) -> Result<(Options, M
let stage = data[pos]; let stage = data[pos];
pos += 1; pos += 1;
let mut node_id = [0; NODE_ID_BYTES]; let mut node_id = [0; NODE_ID_BYTES];
for i in 0..NODE_ID_BYTES { node_id.clone_from_slice(&data[pos..pos+NODE_ID_BYTES]);
node_id[i] = data[pos+i];
}
pos += NODE_ID_BYTES; pos += NODE_ID_BYTES;
let count = data[pos] as usize; let count = data[pos] as usize;
pos += 1; pos += 1;
@ -225,9 +219,7 @@ pub fn encode<'a>(options: &Options, msg: &'a mut Message, mut buf: &'a mut [u8]
pos += 1; pos += 1;
for addr in v4addrs { for addr in v4addrs {
let ip = addr.ip().octets(); let ip = addr.ip().octets();
for i in 0..4 { buf[pos..pos+4].clone_from_slice(&ip);
buf[pos+i] = ip[i];
}
pos += 4; pos += 4;
Encoder::write_u16(addr.port(), &mut buf[pos..]); Encoder::write_u16(addr.port(), &mut buf[pos..]);
pos += 2; pos += 2;
@ -250,9 +242,7 @@ pub fn encode<'a>(options: &Options, msg: &'a mut Message, mut buf: &'a mut [u8]
assert!(buf.len() >= pos + 2 + NODE_ID_BYTES); assert!(buf.len() >= pos + 2 + NODE_ID_BYTES);
buf[pos] = stage; buf[pos] = stage;
pos += 1; pos += 1;
for i in 0..NODE_ID_BYTES { buf[pos..pos+NODE_ID_BYTES].clone_from_slice(node_id);
buf[pos+i] = node_id[i];
}
pos += NODE_ID_BYTES; pos += NODE_ID_BYTES;
assert!(ranges.len() <= 255); assert!(ranges.len() <= 255);
buf[pos] = ranges.len() as u8; buf[pos] = ranges.len() as u8;