Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

perf: avoid cloning in filtering ptr #272

Merged
merged 7 commits into from
Nov 24, 2024
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
56 changes: 29 additions & 27 deletions src/dns_cache.rs
Original file line number Diff line number Diff line change
Expand Up @@ -373,33 +373,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 @@ -412,25 +413,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