@@ -3029,7 +3029,7 @@ fn contains_nonascii(v: usize) -> bool {
3029
3029
///
3030
3030
/// - Read the first word with an unaligned load.
3031
3031
/// - Align the pointer, read subsequent words until end with aligned loads.
3032
- /// - If there's a tail, the last `usize` from `s` with an unaligned load.
3032
+ /// - Read the last `usize` from `s` with an unaligned load.
3033
3033
///
3034
3034
/// If any of these loads produces something for which `contains_nonascii`
3035
3035
/// (above) returns true, then we know the answer is false.
@@ -3077,7 +3077,10 @@ fn is_ascii(s: &[u8]) -> bool {
3077
3077
// `align_offset` though.
3078
3078
debug_assert_eq ! ( ( word_ptr as usize ) % mem:: align_of:: <usize >( ) , 0 ) ;
3079
3079
3080
- while byte_pos <= len - USIZE_SIZE {
3080
+ // Read subsequent words until the last aligned word, excluding the last
3081
+ // aligned word by itself to be done in tail check later, to ensure that
3082
+ // tail is always one `usize` at most to extra branch `byte_pos == len`.
3083
+ while byte_pos < len - USIZE_SIZE {
3081
3084
debug_assert ! (
3082
3085
// Sanity check that the read is in bounds
3083
3086
( word_ptr as usize + USIZE_SIZE ) <= ( start. wrapping_add( len) as usize ) &&
@@ -3098,15 +3101,9 @@ fn is_ascii(s: &[u8]) -> bool {
3098
3101
word_ptr = unsafe { word_ptr. add ( 1 ) } ;
3099
3102
}
3100
3103
3101
- // If we have anything left over, it should be at-most 1 usize worth of bytes,
3102
- // which we check with a read_unaligned.
3103
- if byte_pos == len {
3104
- return true ;
3105
- }
3106
-
3107
3104
// Sanity check to ensure there really is only one `usize` left. This should
3108
3105
// be guaranteed by our loop condition.
3109
- debug_assert ! ( byte_pos < len && len - byte_pos < USIZE_SIZE ) ;
3106
+ debug_assert ! ( byte_pos <= len && len - byte_pos <= USIZE_SIZE ) ;
3110
3107
3111
3108
// SAFETY: This relies on `len >= USIZE_SIZE`, which we check at the start.
3112
3109
let last_word = unsafe { ( start. add ( len - USIZE_SIZE ) as * const usize ) . read_unaligned ( ) } ;
0 commit comments