diff options
author | Daniel Baumann <daniel.baumann@progress-linux.org> | 2024-05-18 02:49:50 +0000 |
---|---|---|
committer | Daniel Baumann <daniel.baumann@progress-linux.org> | 2024-05-18 02:49:50 +0000 |
commit | 9835e2ae736235810b4ea1c162ca5e65c547e770 (patch) | |
tree | 3fcebf40ed70e581d776a8a4c65923e8ec20e026 /vendor/winnow/benches | |
parent | Releasing progress-linux version 1.70.0+dfsg2-1~progress7.99u1. (diff) | |
download | rustc-9835e2ae736235810b4ea1c162ca5e65c547e770.tar.xz rustc-9835e2ae736235810b4ea1c162ca5e65c547e770.zip |
Merging upstream version 1.71.1+dfsg1.
Signed-off-by: Daniel Baumann <daniel.baumann@progress-linux.org>
Diffstat (limited to 'vendor/winnow/benches')
-rw-r--r-- | vendor/winnow/benches/contains_token.rs | 102 | ||||
-rw-r--r-- | vendor/winnow/benches/number.rs | 70 |
2 files changed, 172 insertions, 0 deletions
diff --git a/vendor/winnow/benches/contains_token.rs b/vendor/winnow/benches/contains_token.rs new file mode 100644 index 000000000..67397baf7 --- /dev/null +++ b/vendor/winnow/benches/contains_token.rs @@ -0,0 +1,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); diff --git a/vendor/winnow/benches/number.rs b/vendor/winnow/benches/number.rs new file mode 100644 index 000000000..e801c8c41 --- /dev/null +++ b/vendor/winnow/benches/number.rs @@ -0,0 +1,70 @@ +#[macro_use] +extern crate criterion; + +use criterion::Criterion; + +use winnow::character::float; +use winnow::error::ErrMode; +use winnow::error::Error; +use winnow::error::ErrorKind; +use winnow::number::be_u64; +use winnow::prelude::*; +use winnow::stream::ParseSlice; + +type Stream<'i> = &'i [u8]; + +fn parser(i: Stream<'_>) -> IResult<Stream<'_>, u64> { + be_u64(i) +} + +fn number(c: &mut Criterion) { + let data = [1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12]; + + parser(&data[..]).expect("should parse correctly"); + c.bench_function("number", move |b| { + b.iter(|| parser(&data[..]).unwrap()); + }); +} + +fn float_bytes(c: &mut Criterion) { + println!( + "float_bytes result: {:?}", + float::<_, f64, Error<_>>(&b"-1.234E-12"[..]) + ); + c.bench_function("float bytes", |b| { + b.iter(|| float::<_, f64, Error<_>>(&b"-1.234E-12"[..])); + }); +} + +fn float_str(c: &mut Criterion) { + println!( + "float_str result: {:?}", + float::<_, f64, Error<_>>("-1.234E-12") + ); + c.bench_function("float str", |b| { + b.iter(|| float::<_, f64, Error<_>>("-1.234E-12")); + }); +} + +fn std_float(input: &[u8]) -> IResult<&[u8], f64, Error<&[u8]>> { + match input.parse_slice() { + Some(n) => Ok((&[], n)), + None => Err(ErrMode::Backtrack(Error { + input, + kind: ErrorKind::Slice, + })), + } +} + +fn std_float_bytes(c: &mut Criterion) { + println!( + "std_float_bytes result: {:?}", + std_float(&b"-1.234E-12"[..]) + ); + c.bench_function("std_float bytes", |b| { + b.iter(|| std_float(&b"-1.234E-12"[..])); + }); +} + +criterion_group!(benches, number, float_bytes, std_float_bytes, float_str); +criterion_main!(benches); |