Skip to content

Commit

Permalink
perf: avoid cloning in filtering ptr (#272)
Browse files Browse the repository at this point in the history
  • Loading branch information
CosminPerRam authored Nov 24, 2024
1 parent e185d6f commit 7f6c5e9
Showing 1 changed file with 29 additions and 27 deletions.
56 changes: 29 additions & 27 deletions src/dns_cache.rs
Original file line number Diff line number Diff line change
Expand Up @@ -372,33 +372,34 @@ impl DnsCache {
pub(crate) fn refresh_due_srv(&mut self, ty_domain: &str) -> (HashSet<String>, HashSet<u64>) {
let now = current_time_millis();

let mut instances = vec![];

// find instance names for ty_domain.
for record in self.ptr.get_mut(ty_domain).into_iter().flatten() {
if record.get_record().is_expired(now) {
continue;
}

if let Some(ptr) = record.any().downcast_ref::<DnsPointer>() {
instances.push(ptr.alias.clone());
}
}
let instances: Vec<&String> = self
.ptr
.get(ty_domain)
.into_iter()
.flatten()
.filter(|record| !record.get_record().is_expired(now))
.filter_map(|record| {
record
.any()
.downcast_ref::<DnsPointer>()
.map(|ptr| &ptr.alias)
})
.collect();

// Check SRV records.
let mut refresh_due = HashSet::new();
let mut new_timers = HashSet::new();
for instance in instances {
let refresh_timers: HashSet<u64> = self
.srv
.get_mut(&instance)
.get_mut(instance)
.into_iter()
.flatten()
.filter_map(|record| record.updated_refresh_time(now))
.collect();

if !refresh_timers.is_empty() {
refresh_due.insert(instance);
refresh_due.insert(instance.clone());
new_timers.extend(refresh_timers);
}
}
Expand All @@ -411,25 +412,26 @@ impl DnsCache {
pub(crate) fn refresh_due_hosts(&mut self, ty_domain: &str) -> (HashSet<String>, HashSet<u64>) {
let now = current_time_millis();

let mut instances = vec![];

// find instance names for ty_domain.
for record in self.ptr.get_mut(ty_domain).into_iter().flatten() {
if record.get_record().is_expired(now) {
continue;
}

if let Some(ptr) = record.any().downcast_ref::<DnsPointer>() {
instances.push(ptr.alias.clone());
}
}
let instances: Vec<&String> = self
.ptr
.get(ty_domain)
.into_iter()
.flatten()
.filter(|record| !record.get_record().is_expired(now))
.filter_map(|record| {
record
.any()
.downcast_ref::<DnsPointer>()
.map(|ptr| &ptr.alias)
})
.collect();

// Collect hostnames we have browsers for by SRV records.
let mut hostnames_browsed = HashSet::new();
for instance in instances {
let hosts: HashSet<String> = self
.srv
.get(&instance)
.get(instance)
.into_iter()
.flatten()
.filter_map(|record| {
Expand Down

0 comments on commit 7f6c5e9

Please sign in to comment.