Debian Codegraph @

debian/rust-vendor/crc-fast/benches/benchmark.rs

raw
1 // Copyright 2025 Don MacAskill. Licensed under MIT or Apache-2.0.
2
3 use crc_fast::checksum;
4 use crc_fast::CrcAlgorithm;
5 use criterion::*;
6 use rand::{rng, RngCore};
7 use std::hint::black_box;
8 use std::time::Duration;
9
10 pub const SIZES: &[(&str, i32)] = &[
11 ("1 MiB", 1024 * 1024),
12 //("512 KiB", 512 * 1024),
13 //("256 KiB", 256 * 1024),
14 //("128 KiB", 128 * 1024),
15 //("64 KiB", 64 * 1024),
16 //("16 KiB", 16 * 1024),
17 //("8 KiB", 8 * 1024),
18 //("4 KiB", 4 * 1024),
19 //("2 KiB", 4 * 1024),
20 ("1 KiB", 1024),
21 //("768 bytes", 768),
22 //("512 bytes", 512),
23 //("256 bytes", 256),
24 //("255 bytes", 255),
25 //("128 bytes", 128),
26 //("127 bytes", 127),
27 //("64 bytes", 64),
28 //("32 bytes", 32),
29 //("16 bytes", 16),
30 //("1 bytes", 1),
31 ];
32
33 // these are the most important algorithms in popular use, with forward/reflected coverage
34 pub const CRC32_ALGORITHMS: &[CrcAlgorithm] = &[
35 // benchmark both CRC-32/ISCSI and CRC-32/ISO-HDLC since they're special flowers with lots of
36 // different acceleration targets.
37 CrcAlgorithm::Crc32Autosar, // reflected
38 CrcAlgorithm::Crc32Iscsi, // reflected, fusion
39 CrcAlgorithm::Crc32IsoHdlc, // reflected, fusion
40 CrcAlgorithm::Crc32Bzip2, // forward
41 ];
42
43 // these are the most important algorithms in popular use, with forward/reflected coverage
44 pub const CRC64_ALGORITHMS: &[CrcAlgorithm] = &[
45 CrcAlgorithm::Crc64Ecma182, // forward
46 CrcAlgorithm::Crc64Nvme, // reflected
47 ];
48
49 #[inline(always)]
50 fn random_data(size: i32) -> Vec<u8> {
51 let mut rng = rng();
52 let mut buf = vec![0u8; size as usize];
53 rng.fill_bytes(&mut buf);
54
55 buf
56 }
57
58 fn create_aligned_data(input: &[u8]) -> Vec<u8> {
59 // Size of our target alignment structure
60 let align_size = std::mem::size_of::<[[u64; 4]; 2]>(); // 64 bytes
61
62 // Create a zero-filled vector with padding to ensure we can find a properly aligned position
63 let mut padded = vec![0; input.len() + align_size];
64
65 // Find the first address that satisfies our alignment
66 let start_addr = padded.as_ptr() as usize;
67 let align_offset = (align_size - (start_addr % align_size)) % align_size;
68
69 // Copy the input into the aligned position
70 let aligned_start = &mut padded[align_offset..];
71 aligned_start[..input.len()].copy_from_slice(input);
72
73 // Return the exact slice we need
74 aligned_start[..input.len()].to_vec()
75 }
76
77 #[inline(always)]
78 fn bench_crc32(c: &mut Criterion) {
79 let mut group = c.benchmark_group("CRC-32");
80
81 println!(
82 "Acceleration target: {}",
83 crc_fast::get_calculator_target(CrcAlgorithm::Crc32Iscsi)
84 );
85
86 for (size_name, size) in SIZES {
87 let buf = create_aligned_data(&random_data(*size));
88
89 let (part1, rest) = buf.split_at(buf.len() / 4);
90 let (part2, rest) = rest.split_at(rest.len() / 3);
91 let (part3, part4) = rest.split_at(rest.len() / 2);
92
93 for algorithm in CRC32_ALGORITHMS {
94 let algorithm_name = algorithm.to_string();
95 let mut algorithm_name_parts = algorithm_name.split('/');
96 let _ = algorithm_name_parts.next();
97 let alg_suffix = algorithm_name_parts.next();
98
99 group.throughput(Throughput::Bytes(*size as u64));
100 group.sample_size(1000);
101 group.measurement_time(Duration::from_secs(30));
102
103 let bench_name = [alg_suffix.unwrap(), "(checksum)"].join(" ");
104
105 group.bench_function(BenchmarkId::new(bench_name, size_name), |b| {
106 b.iter(|| black_box(checksum(*algorithm, &buf)))
107 });
108
109 let bench_name = [algorithm_name.clone(), "(4-part digest)".parse().unwrap()].join(" ");
110
111 group.bench_function(BenchmarkId::new(bench_name, size_name), |b| {
112 b.iter(|| {
113 black_box({
114 let mut digest = crc_fast::Digest::new(*algorithm);
115 digest.update(part1);
116 digest.update(part2);
117 digest.update(part3);
118 digest.update(part4);
119 digest.finalize()
120 })
121 })
122 });
123 }
124 }
125 }
126
127 #[inline(always)]
128 fn bench_crc64(c: &mut Criterion) {
129 println!(
130 "Acceleration target: {}",
131 crc_fast::get_calculator_target(CrcAlgorithm::Crc64Nvme)
132 );
133
134 let mut group = c.benchmark_group("CRC-64");
135
136 for (size_name, size) in SIZES {
137 let buf = create_aligned_data(&random_data(*size));
138
139 let (part1, rest) = buf.split_at(buf.len() / 4);
140 let (part2, rest) = rest.split_at(rest.len() / 3);
141 let (part3, part4) = rest.split_at(rest.len() / 2);
142
143 for algorithm in CRC64_ALGORITHMS {
144 let algorithm_name = algorithm.to_string();
145 let mut algorithm_name_parts = algorithm_name.split('/');
146 let _ = algorithm_name_parts.next();
147 let alg_suffix = algorithm_name_parts.next();
148
149 group.throughput(Throughput::Bytes(*size as u64));
150 group.sample_size(1000);
151 group.measurement_time(Duration::from_secs(30));
152
153 let bench_name = [alg_suffix.unwrap(), "(checksum)"].join(" ");
154
155 group.bench_function(BenchmarkId::new(bench_name, size_name), |b| {
156 b.iter(|| black_box(checksum(*algorithm, &buf)))
157 });
158
159 let bench_name = [algorithm_name.clone(), "(4-part digest)".parse().unwrap()].join(" ");
160
161 group.bench_function(BenchmarkId::new(bench_name, size_name), |b| {
162 b.iter(|| {
163 black_box({
164 let mut digest = crc_fast::Digest::new(*algorithm);
165 digest.update(part1);
166 digest.update(part2);
167 digest.update(part3);
168 digest.update(part4);
169 digest.finalize()
170 })
171 })
172 });
173 }
174 }
175 }
176
177 criterion_group!(benches, bench_crc32, bench_crc64);
178
179 criterion_main!(benches);