@@ -51,6 +51,28 @@ fn random_data(size: i32) -> Vec<u8> {
5151 buf
5252}
5353
54+ fn create_aligned_data ( input : & [ u8 ] ) -> Vec < u8 > {
55+ // Size of our target alignment structure
56+ let align_size = std:: mem:: size_of :: < [ [ u64 ; 4 ] ; 2 ] > ( ) ; // 64 bytes
57+
58+ // Create a vector with padding to ensure we can find a properly aligned position
59+ let mut padded = Vec :: with_capacity ( input. len ( ) + align_size) ;
60+
61+ // Fill with zeros initially to reach needed capacity
62+ padded. resize ( input. len ( ) + align_size, 0 ) ;
63+
64+ // Find the first address that satisfies our alignment
65+ let start_addr = padded. as_ptr ( ) as usize ;
66+ let align_offset = ( align_size - ( start_addr % align_size) ) % align_size;
67+
68+ // Copy the input into the aligned position
69+ let aligned_start = & mut padded[ align_offset..] ;
70+ aligned_start[ ..input. len ( ) ] . copy_from_slice ( input) ;
71+
72+ // Return the exact slice we need
73+ aligned_start[ ..input. len ( ) ] . to_vec ( )
74+ }
75+
5476#[ inline( always) ]
5577fn bench_crc32 ( c : & mut Criterion ) {
5678 let mut group = c. benchmark_group ( "CRC-32" ) ;
@@ -65,7 +87,7 @@ fn bench_crc32(c: &mut Criterion) {
6587 ) ;
6688
6789 for ( size_name, size) in SIZES {
68- let buf = random_data ( * size) ;
90+ let buf = create_aligned_data ( & * random_data ( * size) ) ;
6991
7092 let ( part1, rest) = buf. split_at ( buf. len ( ) / 4 ) ;
7193 let ( part2, rest) = rest. split_at ( rest. len ( ) / 3 ) ;
@@ -108,7 +130,7 @@ fn bench_crc64(c: &mut Criterion) {
108130 let mut group = c. benchmark_group ( "CRC-64" ) ;
109131
110132 for ( size_name, size) in SIZES {
111- let buf = random_data ( * size) ;
133+ let buf = create_aligned_data ( & * random_data ( * size) ) ;
112134
113135 let ( part1, rest) = buf. split_at ( buf. len ( ) / 4 ) ;
114136 let ( part2, rest) = rest. split_at ( rest. len ( ) / 3 ) ;
@@ -122,6 +144,8 @@ fn bench_crc64(c: &mut Criterion) {
122144
123145 group. throughput ( Throughput :: Bytes ( * size as u64 ) ) ;
124146
147+ group. measurement_time ( Duration :: from_secs ( 60 ) ) ;
148+
125149 let bench_name = [ alg_suffix. unwrap ( ) , "(checksum)" ] . join ( " " ) ;
126150
127151 group. bench_function ( BenchmarkId :: new ( bench_name, size_name) , |b| {
0 commit comments