forked from BurntSushi/rebar
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathmain.rs
More file actions
162 lines (151 loc) · 4.64 KB
/
main.rs
File metadata and controls
162 lines (151 loc) · 4.64 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
use std::io::Write;
use {
anyhow::Context,
lexopt::Arg,
regex_lite::{Regex, RegexBuilder},
};
fn main() -> anyhow::Result<()> {
let mut p = lexopt::Parser::from_env();
let (mut quiet, mut version) = (false, false);
while let Some(arg) = p.next()? {
match arg {
Arg::Short('h') | Arg::Long("help") => {
anyhow::bail!("main [--version | --quiet]")
}
Arg::Short('q') | Arg::Long("quiet") => {
quiet = true;
}
Arg::Long("version") => {
version = true;
}
_ => return Err(arg.unexpected().into()),
}
}
if version {
writeln!(std::io::stdout(), "{}", env!("CARGO_PKG_VERSION"))?;
return Ok(());
}
let b = klv::Benchmark::read(std::io::stdin())
.context("failed to read KLV data from <stdin>")?;
let samples = match b.model.as_str() {
"compile" => model_compile(&b)?,
"count" => model_count(&b, &compile(&b)?)?,
"count-spans" => model_count_spans(&b, &compile(&b)?)?,
"count-captures" => model_count_captures(&b, &compile(&b)?)?,
"grep" => model_grep(&b, &compile(&b)?)?,
"grep-captures" => model_grep_captures(&b, &compile(&b)?)?,
"regex-redux" => model_regex_redux(&b)?,
_ => anyhow::bail!("unrecognized benchmark model '{}'", b.model),
};
if !quiet {
let mut stdout = std::io::stdout().lock();
for s in samples.iter() {
writeln!(stdout, "{},{}", s.duration.as_nanos(), s.count)?;
}
}
Ok(())
}
fn model_compile(b: &klv::Benchmark) -> anyhow::Result<Vec<timer::Sample>> {
let haystack = b.haystack_str()?;
timer::run_and_count(
b,
|re: Regex| Ok(re.find_iter(haystack).count()),
|| compile(b),
)
}
fn model_count(
b: &klv::Benchmark,
re: &Regex,
) -> anyhow::Result<Vec<timer::Sample>> {
let haystack = b.haystack_str()?;
timer::run(b, || Ok(re.find_iter(haystack).count()))
}
fn model_count_spans(
b: &klv::Benchmark,
re: &Regex,
) -> anyhow::Result<Vec<timer::Sample>> {
let haystack = b.haystack_str()?;
timer::run(b, || Ok(re.find_iter(haystack).map(|m| m.len()).sum()))
}
fn model_count_captures(
b: &klv::Benchmark,
re: &Regex,
) -> anyhow::Result<Vec<timer::Sample>> {
let haystack = b.haystack_str()?;
let mut caps = re.capture_locations();
timer::run(b, || {
let mut at = 0;
let mut count = 0;
while let Some(m) = re.captures_read_at(&mut caps, haystack, at) {
for i in 0..caps.len() {
if caps.get(i).is_some() {
count += 1;
}
}
// Benchmark definition says we may assume empty matches are
// impossible.
at = m.end();
}
Ok(count)
})
}
fn model_grep(
b: &klv::Benchmark,
re: &Regex,
) -> anyhow::Result<Vec<timer::Sample>> {
let haystack = b.haystack_str()?;
timer::run(b, || {
let mut count = 0;
for line in haystack.lines() {
if re.is_match(line) {
count += 1;
}
}
Ok(count)
})
}
fn model_grep_captures(
b: &klv::Benchmark,
re: &Regex,
) -> anyhow::Result<Vec<timer::Sample>> {
let haystack = b.haystack_str()?;
let mut caps = re.capture_locations();
timer::run(b, || {
let mut count = 0;
for line in haystack.lines() {
let mut at = 0;
while let Some(m) = re.captures_read_at(&mut caps, line, at) {
for i in 0..caps.len() {
if caps.get(i).is_some() {
count += 1;
}
}
// Benchmark definition says we may assume empty matches are
// impossible.
at = m.end();
}
}
Ok(count)
})
}
fn model_regex_redux(
b: &klv::Benchmark,
) -> anyhow::Result<Vec<timer::Sample>> {
let haystack = b.haystack_str()?;
let compile = |pattern: &str| -> anyhow::Result<regexredux::RegexFn> {
let re = compile_pattern(b, pattern)?;
let find = move |h: &str| Ok(re.find(h).map(|m| (m.start(), m.end())));
Ok(Box::new(find))
};
timer::run(b, || regexredux::generic(haystack, compile))
}
fn compile(b: &klv::Benchmark) -> anyhow::Result<Regex> {
compile_pattern(b, b.regex.one()?)
}
fn compile_pattern(b: &klv::Benchmark, pat: &str) -> anyhow::Result<Regex> {
let re = RegexBuilder::new(pat)
.case_insensitive(b.regex.case_insensitive)
.size_limit((1 << 20) * 100)
.build()?;
Ok(re)
}