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

Remove manual unrolling from slice::Iter(Mut)::try_fold #64600

Merged
merged 2 commits into from
Sep 30, 2019
Merged
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
69 changes: 1 addition & 68 deletions src/libcore/slice/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,7 @@ use crate::fmt;
use crate::intrinsics::{assume, exact_div, unchecked_sub, is_aligned_and_not_null};
use crate::isize;
use crate::iter::*;
use crate::ops::{FnMut, Try, self};
use crate::ops::{FnMut, self};
use crate::option::Option;
use crate::option::Option::{None, Some};
use crate::result::Result;
Expand Down Expand Up @@ -3180,39 +3180,6 @@ macro_rules! iterator {
self.next_back()
}

#[inline]
fn try_fold<B, F, R>(&mut self, init: B, mut f: F) -> R where
Self: Sized, F: FnMut(B, Self::Item) -> R, R: Try<Ok=B>
{
// manual unrolling is needed when there are conditional exits from the loop
let mut accum = init;
unsafe {
while len!(self) >= 4 {
accum = f(accum, next_unchecked!(self))?;
accum = f(accum, next_unchecked!(self))?;
accum = f(accum, next_unchecked!(self))?;
accum = f(accum, next_unchecked!(self))?;
}
while !is_empty!(self) {
accum = f(accum, next_unchecked!(self))?;
}
}
Try::from_ok(accum)
}

#[inline]
fn fold<Acc, Fold>(mut self, init: Acc, mut f: Fold) -> Acc
where Fold: FnMut(Acc, Self::Item) -> Acc,
{
// Let LLVM unroll this, rather than using the default
// impl that would force the manual unrolling above
let mut accum = init;
while let Some(x) = self.next() {
accum = f(accum, x);
}
accum
}

#[inline]
#[rustc_inherit_overflow_checks]
fn position<P>(&mut self, mut predicate: P) -> Option<usize> where
Expand Down Expand Up @@ -3283,40 +3250,6 @@ macro_rules! iterator {
Some(next_back_unchecked!(self))
}
}

#[inline]
fn try_rfold<B, F, R>(&mut self, init: B, mut f: F) -> R where
Self: Sized, F: FnMut(B, Self::Item) -> R, R: Try<Ok=B>
{
// manual unrolling is needed when there are conditional exits from the loop
let mut accum = init;
unsafe {
while len!(self) >= 4 {
accum = f(accum, next_back_unchecked!(self))?;
accum = f(accum, next_back_unchecked!(self))?;
accum = f(accum, next_back_unchecked!(self))?;
accum = f(accum, next_back_unchecked!(self))?;
}
// inlining is_empty everywhere makes a huge performance difference
while !is_empty!(self) {
accum = f(accum, next_back_unchecked!(self))?;
}
}
Try::from_ok(accum)
}

#[inline]
fn rfold<Acc, Fold>(mut self, init: Acc, mut f: Fold) -> Acc
where Fold: FnMut(Acc, Self::Item) -> Acc,
{
// Let LLVM unroll this, rather than using the default
// impl that would force the manual unrolling above
let mut accum = init;
while let Some(x) = self.next_back() {
accum = f(accum, x);
}
accum
}
}

#[stable(feature = "fused", since = "1.26.0")]
Expand Down