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

Optimize Iterator implementation for &mut impl Iterator + Sized #111200

Merged
merged 1 commit into from
Aug 5, 2023
Merged
Show file tree
Hide file tree
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
6 changes: 6 additions & 0 deletions library/core/src/iter/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -361,6 +361,12 @@ macro_rules! impl_fold_via_try_fold {
(rfold -> try_rfold) => {
impl_fold_via_try_fold! { @internal rfold -> try_rfold }
};
(spec_fold -> spec_try_fold) => {
impl_fold_via_try_fold! { @internal spec_fold -> spec_try_fold }
};
(spec_rfold -> spec_try_rfold) => {
impl_fold_via_try_fold! { @internal spec_rfold -> spec_try_rfold }
};
(@internal $fold:ident -> $try_fold:ident) => {
#[inline]
fn $fold<AAA, FFF>(mut self, init: AAA, fold: FFF) -> AAA
Expand Down
62 changes: 62 additions & 0 deletions library/core/src/iter/traits/double_ended.rs
Original file line number Diff line number Diff line change
Expand Up @@ -379,4 +379,66 @@ impl<'a, I: DoubleEndedIterator + ?Sized> DoubleEndedIterator for &'a mut I {
fn nth_back(&mut self, n: usize) -> Option<I::Item> {
(**self).nth_back(n)
}
fn rfold<B, F>(self, init: B, f: F) -> B
where
F: FnMut(B, Self::Item) -> B,
{
self.spec_rfold(init, f)
}
fn try_rfold<B, F, R>(&mut self, init: B, f: F) -> R
where
F: FnMut(B, Self::Item) -> R,
R: Try<Output = B>,
{
self.spec_try_rfold(init, f)
}
}

/// Helper trait to specialize `rfold` and `rtry_fold` for `&mut I where I: Sized`
trait DoubleEndedIteratorRefSpec: DoubleEndedIterator {
fn spec_rfold<B, F>(self, init: B, f: F) -> B
where
F: FnMut(B, Self::Item) -> B;

fn spec_try_rfold<B, F, R>(&mut self, init: B, f: F) -> R
where
F: FnMut(B, Self::Item) -> R,
R: Try<Output = B>;
}

impl<I: DoubleEndedIterator + ?Sized> DoubleEndedIteratorRefSpec for &mut I {
default fn spec_rfold<B, F>(self, init: B, mut f: F) -> B
where
F: FnMut(B, Self::Item) -> B,
{
let mut accum = init;
while let Some(x) = self.next_back() {
accum = f(accum, x);
}
accum
}

default fn spec_try_rfold<B, F, R>(&mut self, init: B, mut f: F) -> R
where
F: FnMut(B, Self::Item) -> R,
R: Try<Output = B>,
{
let mut accum = init;
while let Some(x) = self.next_back() {
accum = f(accum, x)?;
}
try { accum }
}
}

impl<I: DoubleEndedIterator> DoubleEndedIteratorRefSpec for &mut I {
impl_fold_via_try_fold! { spec_rfold -> spec_try_rfold }

fn spec_try_rfold<B, F, R>(&mut self, init: B, f: F) -> R
where
F: FnMut(B, Self::Item) -> R,
R: Try<Output = B>,
{
(**self).try_rfold(init, f)
}
}
62 changes: 62 additions & 0 deletions library/core/src/iter/traits/iterator.rs
Original file line number Diff line number Diff line change
Expand Up @@ -4018,4 +4018,66 @@ impl<I: Iterator + ?Sized> Iterator for &mut I {
fn nth(&mut self, n: usize) -> Option<Self::Item> {
(**self).nth(n)
}
fn fold<B, F>(self, init: B, f: F) -> B
Copy link
Member

Choose a reason for hiding this comment

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

Copy link
Member

@the8472 the8472 Aug 5, 2023

Choose a reason for hiding this comment

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

Mostly we're getting closer, but the #[inline]s still differ so that would affect performance and the tradeoffs aren't straight-forward.

where
F: FnMut(B, Self::Item) -> B,
{
self.spec_fold(init, f)
}
fn try_fold<B, F, R>(&mut self, init: B, f: F) -> R
where
F: FnMut(B, Self::Item) -> R,
R: Try<Output = B>,
{
self.spec_try_fold(init, f)
}
}

/// Helper trait to specialize `fold` and `try_fold` for `&mut I where I: Sized`
trait IteratorRefSpec: Iterator {
fn spec_fold<B, F>(self, init: B, f: F) -> B
where
F: FnMut(B, Self::Item) -> B;

fn spec_try_fold<B, F, R>(&mut self, init: B, f: F) -> R
where
F: FnMut(B, Self::Item) -> R,
R: Try<Output = B>;
}

impl<I: Iterator + ?Sized> IteratorRefSpec for &mut I {
default fn spec_fold<B, F>(self, init: B, mut f: F) -> B
where
F: FnMut(B, Self::Item) -> B,
{
let mut accum = init;
while let Some(x) = self.next() {
accum = f(accum, x);
}
accum
}

default fn spec_try_fold<B, F, R>(&mut self, init: B, mut f: F) -> R
where
F: FnMut(B, Self::Item) -> R,
R: Try<Output = B>,
{
let mut accum = init;
while let Some(x) = self.next() {
accum = f(accum, x)?;
}
try { accum }
}
}

impl<I: Iterator> IteratorRefSpec for &mut I {
impl_fold_via_try_fold! { spec_fold -> spec_try_fold }

fn spec_try_fold<B, F, R>(&mut self, init: B, f: F) -> R
where
F: FnMut(B, Self::Item) -> R,
R: Try<Output = B>,
{
(**self).try_fold(init, f)
}
}