Vendor things

This commit is contained in:
John Doty 2024-03-08 11:03:01 -08:00
parent 5deceec006
commit 977e3c17e5
19434 changed files with 10682014 additions and 0 deletions

View file

@ -0,0 +1,114 @@
use criterion::black_box;
use winnow::combinator::alt;
use winnow::combinator::repeat;
use winnow::prelude::*;
use winnow::token::take_till;
use winnow::token::take_while;
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("slice", name), &len, |b, _| {
b.iter(|| black_box(parser_slice.parse_peek(black_box(sample)).unwrap()));
});
group.bench_with_input(criterion::BenchmarkId::new("array", name), &len, |b, _| {
b.iter(|| black_box(parser_array.parse_peek(black_box(sample)).unwrap()));
});
group.bench_with_input(criterion::BenchmarkId::new("tuple", name), &len, |b, _| {
b.iter(|| black_box(parser_tuple.parse_peek(black_box(sample)).unwrap()));
});
group.bench_with_input(
criterion::BenchmarkId::new("closure-or", name),
&len,
|b, _| {
b.iter(|| black_box(parser_closure_or.parse_peek(black_box(sample)).unwrap()));
},
);
group.bench_with_input(
criterion::BenchmarkId::new("closure-matches", name),
&len,
|b, _| {
b.iter(|| {
black_box(
parser_closure_matches
.parse_peek(black_box(sample))
.unwrap(),
)
});
},
);
}
group.finish();
}
fn parser_slice(input: &mut &str) -> PResult<usize> {
let contains = &['0', '1', '2', '3', '4', '5', '6', '7', '8', '9'][..];
repeat(
0..,
alt((take_while(1.., contains), take_till(1.., contains))),
)
.parse_next(input)
}
fn parser_array(input: &mut &str) -> PResult<usize> {
let contains = ['0', '1', '2', '3', '4', '5', '6', '7', '8', '9'];
repeat(
0..,
alt((take_while(1.., contains), take_till(1.., contains))),
)
.parse_next(input)
}
fn parser_tuple(input: &mut &str) -> PResult<usize> {
let contains = ('0', '1', '2', '3', '4', '5', '6', '7', '8', '9');
repeat(
0..,
alt((take_while(1.., contains), take_till(1.., contains))),
)
.parse_next(input)
}
fn parser_closure_or(input: &mut &str) -> PResult<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'
};
repeat(
0..,
alt((take_while(1.., contains), take_till(1.., contains))),
)
.parse_next(input)
}
fn parser_closure_matches(input: &mut &str) -> PResult<usize> {
let contains = |c: char| matches!(c, '0' | '1' | '2' | '3' | '4' | '5' | '6' | '7' | '8' | '9');
repeat(
0..,
alt((take_while(1.., contains), take_till(1.., 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);

View file

@ -0,0 +1,50 @@
use criterion::black_box;
use winnow::combinator::repeat;
use winnow::prelude::*;
use winnow::token::take_until;
fn find_slice(c: &mut criterion::Criterion) {
let empty = "";
let start_byte = "\r".repeat(100);
let start_slice = "\r\n".repeat(100);
let small = format!("{:>10}\r\n", "").repeat(100);
let large = format!("{:>10000}\r\n", "").repeat(100);
let data = [
("empty", (empty, empty)),
("start", (&start_byte, &start_slice)),
("medium", (&small, &small)),
("large", (&large, &large)),
];
let mut group = c.benchmark_group("find_slice");
for (name, samples) in data {
group.bench_with_input(
criterion::BenchmarkId::new("byte", name),
samples.0,
|b, sample| {
b.iter(|| black_box(parser_byte.parse_peek(black_box(sample)).unwrap()));
},
);
group.bench_with_input(
criterion::BenchmarkId::new("slice", name),
samples.1,
|b, sample| {
b.iter(|| black_box(parser_slice.parse_peek(black_box(sample)).unwrap()));
},
);
}
group.finish();
}
fn parser_byte(input: &mut &str) -> PResult<usize> {
repeat(0.., (take_until(0.., "\r"), "\r")).parse_next(input)
}
fn parser_slice(input: &mut &str) -> PResult<usize> {
repeat(0.., (take_until(0.., "\r\n"), "\r\n")).parse_next(input)
}
criterion::criterion_group!(benches, find_slice);
criterion::criterion_main!(benches);

View file

@ -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);

View file

@ -0,0 +1,133 @@
use criterion::black_box;
use winnow::combinator::repeat;
use winnow::prelude::*;
use winnow::token::one_of;
use winnow::token::tag;
fn next_slice(c: &mut criterion::Criterion) {
let mut group = c.benchmark_group("next_slice");
let name = "ascii";
let sample = "h".repeat(100);
let sample = sample.as_str();
group.bench_with_input(
criterion::BenchmarkId::new("char", name),
sample,
|b, sample| {
b.iter(|| black_box(parser_ascii_char.parse_peek(black_box(sample)).unwrap()));
},
);
group.bench_with_input(
criterion::BenchmarkId::new("str", name),
sample,
|b, sample| {
b.iter(|| black_box(parser_ascii_str.parse_peek(black_box(sample)).unwrap()));
},
);
group.bench_with_input(
criterion::BenchmarkId::new("one_of", name),
sample,
|b, sample| {
b.iter(|| black_box(parser_ascii_one_of.parse_peek(black_box(sample)).unwrap()));
},
);
group.bench_with_input(
criterion::BenchmarkId::new("tag_char", name),
sample,
|b, sample| {
b.iter(|| black_box(parser_ascii_tag_char.parse_peek(black_box(sample)).unwrap()));
},
);
group.bench_with_input(
criterion::BenchmarkId::new("tag_str", name),
sample,
|b, sample| {
b.iter(|| black_box(parser_ascii_tag_str.parse_peek(black_box(sample)).unwrap()));
},
);
let name = "utf8";
let sample = "🧑".repeat(100);
let sample = sample.as_str();
group.bench_with_input(
criterion::BenchmarkId::new("char", name),
sample,
|b, sample| {
b.iter(|| black_box(parser_utf8_char.parse_peek(black_box(sample)).unwrap()));
},
);
group.bench_with_input(
criterion::BenchmarkId::new("str", name),
sample,
|b, sample| {
b.iter(|| black_box(parser_utf8_str.parse_peek(black_box(sample)).unwrap()));
},
);
group.bench_with_input(
criterion::BenchmarkId::new("one_of", name),
sample,
|b, sample| {
b.iter(|| black_box(parser_utf8_one_of.parse_peek(black_box(sample)).unwrap()));
},
);
group.bench_with_input(
criterion::BenchmarkId::new("tag_char", name),
sample,
|b, sample| {
b.iter(|| black_box(parser_utf8_tag_char.parse_peek(black_box(sample)).unwrap()));
},
);
group.bench_with_input(
criterion::BenchmarkId::new("tag_str", name),
sample,
|b, sample| {
b.iter(|| black_box(parser_utf8_tag_str.parse_peek(black_box(sample)).unwrap()));
},
);
group.finish();
}
fn parser_ascii_char(input: &mut &str) -> PResult<usize> {
repeat(0.., 'h').parse_next(input)
}
fn parser_ascii_str(input: &mut &str) -> PResult<usize> {
repeat(0.., "h").parse_next(input)
}
fn parser_ascii_one_of(input: &mut &str) -> PResult<usize> {
repeat(0.., one_of('h')).parse_next(input)
}
fn parser_ascii_tag_char(input: &mut &str) -> PResult<usize> {
repeat(0.., tag('h')).parse_next(input)
}
fn parser_ascii_tag_str(input: &mut &str) -> PResult<usize> {
repeat(0.., tag("h")).parse_next(input)
}
fn parser_utf8_char(input: &mut &str) -> PResult<usize> {
repeat(0.., '🧑').parse_next(input)
}
fn parser_utf8_str(input: &mut &str) -> PResult<usize> {
repeat(0.., "🧑").parse_next(input)
}
fn parser_utf8_one_of(input: &mut &str) -> PResult<usize> {
repeat(0.., one_of('🧑')).parse_next(input)
}
fn parser_utf8_tag_char(input: &mut &str) -> PResult<usize> {
repeat(0.., tag('🧑')).parse_next(input)
}
fn parser_utf8_tag_str(input: &mut &str) -> PResult<usize> {
repeat(0.., tag("🧑")).parse_next(input)
}
criterion::criterion_group!(benches, next_slice);
criterion::criterion_main!(benches);

View file

@ -0,0 +1,70 @@
#[macro_use]
extern crate criterion;
use criterion::Criterion;
use winnow::ascii::float;
use winnow::binary::be_u64;
use winnow::error::ErrMode;
use winnow::error::ErrorKind;
use winnow::error::InputError;
use winnow::error::ParserError;
use winnow::prelude::*;
use winnow::stream::ParseSlice;
type Stream<'i> = &'i [u8];
fn parser(i: &mut Stream<'_>) -> PResult<u64> {
be_u64.parse_next(i)
}
fn number(c: &mut Criterion) {
let data = [1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12];
parser
.parse_peek(&data[..])
.expect("should parse correctly");
c.bench_function("number", move |b| {
b.iter(|| parser.parse_peek(&data[..]).unwrap());
});
}
fn float_bytes(c: &mut Criterion) {
println!(
"float_bytes result: {:?}",
float::<_, f64, InputError<_>>.parse_peek(&b"-1.234E-12"[..])
);
c.bench_function("float bytes", |b| {
b.iter(|| float::<_, f64, InputError<_>>.parse_peek(&b"-1.234E-12"[..]));
});
}
fn float_str(c: &mut Criterion) {
println!(
"float_str result: {:?}",
float::<_, f64, InputError<_>>.parse_peek("-1.234E-12")
);
c.bench_function("float str", |b| {
b.iter(|| float::<_, f64, InputError<_>>.parse_peek("-1.234E-12"));
});
}
fn std_float(input: &mut &[u8]) -> PResult<f64> {
match input.parse_slice() {
Some(n) => Ok(n),
None => Err(ErrMode::from_error_kind(input, ErrorKind::Slice)),
}
}
fn std_float_bytes(c: &mut Criterion) {
println!(
"std_float_bytes result: {:?}",
std_float.parse_peek(&b"-1.234E-12"[..])
);
c.bench_function("std_float bytes", |b| {
b.iter(|| std_float.parse_peek(&b"-1.234E-12"[..]));
});
}
criterion_group!(benches, number, float_bytes, std_float_bytes, float_str);
criterion_main!(benches);