-
Notifications
You must be signed in to change notification settings - Fork 43
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
perf: avoid cloning in filtering ptr #272
Conversation
src/dns_cache.rs
Outdated
.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 comment
The reason will be displayed to describe this comment to others. Learn more.
IMO, flat_map
is harder to understand than filter_map
, hence I think probably combine flat_map
and map
into the following, would be a bit easier to undertand:
.filter_map(|record| {
record.any()
.downcast_ref::<DnsPointer>()
.map(|ptr| &ptr.alias)
})
src/dns_cache.rs
Outdated
from: &'a HashMap<String, Vec<DnsRecordBox>>, | ||
ty_domain: &str, | ||
now: u64, | ||
) -> Vec<&'a String> { |
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
instead from
, and use self.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 of from
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:
Line 395 in d117f4f
.get_mut(&instance) |
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.
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.
Thanks for your PR and update! LGTM!
Extracts the filtering of instance names of a, the replaced code does not clone all the names, instead returns references to it (this is a perf improvement, as now they are being cloned only on demand (in checking the SRV records)) and I find this to be more concise and declarative, thoughts?ty_domain
in a function as it was used in 2 places exactly the sameTests pass on MacOS 14.5.