| 1 |
// Copyright 2026 The Fancy Regex Authors. |
| 2 |
// |
| 3 |
// Permission is hereby granted, free of charge, to any person obtaining a copy |
| 4 |
// of this software and associated documentation files (the "Software"), to deal |
| 5 |
// in the Software without restriction, including without limitation the rights |
| 6 |
// to use, copy, modify, merge, publish, distribute, sublicense, and/or sell |
| 7 |
// copies of the Software, and to permit persons to whom the Software is |
| 8 |
// furnished to do so, subject to the following conditions: |
| 9 |
// |
| 10 |
// The above copyright notice and this permission notice shall be included in |
| 11 |
// all copies or substantial portions of the Software. |
| 12 |
// |
| 13 |
// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR |
| 14 |
// IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, |
| 15 |
// FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE |
| 16 |
// AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER |
| 17 |
// LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, |
| 18 |
// OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN |
| 19 |
// THE SOFTWARE. |
| 20 |
|
| 21 |
use criterion::{black_box, criterion_group, criterion_main, BenchmarkId, Criterion}; |
| 22 |
|
| 23 |
use fancy_regex::{Input, RegexBuilder, RegexInput, RegexSet}; |
| 24 |
|
| 25 |
fn scan_with_regexset(regex_set: &RegexSet, haystack: &str) -> usize { |
| 26 |
let mut count = 0usize; |
| 27 |
let mut pos = 0usize; |
| 28 |
let base_input = RegexInput::new(haystack); |
| 29 |
|
| 30 |
while pos < haystack.len() { |
| 31 |
let input = base_input.clone().from_pos(pos); |
| 32 |
let next_pos_if_empty_match = input.haystack().advance_position(pos); |
| 33 |
let Some(mut matches) = regex_set.find_input(input).unwrap() else { |
| 34 |
break; |
| 35 |
}; |
| 36 |
let first_match = matches.next().unwrap().unwrap(); |
| 37 |
count += 1; |
| 38 |
pos = if first_match.end() > pos { |
| 39 |
first_match.end() |
| 40 |
} else { |
| 41 |
next_pos_if_empty_match |
| 42 |
}; |
| 43 |
} |
| 44 |
|
| 45 |
count |
| 46 |
} |
| 47 |
|
| 48 |
fn scan_with_individual_regexes(regexes: &[fancy_regex::Regex], haystack: &str) -> usize { |
| 49 |
let mut count = 0usize; |
| 50 |
let mut pos = 0usize; |
| 51 |
let mut cached_matches: Vec<Option<(usize, usize)>> = vec![None; regexes.len()]; |
| 52 |
|
| 53 |
while pos < haystack.len() { |
| 54 |
let mut best: Option<(usize, usize, usize)> = None; |
| 55 |
|
| 56 |
for (pattern_index, re) in regexes.iter().enumerate() { |
| 57 |
let matched = match cached_matches[pattern_index] { |
| 58 |
Some((start, end)) if start >= pos => Some((start, end)), |
| 59 |
_ => { |
| 60 |
let next = re |
| 61 |
.find_from_pos(haystack, pos) |
| 62 |
.unwrap() |
| 63 |
.map(|matched| (matched.start(), matched.end())); |
| 64 |
cached_matches[pattern_index] = next; |
| 65 |
next |
| 66 |
} |
| 67 |
}; |
| 68 |
let Some((matched_start, matched_end)) = matched else { |
| 69 |
continue; |
| 70 |
}; |
| 71 |
let candidate = (matched_start, pattern_index, matched_end); |
| 72 |
if best |
| 73 |
.as_ref() |
| 74 |
.map_or(true, |(best_start, best_pattern_index, _)| { |
| 75 |
candidate.0 < *best_start |
| 76 |
|| (candidate.0 == *best_start && candidate.1 < *best_pattern_index) |
| 77 |
}) |
| 78 |
{ |
| 79 |
best = Some(candidate); |
| 80 |
} |
| 81 |
// No later pattern can start earlier than pos, and a higher pattern index |
| 82 |
// can't beat the current best at the same position, so stop searching. |
| 83 |
if best |
| 84 |
.as_ref() |
| 85 |
.map_or(false, |&(best_start, _, _)| best_start == pos) |
| 86 |
{ |
| 87 |
break; |
| 88 |
} |
| 89 |
} |
| 90 |
|
| 91 |
let Some((_, _, best_end)) = best else { |
| 92 |
break; |
| 93 |
}; |
| 94 |
|
| 95 |
count += 1; |
| 96 |
pos = if best_end > pos { best_end } else { pos + 1 }; |
| 97 |
} |
| 98 |
|
| 99 |
count |
| 100 |
} |
| 101 |
|
| 102 |
fn run_scenario_bench(c: &mut Criterion, scenario_name: &str, patterns: &[&str], haystack: &str) { |
| 103 |
let regex_set = RegexSet::new(patterns.iter().copied()).unwrap(); |
| 104 |
let regexes: Vec<fancy_regex::Regex> = patterns |
| 105 |
.iter() |
| 106 |
.map(|pattern| RegexBuilder::new(pattern).seek(true).build().unwrap()) |
| 107 |
.collect(); |
| 108 |
|
| 109 |
let mut group = c.benchmark_group(scenario_name); |
| 110 |
group.bench_function(BenchmarkId::new("regexset", patterns.len()), |b| { |
| 111 |
b.iter(|| black_box(scan_with_regexset(®ex_set, black_box(haystack)))) |
| 112 |
}); |
| 113 |
group.bench_function(BenchmarkId::new("individual", patterns.len()), |b| { |
| 114 |
b.iter(|| black_box(scan_with_individual_regexes(®exes, black_box(haystack)))) |
| 115 |
}); |
| 116 |
group.finish(); |
| 117 |
} |
| 118 |
|
| 119 |
fn bench_regexset_easy(c: &mut Criterion) { |
| 120 |
let patterns = [ |
| 121 |
r"//[^\n]*", |
| 122 |
r#""(?:[^"\\]|\\.)*""#, |
| 123 |
r"\b(?:fn|let|mut|if|else|return)\b", |
| 124 |
r"\b[0-9]+\b", |
| 125 |
r"[A-Za-z_][A-Za-z0-9_]*", |
| 126 |
r"\s+", |
| 127 |
r"[{}();=,+\-]", |
| 128 |
r"\b[A-F0-9]{8}\b", |
| 129 |
]; |
| 130 |
let line = r#"let value = 42; fn run() { return value + 1; } // comment "string" DEADBEEF"#; |
| 131 |
let haystack = line.repeat(250); |
| 132 |
run_scenario_bench(c, "regexset_vs_individual_easy", &patterns, &haystack); |
| 133 |
} |
| 134 |
|
| 135 |
fn bench_regexset_hard(c: &mut Criterion) { |
| 136 |
let patterns = [ |
| 137 |
r"(?<=\$)\d+(?:\.\d{2})?", |
| 138 |
r"(?<=\bfoo)bar", |
| 139 |
r"\b([A-Za-z]{2,8})-\1\b", |
| 140 |
r"\b([0-9]{2})\1\b", |
| 141 |
r"(?<=ID:)[A-Z]{2}[0-9]{4}", |
| 142 |
]; |
| 143 |
let line = "foo bar foofoobar $19.99 test-test 1212 ID:AB1234 xx yy-yy 3434 foo bar "; |
| 144 |
let haystack = line.repeat(400); |
| 145 |
run_scenario_bench(c, "regexset_vs_individual_hard", &patterns, &haystack); |
| 146 |
} |
| 147 |
|
| 148 |
fn bench_regexset_mixed(c: &mut Criterion) { |
| 149 |
let patterns = [ |
| 150 |
r"//[^\n]*", |
| 151 |
r#""(?:[^"\\]|\\.)*""#, |
| 152 |
r"\b(?:fn|let|mut|if|else|return)\b", |
| 153 |
r"\b[0-9]+\b", |
| 154 |
r"[A-Za-z_][A-Za-z0-9_]*", |
| 155 |
r"(?<=\$)\d+(?:\.\d{2})?", |
| 156 |
r"\b([A-Za-z]{2,8})-\1\b", |
| 157 |
r"(?<=ID:)[A-Z]{2}[0-9]{4}", |
| 158 |
]; |
| 159 |
let line = r#"let test = ID:AB1234; if foofoo == 1212 { return "$29.95"; } // note test-test "#; |
| 160 |
let haystack = line.repeat(300); |
| 161 |
run_scenario_bench(c, "regexset_vs_individual_mixed", &patterns, &haystack); |
| 162 |
} |
| 163 |
|
| 164 |
fn bench_regexset_late_match(c: &mut Criterion) { |
| 165 |
// Patterns whose regular approximations (used by the earliest-match finder) |
| 166 |
// hit at many positions where the real patterns then fail to match: the |
| 167 |
// lookbehind approximation is just `z`, and the backref approximation |
| 168 |
// matches any 6-letter word. Only the very end of the haystack has real |
| 169 |
// matches, so `find_input` churns through many failed candidate positions. |
| 170 |
let patterns = [r"(?<=xy)z", r"\b([a-z]{3})\1\b"]; |
| 171 |
let line = "az bz cz dz ez fz gz hz iz jz abcdef ghijkl "; |
| 172 |
let mut haystack = line.repeat(300); |
| 173 |
haystack.push_str("xyz abcabc"); |
| 174 |
run_scenario_bench(c, "regexset_vs_individual_late_match", &patterns, &haystack); |
| 175 |
} |
| 176 |
|
| 177 |
criterion_group!( |
| 178 |
regexset_vs_individual_benches, |
| 179 |
bench_regexset_easy, |
| 180 |
bench_regexset_hard, |
| 181 |
bench_regexset_mixed, |
| 182 |
bench_regexset_late_match |
| 183 |
); |
| 184 |
criterion_main!(regexset_vs_individual_benches); |