File tree 1 file changed +23
-0
lines changed
library/core/src/slice/iter
1 file changed +23
-0
lines changed Original file line number Diff line number Diff line change @@ -191,6 +191,29 @@ macro_rules! iterator {
191
191
self . next_back( )
192
192
}
193
193
194
+ #[ inline]
195
+ fn fold<B , F >( mut self , init: B , mut f: F ) -> B
196
+ where
197
+ F : FnMut ( B , Self :: Item ) -> B ,
198
+ {
199
+ // Handling the 0-len case explicitly and then using a do-while style loop
200
+ // helps the optimizer. See issue #106288
201
+ if is_empty!( self ) {
202
+ return init;
203
+ }
204
+ let mut acc = init;
205
+ // SAFETY: The 0-len case was handled above so one loop iteration is guaranteed.
206
+ unsafe {
207
+ loop {
208
+ acc = f( acc, next_unchecked!( self ) ) ;
209
+ if is_empty!( self ) {
210
+ break ;
211
+ }
212
+ }
213
+ }
214
+ acc
215
+ }
216
+
194
217
// We override the default implementation, which uses `try_fold`,
195
218
// because this simple implementation generates less LLVM IR and is
196
219
// faster to compile.
You can’t perform that action at this time.
0 commit comments