Some speed improvments to routing table

This commit is contained in:
Dennis Schwerdel 2016-08-05 08:42:41 +02:00
parent 8a45cf6192
commit d5a6002507
1 changed files with 10 additions and 4 deletions

View File

@ -65,7 +65,7 @@ type Hash = BuildHasherDefault<FnvHasher>;
/// To speed up lookup, prefixes are grouped into full bytes and map to a list of prefixes with /// To speed up lookup, prefixes are grouped into full bytes and map to a list of prefixes with
/// more fine grained prefixes. /// more fine grained prefixes.
#[derive(Default)] #[derive(Default)]
pub struct RoutingTable(HashMap<Vec<u8>, Vec<RoutingEntry>, Hash>); pub struct RoutingTable(HashMap<[u8; 16], Vec<RoutingEntry>, Hash>);
impl RoutingTable { impl RoutingTable {
/// Creates a new empty routing table /// Creates a new empty routing table
@ -87,8 +87,8 @@ impl Table for RoutingTable {
// length. // length.
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 = [0; 16];
group_bytes.extend_from_slice(&addr.data[0..group_len]); group_bytes[..group_len].copy_from_slice(&addr.data[..group_len]);
// Create an entry // Create an entry
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};
// Add the entry to the routing table, creating a new list of the prefix group is empty. // Add the entry to the routing table, creating a new list of the prefix group is empty.
@ -99,14 +99,20 @@ impl Table for RoutingTable {
} }
/// Retrieves a peer for an address if it is inside the routing table /// Retrieves a peer for an address if it is inside the routing table
#[allow(unknown_lints, needless_range_loop)]
fn lookup(&mut self, addr: &Address) -> Option<SocketAddr> { fn lookup(&mut self, addr: &Address) -> Option<SocketAddr> {
let len = addr.len as usize; let len = addr.len as usize;
let mut found = None; let mut found = None;
let mut found_len: isize = -1; let mut found_len: isize = -1;
// Iterate over the prefix length from longest prefix group to shortest (empty) prefix // Iterate over the prefix length from longest prefix group to shortest (empty) prefix
// group // group
let mut group_bytes = addr.data;
for i in len..16 {
group_bytes[i] = 0;
}
for i in 0..len+1 { for i in 0..len+1 {
if let Some(group) = self.0.get(&addr.data[0..len-i]) { group_bytes[len-i] = 0;
if let Some(group) = self.0.get(&group_bytes) {
// If the group is not empty, check every entry // If the group is not empty, check every entry
for entry in group { for entry in group {
// Calculate the match length of the address and the prefix // Calculate the match length of the address and the prefix