Debian Codegraph @

third_party/rust/winnow/benches/iter.rs

raw
1 use criterion::black_box;
2
3 use winnow::combinator::opt;
4 use winnow::prelude::*;
5 use winnow::stream::AsChar;
6 use winnow::token::one_of;
7
8 fn iter(c: &mut criterion::Criterion) {
9 let data = [
10 ("contiguous", CONTIGUOUS.as_bytes()),
11 ("interleaved", INTERLEAVED.as_bytes()),
12 ("canada", CANADA.as_bytes()),
13 ];
14 let mut group = c.benchmark_group("iter");
15 for (name, sample) in data {
16 let len = sample.len();
17 group.throughput(criterion::Throughput::Bytes(len as u64));
18
19 group.bench_with_input(
20 criterion::BenchmarkId::new("iterate", name),
21 &len,
22 |b, _| {
23 b.iter(|| black_box(iterate.parse_peek(black_box(sample)).unwrap()));
24 },
25 );
26 group.bench_with_input(
27 criterion::BenchmarkId::new("next_token", name),
28 &len,
29 |b, _| {
30 b.iter(|| black_box(next_token.parse_peek(black_box(sample)).unwrap()));
31 },
32 );
33 group.bench_with_input(
34 criterion::BenchmarkId::new("opt(one_of)", name),
35 &len,
36 |b, _| {
37 b.iter(|| black_box(opt_one_of.parse_peek(black_box(sample)).unwrap()));
38 },
39 );
40 group.bench_with_input(
41 criterion::BenchmarkId::new("take_while", name),
42 &len,
43 |b, _| {
44 b.iter(|| black_box(take_while.parse_peek(black_box(sample)).unwrap()));
45 },
46 );
47 group.bench_with_input(criterion::BenchmarkId::new("repeat", name), &len, |b, _| {
48 b.iter(|| black_box(repeat.parse_peek(black_box(sample)).unwrap()));
49 });
50 }
51 group.finish();
52 }
53
54 fn iterate(input: &mut &[u8]) -> ModalResult<usize> {
55 let mut count = 0;
56 for byte in input.iter() {
57 if byte.is_dec_digit() {
58 count += 1;
59 }
60 }
61 input.finish();
62 Ok(count)
63 }
64
65 fn next_token(input: &mut &[u8]) -> ModalResult<usize> {
66 let mut count = 0;
67 while let Some(byte) = input.next_token() {
68 if byte.is_dec_digit() {
69 count += 1;
70 }
71 }
72 Ok(count)
73 }
74
75 fn opt_one_of(input: &mut &[u8]) -> ModalResult<usize> {
76 let mut count = 0;
77 while !input.is_empty() {
78 while opt(one_of(AsChar::is_dec_digit))
79 .parse_next(input)?
80 .is_some()
81 {
82 count += 1;
83 }
84 while opt(one_of(|b: u8| !b.is_dec_digit()))
85 .parse_next(input)?
86 .is_some()
87 {}
88 }
89 Ok(count)
90 }
91
92 fn take_while(input: &mut &[u8]) -> ModalResult<usize> {
93 let mut count = 0;
94 while !input.is_empty() {
95 count += winnow::token::take_while(0.., AsChar::is_dec_digit)
96 .parse_next(input)?
97 .len();
98 let _ = winnow::token::take_while(0.., |b: u8| !b.is_dec_digit()).parse_next(input)?;
99 }
100 Ok(count)
101 }
102
103 fn repeat(input: &mut &[u8]) -> ModalResult<usize> {
104 let mut count = 0;
105 while !input.is_empty() {
106 count += winnow::combinator::repeat(0.., one_of(AsChar::is_dec_digit))
107 .map(|count: usize| count)
108 .parse_next(input)?;
109 let () =
110 winnow::combinator::repeat(0.., one_of(|b: u8| !b.is_dec_digit())).parse_next(input)?;
111 }
112 Ok(count)
113 }
114
115 const CONTIGUOUS: &str = "012345678901234567890123456789012345678901234567890123456789012345678901234567890123456789012345678901234567890123456789012345678901234567890123456789012345678901234567890123456789012345678901234567890123456789012345678901234567890123456789012345678901234567890123456789";
116 const INTERLEAVED: &str = "0123456789abc0123456789ab0123456789ab0123456789ab0123456789ab0123456789ab0123456789ab0123456789ab0123456789ab0123456789ab0123456789ab0123456789ab0123456789ab0123456789ab0123456789ab0123456789ab0123456789ab0123456789ab0123456789ab0123456789ab0123456789ab0123456789ab0123456789ab";
117 const CANADA: &str = include_str!("../third_party/nativejson-benchmark/data/canada.json");
118
119 criterion::criterion_group!(benches, iter);
120 criterion::criterion_main!(benches);