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

Specialize RepeatN::[r]fold #821

Merged
merged 2 commits into from Dec 15, 2023

Conversation

Philippe-Cholet
Copy link
Member

@Philippe-Cholet Philippe-Cholet commented Dec 14, 2023

Related to #755

cargo bench --bench specializations "repeat_n/r?fold"

repeat_n/fold           time:   [550.53 ns 553.84 ns 557.21 ns]
repeat_n/fold           time:   [267.32 ns 268.75 ns 270.78 ns]
                        change: [-52.797% -51.769% -50.705%]

repeat_n/rfold          time:   [546.41 ns 547.81 ns 549.27 ns]
repeat_n/rfold          time:   [268.31 ns 269.45 ns 271.03 ns]
                        change: [-51.214% -50.990% -50.727%]

@Philippe-Cholet Philippe-Cholet added this to the next milestone Dec 14, 2023
Copy link
Member

@phimuemue phimuemue left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Thanks.

Self { elt: Some(elt), n } => {
debug_assert!(n > 0);
init = (1..n).map(|_| elt.clone()).fold(init, &mut f);
f(init, elt)
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Avoiding the clone. Nice.

F: FnMut(B, Self::Item) -> B,
{
match self {
Self { elt: Some(elt), n } => {
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Here, we take elt as reference if there's still work to do, whereas fn next uses self.n > 1. Should we canonicalize?

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I'm not sure there is a real benefit to win here. Feel free to investigate.

@phimuemue phimuemue added this pull request to the merge queue Dec 15, 2023
Merged via the queue into rust-itertools:master with commit db0311c Dec 15, 2023
8 checks passed
@Philippe-Cholet Philippe-Cholet deleted the fold-repeat-n branch December 15, 2023 15:24
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.

None yet

2 participants