-
Notifications
You must be signed in to change notification settings - Fork 15
/
Main.cs
423 lines (392 loc) · 14.4 KB
/
Main.cs
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
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
using System.Diagnostics;
using System.Text;
using System.Text.RegularExpressions;
/// <summary>The configuration of a benchmark, as parsed from the incoming KLV
/// data.</summary>
struct Config
{
public string engine;
public string? name;
public string? model;
public string? pattern;
public bool caseInsensitive;
public bool unicode;
// rebar benchmarks permit the haystack to be invalid UTF-8,
// but .NET's regex engine can only search sequences of UTF-16
// code units (as far as I can tell). So there's no point in
// trying to represent the haystack as a byte[]. If this runner
// program is called with a haystack that contains invalid UTF-8,
// then it will throw an exception.
public string? haystack;
public int maxIters;
public int maxWarmupIters;
public long maxTime;
public long maxWarmupTime;
public Config(string engineName) => engine = engineName;
public Regex CompileRegex() => CompilePattern(pattern!);
public Regex CompilePattern(string pat) {
// We enable the invariant culture to avoid any sort of tailoring.
// Tailoring might be useful to benchmark, but rebar doesn't do it.
// Primarily because most (not all) regex engines don't support it.
RegexOptions options = RegexOptions.CultureInvariant;
if (caseInsensitive) {
options |= RegexOptions.IgnoreCase;
}
options |= engine switch
{
"interp" => RegexOptions.None,
"compiled" => RegexOptions.Compiled,
"nobacktrack" => RegexOptions.NonBacktracking,
_ => throw new Exception($"unrecognized engine '${engine}'"),
};
return new Regex(pat, options);
}
}
/// <summary>A single Key-Length-Value item.</summary>
struct OneKLV
{
/// <summary>The key name.</summary>
public string key;
/// <summary>The value contents.</summary>
public string value;
/// <summary>
/// The length, in bytes, used up by this KLV item.
/// This is useful for parsing a sequence of KLV items.
/// This length says how much to skip ahead to start
/// parsing the next KLV item.
/// </summary>
public int len;
public OneKLV(ReadOnlySpan<byte> raw) {
// The default for the UTF-8 encoding is to lossily decode bytes.
// But we really don't want to do that here. We want to return an
// error, since otherwise, this runner could silently search a slightly
// different haystack than what other runner programs do.
//
// This means that this runner program must only be used in benchmarks
// with valid UTF-8. This is not an arbitrary decision. As far as I can
// tell, C#'s regex engine only works on String or ReadOnlySpan<char>,
// both of which are Unicode strings and incapable of representing
// arbitrary bytes.
Encoding utf8 = Encoding.GetEncoding(
"utf-8",
new EncoderExceptionFallback(),
new DecoderExceptionFallback()
);
var keyEnd = raw.IndexOf((byte)':');
if (keyEnd < 0) {
throw new Exception("invalid KLV item: could not find first ':'");
}
key = utf8.GetString(raw.Slice(0, keyEnd));
raw = raw.Slice(keyEnd + 1);
var valueLenEnd = raw.IndexOf((byte)':');
if (valueLenEnd < 0) {
throw new Exception("invalid KLV item: could not find second ':'");
}
int valueLen = int.Parse(utf8.GetString(raw.Slice(0, valueLenEnd)));
if (raw[valueLenEnd + 1 + valueLen] != '\n') {
throw new Exception("invalid KLV item: no line terminator");
}
value = utf8.GetString(raw.Slice(valueLenEnd + 1, valueLen));
len = keyEnd + 1 + valueLenEnd + 1 + valueLen + 1;
}
}
/// <summary>
/// A representation of the data we gather from a single benchmark execution.
/// That is, the time it took to run and the count reported for verification.
/// </summary>
/// <param name="duration">
/// The duration, in nanoseconds. This might not always
/// have nanosecond resolution, but its units are always
/// nanoseconds.
/// </param>
/// <param name="count">
/// The count reported by the benchmark. This is checked
/// against what is expected in the benchmark definition
/// by rebar.
/// </param>
record struct Sample(long duration, int count);
class Program
{
static void Main(string[] args)
{
if (args.Length != 1) {
throw new Exception(
"Usage: main <interp | compiled | nobacktrack | version>"
);
}
if (args[0] == "version") {
Console.WriteLine(Environment.Version);
return;
}
// Read all of stdin into a span of bytes
MemoryStream stdinCopy = new();
using (Stream stdin = Console.OpenStandardInput()) {
stdin.CopyTo(stdinCopy);
}
ReadOnlySpan<byte> raw = stdinCopy.GetBuffer().AsSpan(
0,
(int)stdinCopy.Length
);
// OK, now read each of our KLV items and build up our config.
Config config = new(args[0]);
while (!raw.IsEmpty) {
var klv = new OneKLV(raw);
raw = raw.Slice(klv.len);
switch (klv.key)
{
case "name":
config.name = klv.value;
break;
case "model":
config.model = klv.value;
break;
case "pattern":
if (config.pattern != null) {
throw new Exception("only one pattern is supported");
}
config.pattern = klv.value;
break;
case "case-insensitive":
config.caseInsensitive = klv.value == "true";
break;
case "unicode":
config.unicode = klv.value == "unicode";
break;
case "haystack":
config.haystack = klv.value;
break;
case "max-iters":
config.maxIters = int.Parse(klv.value);
break;
case "max-warmup-iters":
config.maxWarmupIters = int.Parse(klv.value);
break;
case "max-time":
config.maxTime = long.Parse(klv.value);
break;
case "max-warmup-time":
config.maxWarmupTime = long.Parse(klv.value);
break;
default:
throw new Exception($"unrecognized KLV key {klv.key}");
}
}
if (config.model != "regex-redux" && config.pattern == null) {
throw new Exception("missing pattern, must be provided once");
}
// Run our selected model and print the samples.
List<Sample> samples = config.model switch
{
"compile" => ModelCompile(config),
"count" => ModelCount(config),
"count-spans" => ModelCountSpans(config),
"count-captures" => ModelCountCaptures(config),
"grep" => ModelGrep(config),
"grep-captures" => ModelGrepCaptures(config),
"regex-redux" => ModelRegexRedux(config),
_ => throw new Exception(
$"unknown benchmark model {config.model}"
),
};
foreach (Sample s in samples) {
Console.WriteLine($"{s.duration},{s.count}");
}
}
static List<Sample> ModelCompile(Config config)
{
return RunAndCount(
config,
re => re.Count(config.haystack!),
config.CompileRegex
);
}
static List<Sample> ModelCount(Config config)
{
var re = config.CompileRegex();
return RunAndCount(
config,
n => n,
() => re.Count(config.haystack!)
);
}
static List<Sample> ModelCountSpans(Config config)
{
var re = config.CompileRegex();
return RunAndCount(
config,
n => n,
() => {
int count = 0;
foreach (ValueMatch m in re.EnumerateMatches(config.haystack!)) {
// This is not quite the same as most other regex
// engines, which report span lengths in terms of
// number of bytes. This is in terms of UTF-16 code
// units. We deal this by permitting different counts
// for .NET regex engines in the benchmark definition.
count += m.Length;
}
return count;
}
);
}
static List<Sample> ModelCountCaptures(Config config)
{
var re = config.CompileRegex();
return RunAndCount(
config,
n => n,
() => {
int count = 0;
Match m = re.Match(config.haystack!);
while (m.Success) {
foreach (Group g in m.Groups) {
if (g.Success) {
count++;
}
}
m = m.NextMatch();
}
return count;
}
);
}
static List<Sample> ModelGrep(Config config)
{
var re = config.CompileRegex();
return RunAndCount(
config,
n => n,
() => {
int count = 0;
var span = config.haystack.AsSpan();
foreach (ReadOnlySpan<char> line in span.EnumerateLines()) {
if (re.IsMatch(line)) {
count++;
}
}
return count;
}
);
}
static List<Sample> ModelGrepCaptures(Config config)
{
var re = config.CompileRegex();
return RunAndCount(
config,
n => n,
() => {
int count = 0;
var span = config.haystack.AsSpan();
foreach (ReadOnlySpan<char> line in span.EnumerateLines()) {
Match m = re.Match(line.ToString());
while (m.Success) {
foreach (Group g in m.Groups) {
if (g.Success) {
count++;
}
}
m = m.NextMatch();
}
}
return count;
}
);
}
static List<Sample> ModelRegexRedux(Config config)
{
return RunAndCount(
config,
n => n,
() => {
var expected = """
agggtaaa|tttaccct 6
[cgt]gggtaaa|tttaccc[acg] 26
a[act]ggtaaa|tttacc[agt]t 86
ag[act]gtaaa|tttac[agt]ct 58
agg[act]taaa|ttta[agt]cct 113
aggg[acg]aaa|ttt[cgt]ccct 31
agggt[cgt]aa|tt[acg]accct 31
agggta[cgt]a|t[acg]taccct 32
agggtaa[cgt]|[acg]ttaccct 43
1016745
1000000
547899
""";
var result = new StringBuilder();
var seq = config.haystack!;
var ilen = seq.Length;
seq = config.CompilePattern(@">[^\n]*\n|\n").Replace(seq, "");
var clen = seq.Length;
var variants = new string[] {
@"agggtaaa|tttaccct",
@"[cgt]gggtaaa|tttaccc[acg]",
@"a[act]ggtaaa|tttacc[agt]t",
@"ag[act]gtaaa|tttac[agt]ct",
@"agg[act]taaa|ttta[agt]cct",
@"aggg[acg]aaa|ttt[cgt]ccct",
@"agggt[cgt]aa|tt[acg]accct",
@"agggta[cgt]a|t[acg]taccct",
@"agggtaa[cgt]|[acg]ttaccct",
};
foreach (string variant in variants) {
var re = config.CompilePattern(variant);
var count = re.Count(seq);
result.AppendLine($"{variant} {count}");
}
seq = config.CompilePattern(@"tHa[Nt]").Replace(seq, "<4>");
seq = config.CompilePattern(@"aND|caN|Ha[DS]|WaS").Replace(seq, "<3>");
seq = config.CompilePattern(@"a[NSt]|BY").Replace(seq, "<2>");
seq = config.CompilePattern(@"<[^>]*>").Replace(seq, "|");
seq = config.CompilePattern(@"\|[^|][^|]*\|").Replace(seq, "-");
result.AppendLine("");
result.AppendLine($"{ilen}");
result.AppendLine($"{clen}");
result.AppendLine($"{seq.Length}");
if (result.ToString() != expected) {
throw new Exception("result did not match expected");
}
return seq.Length;
}
);
}
// Takes in a benchmark config, a closure that returns the count from the
// benchmark function and a benchmark function that returns a result that
// can be converted into a count. As output, it returns a list of samples
// generated by repeatedly running the 'bench' function and timing how long
// it takes.
//
// In practice, 'bench' returns the count and 'count' is just the identity
// function in all except for one case: measuring compilation time. In
// that case, 'bench' returns the regex object itself, and 'count' runs the
// regex to get the count.
//
// The 'count' function is not part of the measurement.
static List<Sample> RunAndCount<T>(
Config config,
Func<T, int> count,
Func<T> bench
)
{
Stopwatch warmupTimer = Stopwatch.StartNew();
for (int i = 0; i < config.maxWarmupIters; i++) {
var result = bench();
count(result);
if (warmupTimer.Elapsed.TotalNanoseconds >= config.maxWarmupTime) {
break;
}
}
List<Sample> samples = new();
Stopwatch runTimer = Stopwatch.StartNew();
for (int i = 0; i < config.maxIters; i++) {
Stopwatch benchTimer = Stopwatch.StartNew();
var result = bench();
var elapsed = benchTimer.Elapsed.TotalNanoseconds;
var n = count(result);
samples.Add(new Sample((long)elapsed, n));
if (runTimer.Elapsed.TotalNanoseconds >= config.maxTime) {
break;
}
}
return samples;
}
}