diff --git a/simdnbt/Cargo.toml b/simdnbt/Cargo.toml index 6cff479..39aced1 100644 --- a/simdnbt/Cargo.toml +++ b/simdnbt/Cargo.toml @@ -42,7 +42,10 @@ debug = true [[bench]] harness = false -name = "nbt" +name = "nbt_borrow" +[[bench]] +harness = false +name = "nbt_owned" [[bench]] harness = false diff --git a/simdnbt/benches/nbt.rs b/simdnbt/benches/nbt_borrow.rs similarity index 96% rename from simdnbt/benches/nbt.rs rename to simdnbt/benches/nbt_borrow.rs index 5705fbd..e8b1232 100644 --- a/simdnbt/benches/nbt.rs +++ b/simdnbt/benches/nbt_borrow.rs @@ -21,7 +21,7 @@ fn bench_file(filename: &str, c: &mut Criterion) { let mut decoded_src_stream = Cursor::new(&decoded_src[..]); - let mut group = c.benchmark_group(format!("nbt/{filename}")); + let mut group = c.benchmark_group(format!("nbt_borrow/{filename}")); group.throughput(Throughput::Bytes(decoded_src.len() as u64)); diff --git a/simdnbt/benches/nbt_owned.rs b/simdnbt/benches/nbt_owned.rs new file mode 100644 index 0000000..3e46f5f --- /dev/null +++ b/simdnbt/benches/nbt_owned.rs @@ -0,0 +1,73 @@ +use criterion::{black_box, criterion_group, criterion_main, Criterion, Throughput}; +use flate2::read::GzDecoder; +use std::{ + fs::File, + io::{Cursor, Read}, +}; + +fn bench_file(filename: &str, c: &mut Criterion) { + let mut file = File::open(format!("tests/{filename}")).unwrap(); + let mut contents = Vec::new(); + file.read_to_end(&mut contents).unwrap(); + let mut src = &contents[..]; + + // decode the original src so most of the time isn't spent on unzipping + let mut decoded_src_decoder = GzDecoder::new(&mut src); + let mut decoded_src = Vec::new(); + if decoded_src_decoder.read_to_end(&mut decoded_src).is_err() { + // oh probably wasn't gzipped then + decoded_src = contents; + } + + let mut decoded_src_stream = Cursor::new(&decoded_src[..]); + + let mut group = c.benchmark_group(format!("nbt_owned/{filename}")); + + group.throughput(Throughput::Bytes(decoded_src.len() as u64)); + + group.bench_function("Decode", |b| { + b.iter(|| { + black_box(simdnbt::owned::Nbt::read(&mut decoded_src_stream).unwrap()); + decoded_src_stream.set_position(0); + }) + }); + + let nbt = simdnbt::owned::Nbt::read(&mut decoded_src_stream) + .unwrap() + .unwrap(); + group.bench_function("Get", |b| { + b.iter(|| { + let level = nbt.compound("abilities").unwrap(); + for (k, _) in level.iter() { + black_box(level.get(black_box(&k.to_str()))); + } + }) + }); + group.bench_function("Encode", |b| { + b.iter(|| { + let mut out = Vec::new(); + nbt.write(&mut out); + black_box(out); + }) + }); + group.finish(); +} + +#[global_allocator] +static GLOBAL: mimalloc::MiMalloc = mimalloc::MiMalloc; + +fn bench(c: &mut Criterion) { + bench_file("bigtest.nbt", c); + bench_file("simple_player.dat", c); + bench_file("complex_player.dat", c); + bench_file("level.dat", c); + + // bench_file("stringtest.nbt", c); + // bench_file("inttest16.nbt", c); + + // bench_file("inttest1023.nbt", c); + // bench_file("inttest3.nbt", c); +} + +criterion_group!(nbt, bench); +criterion_main!(nbt);