-
Notifications
You must be signed in to change notification settings - Fork 13.3k
optimize slice::Iter::fold #106343
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 slice::Iter::fold #106343
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -191,6 +191,39 @@ macro_rules! iterator { | |
self.next_back() | ||
} | ||
|
||
#[inline] | ||
fn fold<B, F>(self, init: B, mut f: F) -> B | ||
where | ||
F: FnMut(B, Self::Item) -> B, | ||
{ | ||
// this implementation consists of the following optimizations compared to the | ||
// default implementation: | ||
// - do-while loop, as is llvm's preferred loop shape, | ||
// see https://releases.llvm.org/16.0.0/docs/LoopTerminology.html#more-canonical-loops | ||
// - bumps an index instead of a pointer since the latter case inhibits | ||
// some optimizations, see #111603 | ||
// - avoids Option wrapping/matching | ||
if is_empty!(self) { | ||
return init; | ||
} | ||
let mut acc = init; | ||
let mut i = 0; | ||
let len = len!(self); | ||
loop { | ||
// SAFETY: the loop iterates `i in 0..len`, which always is in bounds of | ||
// the slice allocation | ||
acc = f(acc, unsafe { & $( $mut_ )? *self.ptr.add(i).as_ptr() }); | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. How much of the duplication here is essential? Avoiding the for _ in 0..len!(self) {
acc = f(acc, next_unchecked!(self));
} Or the same with a And if that's not sufficient, it would be nice to have some comments here about why it's written this way. (Also, if overloading @rustbot author There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. If you look at the PR history you'll see that I tried several approaches. A for-in-range loop was one of them. I even used
Ok, will do.
Maybe, but that'd be optimizing two things at once which makes assessing perf more complicated. There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Added a comment. @rustbot ready |
||
// SAFETY: `i` can't overflow since it'll only reach usize::MAX if the | ||
// slice had that length, in which case we'll break out of the loop | ||
// after the increment | ||
i = unsafe { i.unchecked_add(1) }; | ||
if i == len { | ||
break; | ||
} | ||
} | ||
acc | ||
} | ||
|
||
// We override the default implementation, which uses `try_fold`, | ||
// because this simple implementation generates less LLVM IR and is | ||
// faster to compile. | ||
|
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,14 @@ | ||
// ignore-debug: the debug assertions get in the way | ||
// compile-flags: -O | ||
// min-llvm-version: 16 | ||
#![crate_type = "lib"] | ||
|
||
// CHECK-LABEL: @slice_fold_to_last | ||
#[no_mangle] | ||
pub fn slice_fold_to_last(slice: &[i32]) -> Option<&i32> { | ||
// CHECK-NOT: loop | ||
// CHECK-NOT: br | ||
// CHECK-NOT: call | ||
// CHECK: ret | ||
slice.iter().fold(None, |_, i| Some(i)) | ||
} |
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -37,14 +37,6 @@ pub fn issue71861(vec: Vec<u32>) -> Box<[u32]> { | |
// CHECK-LABEL: @issue75636 | ||
#[no_mangle] | ||
pub fn issue75636<'a>(iter: &[&'a str]) -> Box<[&'a str]> { | ||
// CHECK-NOT: panic | ||
|
||
// Call to panic_cannot_unwind in case of double-panic is expected, | ||
// on LLVM 16 and older, but other panics are not. | ||
// old: filter | ||
// old-NEXT: ; call core::panicking::panic_cannot_unwind | ||
// old-NEXT: panic_cannot_unwind | ||
|
||
Comment on lines
-40
to
-47
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Now there panic? Or why this was killed. |
||
// CHECK-NOT: panic | ||
iter.iter().copied().collect() | ||
} | ||
|
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
It's very weird to me that doing this manually is needed, since LLVM has a loop rotation pass to do this. But I guess it's fine.