Skip to content

Commit 8be3622

Browse files
committed
Auto merge of #64600 - scottmcm:no-slice-tryfold-unroll, r=<try>
Remove manual unrolling from slice::Iter(Mut)::try_fold While this definitely helps sometimes (particularly for trivial closures), it's also a pessimization sometimes, so it's better to leave this to (hypothetical) future LLVM improvements instead of forcing this on everyone. I think it's better for the advice to be that sometimes you need to unroll manually than you sometimes need to not-unroll manually (like #64545). --- For context see #64572 (comment)
2 parents 66bf391 + 6ac64ab commit 8be3622

File tree

1 file changed

+1
-68
lines changed

1 file changed

+1
-68
lines changed

src/libcore/slice/mod.rs

+1-68
Original file line numberDiff line numberDiff line change
@@ -28,7 +28,7 @@ use crate::fmt;
2828
use crate::intrinsics::{assume, exact_div, unchecked_sub, is_aligned_and_not_null};
2929
use crate::isize;
3030
use crate::iter::*;
31-
use crate::ops::{FnMut, Try, self};
31+
use crate::ops::{FnMut, self};
3232
use crate::option::Option;
3333
use crate::option::Option::{None, Some};
3434
use crate::result::Result;
@@ -3180,39 +3180,6 @@ macro_rules! iterator {
31803180
self.next_back()
31813181
}
31823182

3183-
#[inline]
3184-
fn try_fold<B, F, R>(&mut self, init: B, mut f: F) -> R where
3185-
Self: Sized, F: FnMut(B, Self::Item) -> R, R: Try<Ok=B>
3186-
{
3187-
// manual unrolling is needed when there are conditional exits from the loop
3188-
let mut accum = init;
3189-
unsafe {
3190-
while len!(self) >= 4 {
3191-
accum = f(accum, next_unchecked!(self))?;
3192-
accum = f(accum, next_unchecked!(self))?;
3193-
accum = f(accum, next_unchecked!(self))?;
3194-
accum = f(accum, next_unchecked!(self))?;
3195-
}
3196-
while !is_empty!(self) {
3197-
accum = f(accum, next_unchecked!(self))?;
3198-
}
3199-
}
3200-
Try::from_ok(accum)
3201-
}
3202-
3203-
#[inline]
3204-
fn fold<Acc, Fold>(mut self, init: Acc, mut f: Fold) -> Acc
3205-
where Fold: FnMut(Acc, Self::Item) -> Acc,
3206-
{
3207-
// Let LLVM unroll this, rather than using the default
3208-
// impl that would force the manual unrolling above
3209-
let mut accum = init;
3210-
while let Some(x) = self.next() {
3211-
accum = f(accum, x);
3212-
}
3213-
accum
3214-
}
3215-
32163183
#[inline]
32173184
#[rustc_inherit_overflow_checks]
32183185
fn position<P>(&mut self, mut predicate: P) -> Option<usize> where
@@ -3283,40 +3250,6 @@ macro_rules! iterator {
32833250
Some(next_back_unchecked!(self))
32843251
}
32853252
}
3286-
3287-
#[inline]
3288-
fn try_rfold<B, F, R>(&mut self, init: B, mut f: F) -> R where
3289-
Self: Sized, F: FnMut(B, Self::Item) -> R, R: Try<Ok=B>
3290-
{
3291-
// manual unrolling is needed when there are conditional exits from the loop
3292-
let mut accum = init;
3293-
unsafe {
3294-
while len!(self) >= 4 {
3295-
accum = f(accum, next_back_unchecked!(self))?;
3296-
accum = f(accum, next_back_unchecked!(self))?;
3297-
accum = f(accum, next_back_unchecked!(self))?;
3298-
accum = f(accum, next_back_unchecked!(self))?;
3299-
}
3300-
// inlining is_empty everywhere makes a huge performance difference
3301-
while !is_empty!(self) {
3302-
accum = f(accum, next_back_unchecked!(self))?;
3303-
}
3304-
}
3305-
Try::from_ok(accum)
3306-
}
3307-
3308-
#[inline]
3309-
fn rfold<Acc, Fold>(mut self, init: Acc, mut f: Fold) -> Acc
3310-
where Fold: FnMut(Acc, Self::Item) -> Acc,
3311-
{
3312-
// Let LLVM unroll this, rather than using the default
3313-
// impl that would force the manual unrolling above
3314-
let mut accum = init;
3315-
while let Some(x) = self.next_back() {
3316-
accum = f(accum, x);
3317-
}
3318-
accum
3319-
}
33203253
}
33213254

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

0 commit comments

Comments
 (0)