Debian Codegraph @

debian/rust-vendor/fancy-regex/benches/compile.rs

raw
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 //! End-to-end *compilation* benchmarks: how long it takes to turn a pattern
22 //! string into a finished `Regex` / `RegexSet`.
23 //!
24 //! These complement `bench.rs`, which measures parsing and matching. Here every
25 //! iteration runs the full `Regex::new` pipeline (parse -> optimize -> analyze ->
26 //! compile, including building the underlying regex-automata engines).
27 //!
28 //! For "easy" patterns that fancy-regex delegates wholesale to regex-automata, a
29 //! `regex::Regex::new` row is included as a side-by-side baseline so the
30 //! fancy-regex overhead on top of the engine build is visible.
31
32 #[macro_use]
33 extern crate criterion;
34
35 use criterion::{black_box, Criterion};
36
37 use fancy_regex::{Regex, RegexSet};
38 use regex::Regex as StdRegex;
39
40 // ---------------------------------------------------------------------------
41 // Corpus
42 // ---------------------------------------------------------------------------
43
44 /// Easy pattern, fully delegated (the same literal-heavy pattern used in `bench.rs`).
45 /// Exercises bottleneck #1 (re-parse on the easy/Wrap path).
46 const EASY_LITERAL: &str =
47 r"^\\([!-/:-@\[-`\{-~aftnrv]|[0-7]{1,3}|x[0-9a-fA-F]{2}|x\{[0-9a-fA-F]{1,6}\})";
48
49 /// A second easy pattern resembling a real-world tokenizer rule.
50 const EASY_EMAIL: &str = r"[a-zA-Z0-9._%+-]+@[a-zA-Z0-9.-]+\.[a-zA-Z]{2,}";
51
52 /// Hard pattern (lookarounds + backreference) interleaved with easy runs, so the
53 /// compiler emits several `Insn::Delegate` engines. Exercises bottleneck #2.
54 const DELEGATE_HEAVY: &str = r"(\d{3})(?=x)[a-z]+(?<=ab)\w+\1[A-Z]{2}(?!q)\s+foo";
55
56 /// Build a pattern with many fancy-separated easy runs to stress the
57 /// "one meta engine per easy run" cost.
58 fn delegate_stress() -> String {
59 let mut s = String::new();
60 for _ in 0..20 {
61 // each `[a-z]{2}` easy run is separated by a lookahead (a hard part),
62 // forcing a separate delegate engine for each run.
63 s.push_str("[a-z]{2}(?=x)");
64 }
65 s
66 }
67
68 /// A large alternation of literals — stays "easy" (Wrap), stressing the engine's
69 /// literal extraction / prefilter build on the top-level (unanchored) engine.
70 fn large_alternation() -> String {
71 let mut s = String::from(r"\b(?:");
72 for i in 0..100 {
73 if i != 0 {
74 s.push('|');
75 }
76 s.push_str("word");
77 s.push_str(&i.to_string());
78 }
79 s.push_str(r")\b");
80 s
81 }
82
83 /// Patterns for the RegexSet bench: a mix of easy and hard (fancy) patterns.
84 fn regexset_patterns() -> Vec<String> {
85 vec![
86 r"\d{4}-\d{2}-\d{2}".to_string(),
87 r"[a-zA-Z0-9._%+-]+@[a-zA-Z0-9.-]+\.[a-zA-Z]{2,}".to_string(),
88 r"(?<=\$)\d+(?:\.\d{2})?".to_string(),
89 r"(\w+)\s+\1".to_string(),
90 r"(?:https?|ftp)://\S+".to_string(),
91 r"#[0-9a-fA-F]{6}\b".to_string(),
92 r"foo(?=bar)".to_string(),
93 r"\b[A-Z][a-z]+\b".to_string(),
94 ]
95 }
96
97 // ---------------------------------------------------------------------------
98 // Benchmarks
99 // ---------------------------------------------------------------------------
100
101 fn compile_easy(c: &mut Criterion) {
102 let mut group = c.benchmark_group("compile_easy");
103 group.bench_function("fancy/literal", |b| {
104 b.iter(|| Regex::new(black_box(EASY_LITERAL)).unwrap())
105 });
106 group.bench_function("regex/literal", |b| {
107 b.iter(|| StdRegex::new(black_box(EASY_LITERAL)).unwrap())
108 });
109 group.bench_function("fancy/email", |b| {
110 b.iter(|| Regex::new(black_box(EASY_EMAIL)).unwrap())
111 });
112 group.bench_function("regex/email", |b| {
113 b.iter(|| StdRegex::new(black_box(EASY_EMAIL)).unwrap())
114 });
115 group.finish();
116 }
117
118 fn compile_large_alternation(c: &mut Criterion) {
119 let pat = large_alternation();
120 let mut group = c.benchmark_group("compile_large_alternation");
121 group.bench_function("fancy", |b| b.iter(|| Regex::new(black_box(&pat)).unwrap()));
122 group.bench_function("regex", |b| {
123 b.iter(|| StdRegex::new(black_box(&pat)).unwrap())
124 });
125 group.finish();
126 }
127
128 fn compile_delegate_heavy(c: &mut Criterion) {
129 let stress = delegate_stress();
130 let mut group = c.benchmark_group("compile_delegate_heavy");
131 // No regex baseline: these use fancy features regex-automata can't compile.
132 group.bench_function("fancy/realistic", |b| {
133 b.iter(|| Regex::new(black_box(DELEGATE_HEAVY)).unwrap())
134 });
135 group.bench_function("fancy/stress_20_delegates", |b| {
136 b.iter(|| Regex::new(black_box(&stress)).unwrap())
137 });
138 group.finish();
139 }
140
141 fn compile_regexset(c: &mut Criterion) {
142 let pats = regexset_patterns();
143 let mut group = c.benchmark_group("compile_regexset");
144 group.sample_size(30);
145 group.bench_function("fancy_set_8", |b| {
146 b.iter(|| RegexSet::new(black_box(&pats)).unwrap())
147 });
148 group.finish();
149 }
150
151 criterion_group!(
152 name = compile_benches;
153 config = Criterion::default();
154 targets =
155 compile_easy,
156 compile_large_alternation,
157 compile_delegate_heavy,
158 compile_regexset,
159 );
160 criterion_main!(compile_benches);