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
|
use criterion::black_box;
use winnow::branch::alt;
use winnow::bytes::take_till1;
use winnow::bytes::take_while1;
use winnow::multi::many0;
use winnow::prelude::*;
fn contains_token(c: &mut criterion::Criterion) {
let data = [
("contiguous", CONTIGUOUS),
("interleaved", INTERLEAVED),
("canada", CANADA),
];
let mut group = c.benchmark_group("contains_token");
for (name, sample) in data {
let len = sample.len();
group.throughput(criterion::Throughput::Bytes(len as u64));
group.bench_with_input(criterion::BenchmarkId::new("str", name), &len, |b, _| {
b.iter(|| black_box(parser_str.parse_next(black_box(sample)).unwrap()));
});
group.bench_with_input(criterion::BenchmarkId::new("slice", name), &len, |b, _| {
b.iter(|| black_box(parser_slice.parse_next(black_box(sample)).unwrap()));
});
group.bench_with_input(criterion::BenchmarkId::new("array", name), &len, |b, _| {
b.iter(|| black_box(parser_array.parse_next(black_box(sample)).unwrap()));
});
group.bench_with_input(criterion::BenchmarkId::new("tuple", name), &len, |b, _| {
b.iter(|| black_box(parser_tuple.parse_next(black_box(sample)).unwrap()));
});
group.bench_with_input(
criterion::BenchmarkId::new("closure-or", name),
&len,
|b, _| {
b.iter(|| black_box(parser_closure_or.parse_next(black_box(sample)).unwrap()));
},
);
group.bench_with_input(
criterion::BenchmarkId::new("closure-matches", name),
&len,
|b, _| {
b.iter(|| {
black_box(
parser_closure_matches
.parse_next(black_box(sample))
.unwrap(),
)
});
},
);
}
group.finish();
}
fn parser_str(input: &str) -> IResult<&str, usize> {
let contains = "0123456789";
many0(alt((take_while1(contains), take_till1(contains)))).parse_next(input)
}
fn parser_slice(input: &str) -> IResult<&str, usize> {
let contains = &['0', '1', '2', '3', '4', '5', '6', '7', '8', '9'][..];
many0(alt((take_while1(contains), take_till1(contains)))).parse_next(input)
}
fn parser_array(input: &str) -> IResult<&str, usize> {
let contains = ['0', '1', '2', '3', '4', '5', '6', '7', '8', '9'];
many0(alt((take_while1(contains), take_till1(contains)))).parse_next(input)
}
fn parser_tuple(input: &str) -> IResult<&str, usize> {
let contains = ('0', '1', '2', '3', '4', '5', '6', '7', '8', '9');
many0(alt((take_while1(contains), take_till1(contains)))).parse_next(input)
}
fn parser_closure_or(input: &str) -> IResult<&str, usize> {
let contains = |c: char| {
c == '0'
|| c == '1'
|| c == '2'
|| c == '3'
|| c == '4'
|| c == '5'
|| c == '6'
|| c == '7'
|| c == '8'
|| c == '9'
};
many0(alt((take_while1(contains), take_till1(contains)))).parse_next(input)
}
fn parser_closure_matches(input: &str) -> IResult<&str, usize> {
let contains = |c: char| matches!(c, '0' | '1' | '2' | '3' | '4' | '5' | '6' | '7' | '8' | '9');
many0(alt((take_while1(contains), take_till1(contains)))).parse_next(input)
}
const CONTIGUOUS: &str = "012345678901234567890123456789012345678901234567890123456789012345678901234567890123456789012345678901234567890123456789012345678901234567890123456789012345678901234567890123456789012345678901234567890123456789012345678901234567890123456789012345678901234567890123456789";
const INTERLEAVED: &str = "0123456789abc0123456789ab0123456789ab0123456789ab0123456789ab0123456789ab0123456789ab0123456789ab0123456789ab0123456789ab0123456789ab0123456789ab0123456789ab0123456789ab0123456789ab0123456789ab0123456789ab0123456789ab0123456789ab0123456789ab0123456789ab0123456789ab0123456789ab";
const CANADA: &str = include_str!("../third_party/nativejson-benchmark/data/canada.json");
criterion::criterion_group!(benches, contains_token);
criterion::criterion_main!(benches);
|