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

Conversation

CosminPerRam
Copy link
Contributor

@CosminPerRam CosminPerRam commented Nov 19, 2024

Extracts the filtering of instance names of a ty_domain in a function as it was used in 2 places exactly the same, 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?

Tests pass on MacOS 14.5.

@CosminPerRam CosminPerRam changed the title extract, no mut and return refs, not clones feat: extract filtering of instance names Nov 19, 2024
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)
Copy link
Owner

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> {
Copy link
Owner

@keepsimple1 keepsimple1 Nov 20, 2024

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).

Copy link
Contributor Author

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:

.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.

@CosminPerRam CosminPerRam changed the title feat: extract filtering of instance names perf: avoid cloning in filtering ptr Nov 23, 2024
Copy link
Owner

@keepsimple1 keepsimple1 left a 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!

@keepsimple1 keepsimple1 merged commit 7f6c5e9 into keepsimple1:main Nov 24, 2024
3 checks passed
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

2 participants