-
Notifications
You must be signed in to change notification settings - Fork 44
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
keepsimple1
merged 7 commits into
keepsimple1:main
from
CosminPerRam:feat/extract_pointer_find
Nov 24, 2024
Merged
Changes from 5 commits
Commits
Show all changes
7 commits
Select commit
Hold shift + click to select a range
016be96
extract, no mut and return refs, not clones
CosminPerRam 24ce727
fix filtering
CosminPerRam 7c5aa42
rename parameter
CosminPerRam 2fc86c6
chore: run cargo fmt
CosminPerRam ce228ea
chore: run cargo clippy
CosminPerRam b1231b9
restore extraction, use flat_map
CosminPerRam 3b242ae
fix filtering
CosminPerRam File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -373,64 +373,56 @@ 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 = Self::find_instance_names(&self.ptr, ty_domain, now); | ||
|
||
// 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); | ||
} | ||
} | ||
|
||
(refresh_due, new_timers) | ||
} | ||
|
||
fn find_instance_names<'a>( | ||
from: &'a HashMap<String, Vec<DnsRecordBox>>, | ||
ty_domain: &str, | ||
now: u64, | ||
) -> Vec<&'a String> { | ||
from.get(ty_domain) | ||
.into_iter() | ||
.flatten() | ||
.filter(|record| !record.get_record().is_expired(now)) | ||
.flat_map(|record| record.any().downcast_ref::<DnsPointer>()) | ||
.map(|ptr| &ptr.alias) | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. IMO,
|
||
.collect() | ||
} | ||
|
||
/// Returns the set of `host`, where refreshing the A / AAAA records is due | ||
/// for a `ty_domain`. | ||
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 = Self::find_instance_names(&self.ptr, ty_domain, now); | ||
|
||
// 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| { | ||
|
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I think this definition of a function seems arbitrary. I would opt for just changing the imperative style to the functional style in each place. (The apparent duplication is OK for such functional style, and it will save two function calls).
(also, if we did use a function, it would be better to take
&self
insteadfrom
, and useself.ptr
inside the function).There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Using
&self
instead offrom
would return a reference that would be tied to the entire self, and we cant do that without cloning the entire vector because we have a mutable reference here:mdns-sd/src/dns_cache.rs
Line 395 in d117f4f
So, instead, as you also mentioned, I reverted the extraction, and by such, this PR is just a perf improvement as it avoids many
.clone
calls.