diff options
author | Daniel Baumann <daniel.baumann@progress-linux.org> | 2024-05-30 18:31:36 +0000 |
---|---|---|
committer | Daniel Baumann <daniel.baumann@progress-linux.org> | 2024-05-30 18:31:36 +0000 |
commit | e02c5b5930c2c9ba3e5423fe12e2ef0155017297 (patch) | |
tree | fd60ebbbb5299e16e5fca8c773ddb74f764760db /vendor/winnow/benches/iter.rs | |
parent | Adding debian version 1.73.0+dfsg1-1. (diff) | |
download | rustc-e02c5b5930c2c9ba3e5423fe12e2ef0155017297.tar.xz rustc-e02c5b5930c2c9ba3e5423fe12e2ef0155017297.zip |
Merging upstream version 1.74.1+dfsg1.
Signed-off-by: Daniel Baumann <daniel.baumann@progress-linux.org>
Diffstat (limited to 'vendor/winnow/benches/iter.rs')
-rw-r--r-- | vendor/winnow/benches/iter.rs | 120 |
1 files changed, 120 insertions, 0 deletions
diff --git a/vendor/winnow/benches/iter.rs b/vendor/winnow/benches/iter.rs new file mode 100644 index 000000000..0a0d5ff3d --- /dev/null +++ b/vendor/winnow/benches/iter.rs @@ -0,0 +1,120 @@ +use criterion::black_box; + +use winnow::combinator::opt; +use winnow::prelude::*; +use winnow::stream::AsChar; +use winnow::stream::Stream as _; +use winnow::token::one_of; + +fn iter(c: &mut criterion::Criterion) { + let data = [ + ("contiguous", CONTIGUOUS.as_bytes()), + ("interleaved", INTERLEAVED.as_bytes()), + ("canada", CANADA.as_bytes()), + ]; + let mut group = c.benchmark_group("iter"); + for (name, sample) in data { + let len = sample.len(); + group.throughput(criterion::Throughput::Bytes(len as u64)); + + group.bench_with_input( + criterion::BenchmarkId::new("iterate", name), + &len, + |b, _| { + b.iter(|| black_box(iterate.parse_peek(black_box(sample)).unwrap())); + }, + ); + group.bench_with_input( + criterion::BenchmarkId::new("next_token", name), + &len, + |b, _| { + b.iter(|| black_box(next_token.parse_peek(black_box(sample)).unwrap())); + }, + ); + group.bench_with_input( + criterion::BenchmarkId::new("opt(one_of)", name), + &len, + |b, _| { + b.iter(|| black_box(opt_one_of.parse_peek(black_box(sample)).unwrap())); + }, + ); + group.bench_with_input( + criterion::BenchmarkId::new("take_while", name), + &len, + |b, _| { + b.iter(|| black_box(take_while.parse_peek(black_box(sample)).unwrap())); + }, + ); + group.bench_with_input(criterion::BenchmarkId::new("repeat", name), &len, |b, _| { + b.iter(|| black_box(repeat.parse_peek(black_box(sample)).unwrap())); + }); + } + group.finish(); +} + +fn iterate(input: &mut &[u8]) -> PResult<usize> { + let mut count = 0; + for byte in input.iter() { + if byte.is_dec_digit() { + count += 1; + } + } + input.finish(); + Ok(count) +} + +fn next_token(input: &mut &[u8]) -> PResult<usize> { + let mut count = 0; + while let Some(byte) = input.next_token() { + if byte.is_dec_digit() { + count += 1; + } + } + Ok(count) +} + +fn opt_one_of(input: &mut &[u8]) -> PResult<usize> { + let mut count = 0; + while !input.is_empty() { + while opt(one_of(AsChar::is_dec_digit)) + .parse_next(input)? + .is_some() + { + count += 1; + } + while opt(one_of(|b: u8| !b.is_dec_digit())) + .parse_next(input)? + .is_some() + {} + } + Ok(count) +} + +fn take_while(input: &mut &[u8]) -> PResult<usize> { + let mut count = 0; + while !input.is_empty() { + count += winnow::token::take_while(0.., AsChar::is_dec_digit) + .parse_next(input)? + .len(); + let _ = winnow::token::take_while(0.., |b: u8| !b.is_dec_digit()).parse_next(input)?; + } + Ok(count) +} + +fn repeat(input: &mut &[u8]) -> PResult<usize> { + let mut count = 0; + while !input.is_empty() { + count += winnow::combinator::repeat(0.., one_of(AsChar::is_dec_digit)) + .map(|count: usize| count) + .parse_next(input)?; + winnow::combinator::repeat(0.., one_of(|b: u8| !b.is_dec_digit())).parse_next(input)?; + } + Ok(count) +} + +const CONTIGUOUS: &str = "012345678901234567890123456789012345678901234567890123456789012345678901234567890123456789012345678901234567890123456789012345678901234567890123456789012345678901234567890123456789012345678901234567890123456789012345678901234567890123456789012345678901234567890123456789"; +const INTERLEAVED: &str = "0123456789abc0123456789ab0123456789ab0123456789ab0123456789ab0123456789ab0123456789ab0123456789ab0123456789ab0123456789ab0123456789ab0123456789ab0123456789ab0123456789ab0123456789ab0123456789ab0123456789ab0123456789ab0123456789ab0123456789ab0123456789ab0123456789ab0123456789ab"; +const CANADA: &str = include_str!("../third_party/nativejson-benchmark/data/canada.json"); + +criterion::criterion_group!(benches, iter); +criterion::criterion_main!(benches); |