@@ -65,6 +65,23 @@ macro_rules! benches {
65
65
benches!( @ro mod short_readonly SHORT $( $name $arg $body) +) ;
66
66
benches!( @ro mod medium_readonly MEDIUM $( $name $arg $body) +) ;
67
67
benches!( @ro mod long_readonly LONG $( $name $arg $body) +) ;
68
+ // Add another `MEDIUM` bench, but trim the ends so that we can (try to)
69
+ // benchmark a case where the function has to handle misalignment.
70
+ mod medium_unaligned {
71
+ use super :: * ;
72
+ $(
73
+ #[ bench]
74
+ fn $name( bencher: & mut Bencher ) {
75
+ bencher. bytes = MEDIUM . len( ) as u64 - 2 ;
76
+ let mut vec = MEDIUM . as_bytes( ) . to_vec( ) ;
77
+ bencher. iter( || {
78
+ black_box( & mut vec) ;
79
+ let $arg = black_box( & vec[ 1 ..( vec. len( ) - 1 ) ] ) ;
80
+ black_box( $body)
81
+ } )
82
+ }
83
+ ) +
84
+ }
68
85
} ;
69
86
( @ro mod $mod_name: ident $input: ident $( $name: ident $arg: ident $body: block) +) => {
70
87
mod $mod_name {
@@ -291,10 +308,11 @@ fn is_ascii_align_to_impl(bytes: &[u8]) -> bool {
291
308
if bytes. len ( ) < core:: mem:: size_of :: < usize > ( ) {
292
309
return bytes. iter ( ) . all ( |b| b. is_ascii ( ) ) ;
293
310
}
311
+ // SAFETY: transmuting a sequence of `u8` to `usize` is always fine
294
312
let ( head, body, tail) = unsafe { bytes. align_to :: < usize > ( ) } ;
295
- head. iter ( ) . all ( |b| b. is_ascii ( ) ) &&
296
- body. iter ( ) . all ( |w| !contains_nonascii ( * w) ) &&
297
- tail. iter ( ) . all ( |b| b. is_ascii ( ) )
313
+ head. iter ( ) . all ( |b| b. is_ascii ( ) )
314
+ && body. iter ( ) . all ( |w| !contains_nonascii ( * w) )
315
+ && tail. iter ( ) . all ( |b| b. is_ascii ( ) )
298
316
}
299
317
300
318
#[ inline]
0 commit comments