Skip to content

Commit

Permalink
Inline the run_gc method.
Browse files Browse the repository at this point in the history
  • Loading branch information
squidarth committed Jun 18, 2018
1 parent 611a59a commit 3fbc626
Showing 1 changed file with 10 additions and 14 deletions.
24 changes: 10 additions & 14 deletions src/iface/neighbor.rs
Original file line number Diff line number Diff line change
Expand Up @@ -7,9 +7,7 @@ use wire::{EthernetAddress, IpAddress};
use time::{Duration, Instant};

#[cfg(any(feature = "std", feature = "alloc"))]
use std::mem::replace;
#[cfg(any(feature = "std", feature = "alloc"))]
use std::collections::BTreeMap;
use core::mem::replace;

/// A cached neighbor.
///
Expand Down Expand Up @@ -88,26 +86,24 @@ impl<'a> Cache<'a> {
Cache { storage, gc_threshold, silent_until: Instant::from_millis(0) }
}

#[cfg(any(feature = "std", feature = "alloc"))]
fn run_gc(original_btree_map: &BTreeMap<IpAddress, Neighbor>, timestamp: Instant) -> BTreeMap<IpAddress, Neighbor> {
original_btree_map.into_iter()
.map(|(key, value)| (*key, *value))
.filter(|(_, v)| timestamp < v.expires_at)
.collect()
}

pub(crate) fn fill(&mut self, protocol_addr: IpAddress, hardware_addr: EthernetAddress,
timestamp: Instant) {
debug_assert!(protocol_addr.is_unicast());
debug_assert!(hardware_addr.is_unicast());

let _current_storage_size = self.storage.len();
#[cfg(any(feature = "std", feature = "alloc"))]
let current_storage_size = self.storage.len();

match self.storage {
ManagedMap::Borrowed(_) => (),
#[cfg(any(feature = "std", feature = "alloc"))]
ManagedMap::Owned(ref mut map) => {
if _current_storage_size >= self.gc_threshold {
let new_btree_map = Cache::run_gc(map, timestamp);
if current_storage_size >= self.gc_threshold {
let new_btree_map = map.into_iter()
.map(|(key, value)| (*key, *value))
.filter(|(_, v)| timestamp < v.expires_at)
.collect();

replace(map, new_btree_map);
}
}
Expand Down

0 comments on commit 3fbc626

Please sign in to comment.