@@ -3014,21 +3014,41 @@ macro_rules! iterator {
3014
3014
{ $( $mut_: tt ) * } ,
3015
3015
{ $( $extra: tt) * }
3016
3016
) => {
3017
+ // Returns the first element and moves the start of the iterator forwards by 1.
3018
+ // Greatly improves performance compared to an inlined function. The iterator
3019
+ // must not be empty.
3020
+ macro_rules! next_unchecked {
3021
+ ( $self: ident) => { & $( $mut_ ) * * $self. post_inc_start( 1 ) }
3022
+ }
3023
+
3024
+ // Returns the last element and moves the end of the iterator backwards by 1.
3025
+ // Greatly improves performance compared to an inlined function. The iterator
3026
+ // must not be empty.
3027
+ macro_rules! next_back_unchecked {
3028
+ ( $self: ident) => { & $( $mut_ ) * * $self. pre_dec_end( 1 ) }
3029
+ }
3030
+
3017
3031
impl <' a, T > $name<' a, T > {
3018
3032
// Helper function for creating a slice from the iterator.
3019
3033
#[ inline( always) ]
3020
3034
fn make_slice( & self ) -> & ' a [ T ] {
3021
3035
unsafe { from_raw_parts( self . ptr, len!( self ) ) }
3022
3036
}
3023
3037
3038
+ // Helper function for shrinking the iterator when T is a ZST, by moving the
3039
+ // end of the iterator backwards by `n`.
3040
+ #[ inline( always) ]
3041
+ fn zst_shrink( & mut self , n: isize ) {
3042
+ self . end = ( self . end as * $raw_mut u8 ) . wrapping_offset( -n) as * $raw_mut T ;
3043
+ }
3044
+
3024
3045
// Helper function for moving the start of the iterator forwards by `offset` elements,
3025
3046
// returning the old start.
3026
3047
// Unsafe because the offset must be in-bounds or one-past-the-end.
3027
3048
#[ inline( always) ]
3028
3049
unsafe fn post_inc_start( & mut self , offset: isize ) -> * $raw_mut T {
3029
3050
if mem:: size_of:: <T >( ) == 0 {
3030
- // This is *reducing* the length. `ptr` never changes with ZST.
3031
- self . end = ( self . end as * $raw_mut u8 ) . wrapping_offset( -offset) as * $raw_mut T ;
3051
+ self . zst_shrink( offset) ;
3032
3052
self . ptr
3033
3053
} else {
3034
3054
let old = self . ptr;
@@ -3043,7 +3063,7 @@ macro_rules! iterator {
3043
3063
#[ inline( always) ]
3044
3064
unsafe fn pre_dec_end( & mut self , offset: isize ) -> * $raw_mut T {
3045
3065
if mem:: size_of:: <T >( ) == 0 {
3046
- self . end = ( self . end as * $raw_mut u8 ) . wrapping_offset ( - offset) as * $raw_mut T ;
3066
+ self . zst_shrink ( offset) ;
3047
3067
self . ptr
3048
3068
} else {
3049
3069
self . end = self . end. offset( -offset) ;
@@ -3080,7 +3100,7 @@ macro_rules! iterator {
3080
3100
if is_empty!( self ) {
3081
3101
None
3082
3102
} else {
3083
- Some ( & $ ( $mut_ ) * * self . post_inc_start ( 1 ) )
3103
+ Some ( next_unchecked! ( self ) )
3084
3104
}
3085
3105
}
3086
3106
}
@@ -3109,11 +3129,10 @@ macro_rules! iterator {
3109
3129
}
3110
3130
return None ;
3111
3131
}
3112
- // We are in bounds. `offset ` does the right thing even for ZSTs.
3132
+ // We are in bounds. `post_inc_start ` does the right thing even for ZSTs.
3113
3133
unsafe {
3114
- let elem = Some ( & $( $mut_ ) * * self . ptr. add( n) ) ;
3115
- self . post_inc_start( ( n as isize ) . wrapping_add( 1 ) ) ;
3116
- elem
3134
+ self . post_inc_start( n as isize ) ;
3135
+ Some ( next_unchecked!( self ) )
3117
3136
}
3118
3137
}
3119
3138
@@ -3130,13 +3149,13 @@ macro_rules! iterator {
3130
3149
let mut accum = init;
3131
3150
unsafe {
3132
3151
while len!( self ) >= 4 {
3133
- accum = f( accum, & $ ( $mut_ ) * * self . post_inc_start ( 1 ) ) ?;
3134
- accum = f( accum, & $ ( $mut_ ) * * self . post_inc_start ( 1 ) ) ?;
3135
- accum = f( accum, & $ ( $mut_ ) * * self . post_inc_start ( 1 ) ) ?;
3136
- accum = f( accum, & $ ( $mut_ ) * * self . post_inc_start ( 1 ) ) ?;
3152
+ accum = f( accum, next_unchecked! ( self ) ) ?;
3153
+ accum = f( accum, next_unchecked! ( self ) ) ?;
3154
+ accum = f( accum, next_unchecked! ( self ) ) ?;
3155
+ accum = f( accum, next_unchecked! ( self ) ) ?;
3137
3156
}
3138
3157
while !is_empty!( self ) {
3139
- accum = f( accum, & $ ( $mut_ ) * * self . post_inc_start ( 1 ) ) ?;
3158
+ accum = f( accum, next_unchecked! ( self ) ) ?;
3140
3159
}
3141
3160
}
3142
3161
Try :: from_ok( accum)
@@ -3207,11 +3226,25 @@ macro_rules! iterator {
3207
3226
if is_empty!( self ) {
3208
3227
None
3209
3228
} else {
3210
- Some ( & $ ( $mut_ ) * * self . pre_dec_end ( 1 ) )
3229
+ Some ( next_back_unchecked! ( self ) )
3211
3230
}
3212
3231
}
3213
3232
}
3214
3233
3234
+ #[ inline]
3235
+ fn nth_back( & mut self , n: usize ) -> Option <$elem> {
3236
+ if n >= len!( self ) {
3237
+ // This iterator is now empty.
3238
+ self . end = self . ptr;
3239
+ return None ;
3240
+ }
3241
+ // We are in bounds. `pre_dec_end` does the right thing even for ZSTs.
3242
+ unsafe {
3243
+ self . pre_dec_end( n as isize ) ;
3244
+ Some ( next_back_unchecked!( self ) )
3245
+ }
3246
+ }
3247
+
3215
3248
#[ inline]
3216
3249
fn try_rfold<B , F , R >( & mut self , init: B , mut f: F ) -> R where
3217
3250
Self : Sized , F : FnMut ( B , Self :: Item ) -> R , R : Try <Ok =B >
@@ -3220,14 +3253,14 @@ macro_rules! iterator {
3220
3253
let mut accum = init;
3221
3254
unsafe {
3222
3255
while len!( self ) >= 4 {
3223
- accum = f( accum, & $ ( $mut_ ) * * self . pre_dec_end ( 1 ) ) ?;
3224
- accum = f( accum, & $ ( $mut_ ) * * self . pre_dec_end ( 1 ) ) ?;
3225
- accum = f( accum, & $ ( $mut_ ) * * self . pre_dec_end ( 1 ) ) ?;
3226
- accum = f( accum, & $ ( $mut_ ) * * self . pre_dec_end ( 1 ) ) ?;
3256
+ accum = f( accum, next_back_unchecked! ( self ) ) ?;
3257
+ accum = f( accum, next_back_unchecked! ( self ) ) ?;
3258
+ accum = f( accum, next_back_unchecked! ( self ) ) ?;
3259
+ accum = f( accum, next_back_unchecked! ( self ) ) ?;
3227
3260
}
3228
3261
// inlining is_empty everywhere makes a huge performance difference
3229
3262
while !is_empty!( self ) {
3230
- accum = f( accum, & $ ( $mut_ ) * * self . pre_dec_end ( 1 ) ) ?;
3263
+ accum = f( accum, next_back_unchecked! ( self ) ) ?;
3231
3264
}
3232
3265
}
3233
3266
Try :: from_ok( accum)
0 commit comments