| 1 | use criterion::*; |
| 2 | use rand::prelude::*; |
| 3 | use triple_accel::*; |
| 4 | use triple_accel::levenshtein::*; |
| 5 | use triple_accel::hamming::*; |
| 6 | |
| 7 | fn bench_rand_hamming(c: &mut Criterion) { |
| 8 | let mut rng = StdRng::seed_from_u64(1234); |
| 9 | let mut group = c.benchmark_group("bench_rand_hamming"); |
| 10 | let config = PlotConfiguration::default().summary_scale(AxisScale::Logarithmic); |
| 11 | group.plot_config(config); |
| 12 | |
| 13 | for str_len in [10, 100, 1000].iter() { |
| 14 | let k = black_box(((*str_len) as u32) / 10); |
| 15 | let (a_str, b_str) = black_box(rand_hamming_pair(*str_len, k, &mut rng)); |
| 16 | |
| 17 | let res = hamming_naive(&a_str, &b_str); |
| 18 | assert!(res == hamming_words_64(&a_str, &b_str)); |
| 19 | assert!(res == hamming_words_128(&a_str, &b_str)); |
| 20 | assert!(res == hamming_simd_movemask(&a_str, &b_str)); |
| 21 | assert!(res == hamming_simd_parallel(&a_str, &b_str)); |
| 22 | |
| 23 | group.bench_function(BenchmarkId::new("hamming_naive", *str_len), |b| b.iter(|| hamming_naive(&a_str, &b_str))); |
| 24 | group.bench_function(BenchmarkId::new("hamming_words_64", *str_len), |b| b.iter(|| hamming_words_64(&a_str, &b_str))); |
| 25 | group.bench_function(BenchmarkId::new("hamming_words_128", *str_len), |b| b.iter(|| hamming_words_128(&a_str, &b_str))); |
| 26 | group.bench_function(BenchmarkId::new("hamming_simd_movemask", *str_len), |b| b.iter(|| hamming_simd_movemask(&a_str, &b_str))); |
| 27 | group.bench_function(BenchmarkId::new("hamming_simd_parallel", *str_len), |b| b.iter(|| hamming_simd_parallel(&a_str, &b_str))); |
| 28 | } |
| 29 | |
| 30 | group.finish(); |
| 31 | } |
| 32 | |
| 33 | fn bench_rand_hamming_search(c: &mut Criterion) { |
| 34 | let mut rng = StdRng::seed_from_u64(1234); |
| 35 | let mut group = c.benchmark_group("bench_rand_hamming_search"); |
| 36 | let config = PlotConfiguration::default().summary_scale(AxisScale::Logarithmic); |
| 37 | group.plot_config(config); |
| 38 | |
| 39 | for str_len in [100, 1000].iter() { |
| 40 | let needle_len = black_box(*str_len / 10); |
| 41 | let num_needles = black_box(*str_len / 20); |
| 42 | let k = black_box(((*str_len) as u32) / 100); |
| 43 | let (needle, haystack) = black_box(rand_hamming_needle_haystack(needle_len, *str_len, num_needles, k, &mut rng)); |
| 44 | |
| 45 | let res: Vec<Match> = hamming_search_naive_with_opts(&needle, &haystack, k, SearchType::All).collect(); |
| 46 | assert!(res == hamming_search_simd_with_opts(&needle, &haystack, k, SearchType::All).collect::<Vec<Match>>()); |
| 47 | |
| 48 | group.bench_function(BenchmarkId::new("hamming_search_naive_k", *str_len), |b| b.iter(|| hamming_search_naive_with_opts(&needle, &haystack, k, SearchType::All).last())); |
| 49 | group.bench_function(BenchmarkId::new("hamming_search_simd_k", *str_len), |b| b.iter(|| hamming_search_simd_with_opts(&needle, &haystack, k, SearchType::All).last())); |
| 50 | } |
| 51 | |
| 52 | group.finish(); |
| 53 | } |
| 54 | |
| 55 | fn bench_rand_levenshtein(c: &mut Criterion) { |
| 56 | let mut rng = StdRng::seed_from_u64(1234); |
| 57 | let mut group = c.benchmark_group("bench_rand_levenshtein"); |
| 58 | let config = PlotConfiguration::default().summary_scale(AxisScale::Logarithmic); |
| 59 | group.plot_config(config); |
| 60 | |
| 61 | for str_len in [10, 100, 1000].iter() { |
| 62 | let k = black_box(((*str_len) as u32) / 10); |
| 63 | let (a_str, b_str) = black_box(rand_levenshtein_pair(*str_len, k, &mut rng)); |
| 64 | |
| 65 | let res = levenshtein_naive(&a_str, &b_str); |
| 66 | assert!(res == levenshtein_exp(&a_str, &b_str)); |
| 67 | assert!(res == levenshtein(&a_str, &b_str)); |
| 68 | |
| 69 | group.bench_function(BenchmarkId::new("levenshtein_naive", *str_len), |b| b.iter(|| levenshtein_naive(&a_str, &b_str))); |
| 70 | group.bench_function(BenchmarkId::new("levenshtein_exp", *str_len), |b| b.iter(|| levenshtein_exp(&a_str, &b_str))); |
| 71 | group.bench_function(BenchmarkId::new("levenshtein", *str_len), |b| b.iter(|| levenshtein(&a_str, &b_str))); |
| 72 | } |
| 73 | |
| 74 | group.finish(); |
| 75 | } |
| 76 | |
| 77 | fn bench_rand_levenshtein_k(c: &mut Criterion) { |
| 78 | let mut rng = StdRng::seed_from_u64(1234); |
| 79 | let mut group = c.benchmark_group("bench_rand_levenshtein_k"); |
| 80 | let config = PlotConfiguration::default().summary_scale(AxisScale::Logarithmic); |
| 81 | group.plot_config(config); |
| 82 | |
| 83 | for str_len in [10, 100, 1000].iter() { |
| 84 | let k = black_box(((*str_len) as u32) / 10); |
| 85 | let trace_on = black_box(false); |
| 86 | let (a_str, b_str) = black_box(rand_levenshtein_pair(*str_len, k, &mut rng)); |
| 87 | |
| 88 | let res = levenshtein_naive_with_opts(&a_str, &b_str, trace_on, LEVENSHTEIN_COSTS); |
| 89 | assert!(res == levenshtein_naive_k_with_opts(&a_str, &b_str, k, trace_on, LEVENSHTEIN_COSTS).unwrap()); |
| 90 | assert!(res == levenshtein_simd_k_with_opts(&a_str, &b_str, k, trace_on, LEVENSHTEIN_COSTS).unwrap()); |
| 91 | |
| 92 | group.bench_function(BenchmarkId::new("levenshtein_naive", *str_len), |b| b.iter(|| levenshtein_naive_with_opts(&a_str, &b_str, trace_on, LEVENSHTEIN_COSTS))); |
| 93 | group.bench_function(BenchmarkId::new("levenshtein_naive_k", *str_len), |b| b.iter(|| levenshtein_naive_k_with_opts(&a_str, &b_str, k, trace_on, LEVENSHTEIN_COSTS))); |
| 94 | group.bench_function(BenchmarkId::new("levenshtein_simd_k", *str_len), |b| b.iter(|| levenshtein_simd_k_with_opts(&a_str, &b_str, k, trace_on, LEVENSHTEIN_COSTS))); |
| 95 | } |
| 96 | |
| 97 | group.finish(); |
| 98 | } |
| 99 | |
| 100 | fn bench_rand_levenshtein_search(c: &mut Criterion) { |
| 101 | let mut rng = StdRng::seed_from_u64(1234); |
| 102 | let mut group = c.benchmark_group("bench_rand_levenshtein_search"); |
| 103 | let config = PlotConfiguration::default().summary_scale(AxisScale::Logarithmic); |
| 104 | group.plot_config(config); |
| 105 | |
| 106 | for str_len in [100, 1000].iter() { |
| 107 | let needle_len = black_box(*str_len / 10); |
| 108 | let num_needles = black_box(*str_len / 20); |
| 109 | let k = black_box(((*str_len) as u32) / 100); |
| 110 | let anchored = black_box(false); |
| 111 | let (needle, haystack) = black_box(rand_levenshtein_needle_haystack(needle_len, *str_len, num_needles, k, &mut rng)); |
| 112 | |
| 113 | let res: Vec<Match> = levenshtein_search_naive_with_opts(&needle, &haystack, k, SearchType::All, LEVENSHTEIN_COSTS, anchored).collect(); |
| 114 | assert!(res == levenshtein_search_simd_with_opts(&needle, &haystack, k, SearchType::All, LEVENSHTEIN_COSTS, anchored).collect::<Vec<Match>>()); |
| 115 | |
| 116 | group.bench_function(BenchmarkId::new("levenshtein_search_naive_k", *str_len), |b| b.iter(|| levenshtein_search_naive_with_opts(&needle, &haystack, k, SearchType::All, LEVENSHTEIN_COSTS, anchored).last())); |
| 117 | group.bench_function(BenchmarkId::new("levenshtein_search_simd_k", *str_len), |b| b.iter(|| levenshtein_search_simd_with_opts(&needle, &haystack, k, SearchType::All, LEVENSHTEIN_COSTS, anchored).last())); |
| 118 | } |
| 119 | |
| 120 | group.finish(); |
| 121 | } |
| 122 | |
| 123 | criterion_group!(bench_rand, bench_rand_hamming, bench_rand_hamming_search, bench_rand_levenshtein, bench_rand_levenshtein_k, bench_rand_levenshtein_search); |
| 124 | criterion_main!(bench_rand); |
| 125 | |
| 126 | fn rand_hamming_needle_haystack<R: Rng>(needle_len: usize, haystack_len: usize, num_match: usize, k: u32, rng: &mut R) -> (Vec<u8>, Vec<u8>) { |
| 127 | let mut idx: Vec<usize> = (0usize..haystack_len).collect(); |
| 128 | idx.shuffle(rng); |
| 129 | let mut insert = vec![false; haystack_len]; |
| 130 | |
| 131 | for i in 0..num_match { |
| 132 | insert[idx[i]] = true; |
| 133 | } |
| 134 | |
| 135 | let bytes: Vec<u8> = (33u8..127u8).collect(); |
| 136 | let needle = rand_alloc_str(needle_len, rng); |
| 137 | let mut haystack: Vec<u8> = vec![]; |
| 138 | |
| 139 | for i in 0..haystack_len { |
| 140 | if insert[i] { |
| 141 | let s = rand_hamming_mutate(&needle, k, rng); |
| 142 | haystack.extend(&s[..needle_len]); |
| 143 | }else{ |
| 144 | haystack.push(*bytes.choose(rng).unwrap()); |
| 145 | } |
| 146 | } |
| 147 | |
| 148 | let mut haystack_final = alloc_str(haystack.len()); |
| 149 | fill_str(&mut haystack_final, &haystack); |
| 150 | |
| 151 | (needle, haystack_final) |
| 152 | } |
| 153 | |
| 154 | fn rand_hamming_pair<R: Rng>(length: usize, k: u32, rng: &mut R) -> (Vec<u8>, Vec<u8>) { |
| 155 | let a = rand_alloc_str(length, rng); |
| 156 | let b = rand_hamming_mutate(&a, k, rng); |
| 157 | |
| 158 | (a, b) |
| 159 | } |
| 160 | |
| 161 | fn rand_hamming_mutate<R: Rng>(a: &[u8], k: u32, rng: &mut R) -> Vec<u8> { |
| 162 | let mut b = alloc_str(a.len()); |
| 163 | fill_str(&mut b, a); |
| 164 | let curr_k: usize = rng.gen_range((k / 2) as usize, k as usize + 1); |
| 165 | let mut idx: Vec<usize> = (0usize..a.len()).collect(); |
| 166 | idx.shuffle(rng); |
| 167 | |
| 168 | for i in 0..curr_k { |
| 169 | b[idx[i]] = 32u8; |
| 170 | } |
| 171 | |
| 172 | b |
| 173 | } |
| 174 | |
| 175 | fn rand_levenshtein_needle_haystack<R: Rng>(needle_len: usize, haystack_len: usize, num_match: usize, k: u32, rng: &mut R) -> (Vec<u8>, Vec<u8>) { |
| 176 | let mut idx: Vec<usize> = (0usize..haystack_len).collect(); |
| 177 | idx.shuffle(rng); |
| 178 | let mut insert = vec![false; haystack_len]; |
| 179 | |
| 180 | for i in 0..num_match { |
| 181 | insert[idx[i]] = true; |
| 182 | } |
| 183 | |
| 184 | let bytes: Vec<u8> = (33u8..127u8).collect(); |
| 185 | let needle = rand_str(needle_len, rng); |
| 186 | let mut haystack: Vec<u8> = vec![]; |
| 187 | |
| 188 | for i in 0..haystack_len { |
| 189 | if insert[i] { |
| 190 | let s = rand_levenshtein_mutate(&needle, k, rng); |
| 191 | haystack.extend(&s); |
| 192 | }else{ |
| 193 | haystack.push(*bytes.choose(rng).unwrap()); |
| 194 | } |
| 195 | } |
| 196 | |
| 197 | (needle, haystack) |
| 198 | } |
| 199 | |
| 200 | fn rand_levenshtein_pair<R: Rng>(length: usize, k: u32, rng: &mut R) -> (Vec<u8>, Vec<u8>) { |
| 201 | let a = rand_str(length, rng); |
| 202 | let b = rand_levenshtein_mutate(&a, k, rng); |
| 203 | |
| 204 | (a, b) |
| 205 | } |
| 206 | |
| 207 | fn rand_levenshtein_mutate<R: Rng>(a: &[u8], k: u32, rng: &mut R) -> Vec<u8> { |
| 208 | let mut edits = vec![0u8; a.len()]; |
| 209 | let curr_k: usize = rng.gen_range((k / 2) as usize, k as usize + 1); |
| 210 | let mut idx: Vec<usize> = (0usize..a.len()).collect(); |
| 211 | idx.shuffle(rng); |
| 212 | |
| 213 | for i in 0..curr_k { |
| 214 | edits[idx[i]] = rng.gen_range(1u8, 4u8); |
| 215 | } |
| 216 | |
| 217 | let bytes: Vec<u8> = (33u8..127u8).collect(); |
| 218 | let mut b = vec![]; |
| 219 | |
| 220 | for i in 0..a.len() { |
| 221 | match edits[i] { |
| 222 | 0u8 => { // same |
| 223 | b.push(a[i]); |
| 224 | }, |
| 225 | 1u8 => { // diff |
| 226 | b.push(32u8); |
| 227 | }, |
| 228 | 2u8 => { // insert |
| 229 | b.push(*bytes.choose(rng).unwrap()); |
| 230 | b.push(a[i]); |
| 231 | }, |
| 232 | 3u8 => (), // delete |
| 233 | _ => panic!("This should not have been reached!") |
| 234 | } |
| 235 | } |
| 236 | |
| 237 | b |
| 238 | } |
| 239 | |
| 240 | fn rand_str<R: Rng>(length: usize, rng: &mut R) -> Vec<u8> { |
| 241 | let bytes: Vec<u8> = (33u8..127u8).collect(); |
| 242 | let mut res = vec![0u8; length]; |
| 243 | |
| 244 | for i in 0..length { |
| 245 | res[i] = *bytes.choose(rng).unwrap(); |
| 246 | } |
| 247 | |
| 248 | res |
| 249 | } |
| 250 | |
| 251 | fn rand_alloc_str<R: Rng>(length: usize, rng: &mut R) -> Vec<u8> { |
| 252 | let bytes: Vec<u8> = (33u8..127u8).collect(); |
| 253 | let mut res = alloc_str(length); |
| 254 | |
| 255 | for i in 0..length { |
| 256 | res[i] = *bytes.choose(rng).unwrap(); |
| 257 | } |
| 258 | |
| 259 | res |
| 260 | } |
| 261 |