diff --git a/Cargo.toml b/Cargo.toml index 1a5322956..4fefd32fd 100644 --- a/Cargo.toml +++ b/Cargo.toml @@ -73,7 +73,7 @@ valence_world_border = { workspace = true, optional = true } [dev-dependencies] anyhow.workspace = true clap.workspace = true -criterion.workspace = true +divan.workspace = true flume.workspace = true noise.workspace = true # For the terrain example. tracing.workspace = true @@ -129,9 +129,9 @@ bytes = "1.2.1" cesu8 = "1.1.0" cfb8 = "0.8.1" clap = { version = "4.0.30", features = ["derive"] } -criterion = "0.5.1" derive_more = "1.0.0-beta.3" directories = "5.0.0" +divan = "0.1.14" eframe = { version = "0.22.0", default-features = false } egui = "0.22.0" egui_dock = "0.6" diff --git a/benches/block.rs b/benches/block.rs index cdecba853..ebd35e58f 100644 --- a/benches/block.rs +++ b/benches/block.rs @@ -1,91 +1,93 @@ use std::hint::black_box; -use criterion::Criterion; +use divan::Bencher; use valence::block::{BlockKind, BlockState, PropName, PropValue}; use valence::ItemKind; -pub(crate) fn block(c: &mut Criterion) { - let mut group = c.benchmark_group("block"); - - let states = BlockKind::ALL.map(BlockKind::to_state); - - group.bench_function("BlockState::from_kind", |b| { - b.iter(|| { - for kind in black_box(BlockKind::ALL) { - black_box(BlockState::from_kind(kind)); - } - }); +#[divan::bench] +pub fn from_kind(bencher: Bencher) { + bencher.bench(|| { + for kind in black_box(BlockKind::ALL) { + black_box(BlockState::from_kind(kind)); + } }); - - group.bench_function("BlockState::to_kind", |b| { - b.iter(|| { - for state in black_box(states) { - black_box(state.to_kind()); - } - }); +} +#[divan::bench] +pub fn to_kind(bencher: Bencher) { + let states = BlockKind::ALL.map(BlockKind::to_state); + bencher.bench(|| { + for state in black_box(states) { + black_box(state.to_kind()); + } }); - - group.bench_function("BlockState::get", |b| { - b.iter(|| { - for state in black_box(states) { - black_box(state.get(PropName::Note)); - } - }); +} +#[divan::bench] +pub fn get_prop(bencher: Bencher) { + let states = BlockKind::ALL.map(BlockKind::to_state); + bencher.bench(|| { + for state in black_box(states) { + black_box(state.get(PropName::Note)); + } }); - - group.bench_function("BlockState::set", |b| { - b.iter(|| { - for state in black_box(states) { - black_box(state.set(PropName::Note, PropValue::Didgeridoo)); - } - }); +} +#[divan::bench] +pub fn set_prop(bencher: Bencher) { + let states = BlockKind::ALL.map(BlockKind::to_state); + bencher.bench(|| { + for state in black_box(states) { + black_box(state.set(PropName::Note, PropValue::Didgeridoo)); + } }); - - group.bench_function("BlockState::is_liquid", |b| { - b.iter(|| { - for state in black_box(states) { - black_box(state.is_liquid()); - } - }); +} +#[divan::bench] +pub fn is_liquid(bencher: Bencher) { + let states = BlockKind::ALL.map(BlockKind::to_state); + bencher.bench(|| { + for state in black_box(states) { + black_box(state.is_liquid()); + } }); - - group.bench_function("BlockState::is_opaque", |b| { - b.iter(|| { - for state in black_box(states) { - black_box(state.is_opaque()); - } - }) +} +#[divan::bench] +pub fn is_opaque(bencher: Bencher) { + let states = BlockKind::ALL.map(BlockKind::to_state); + bencher.bench(|| { + for state in black_box(states) { + black_box(state.is_opaque()); + } }); - - group.bench_function("BlockState::is_replaceable", |b| { - b.iter(|| { - for state in black_box(states) { - black_box(state.is_replaceable()); - } - }) +} +#[divan::bench] +pub fn is_replaceable(bencher: Bencher) { + let states = BlockKind::ALL.map(BlockKind::to_state); + bencher.bench(|| { + for state in black_box(states) { + black_box(state.is_replaceable()); + } }); - - group.bench_function("BlockState::luminance", |b| { - b.iter(|| { - for state in black_box(states) { - black_box(state.luminance()); - } - }) +} +#[divan::bench] +pub fn luminance(bencher: Bencher) { + let states = BlockKind::ALL.map(BlockKind::to_state); + bencher.bench(|| { + for state in black_box(states) { + black_box(state.luminance()); + } }); - - group.bench_function("BlockKind::to_item_kind", |b| { - b.iter(|| { - for kind in black_box(BlockKind::ALL) { - black_box(kind.to_item_kind()); - } - }); +} +#[divan::bench] +pub fn to_item_kind(bencher: Bencher) { + bencher.bench(|| { + for kind in black_box(BlockKind::ALL) { + black_box(kind.to_item_kind()); + } }); - - group.bench_function("BlockKind::from_item_kind", |b| { - b.iter(|| { - for kind in black_box(ItemKind::ALL) { - black_box(BlockKind::from_item_kind(kind)); - } - }); +} +#[divan::bench] +pub fn from_item_kind(bencher: Bencher) { + bencher.bench(|| { + for kind in black_box(ItemKind::ALL) { + black_box(BlockKind::from_item_kind(kind)); + } }); } diff --git a/benches/decode_array.rs b/benches/decode_array.rs index 7a0de3f86..028c82030 100644 --- a/benches/decode_array.rs +++ b/benches/decode_array.rs @@ -1,29 +1,27 @@ use std::hint::black_box; -use criterion::Criterion; +use divan::Bencher; use valence::protocol::{Decode, Encode}; -pub(crate) fn decode_array(c: &mut Criterion) { - let mut group = c.benchmark_group("decode_array"); - +#[divan::bench] +pub fn decode_small_array(bencher: Bencher) { let floats = [123.0, 456.0, 789.0]; let mut buf = [0_u8; 24]; floats.encode(buf.as_mut_slice()).unwrap(); - group.bench_function("<[f64; 3]>::decode", |b| { - b.iter(|| { - let mut r = black_box(buf.as_slice()); - let _ = black_box(<[f64; 3]>::decode(&mut r)); - }); + bencher.bench(|| { + let mut r = black_box(buf.as_slice()); + let _ = black_box(<[f64; 3]>::decode(&mut r)); }); +} +#[divan::bench] +pub fn decode_large_array(bencher: Bencher) { let bytes = [42; 4096]; - group.bench_function("<[u8; 4096]>::decode", |b| { - b.iter(|| { - let mut r = black_box(bytes.as_slice()); - let _ = black_box(<[u8; 4096]>::decode(&mut r)); - }) + bencher.bench(|| { + let mut r = black_box(bytes.as_slice()); + let _ = black_box(<[u8; 4096]>::decode(&mut r)); }); } diff --git a/benches/idle.rs b/benches/idle.rs index 4782f4744..3aa8e6a70 100644 --- a/benches/idle.rs +++ b/benches/idle.rs @@ -1,9 +1,10 @@ -use criterion::Criterion; +use divan::Bencher; use valence::prelude::*; /// Benches the performance of a single server tick while nothing much is /// happening. -pub(crate) fn idle_update(c: &mut Criterion) { +#[divan::bench] +pub fn idle_update(bencher: Bencher) { let mut app = App::new(); app.add_plugins(DefaultPlugins); @@ -12,10 +13,8 @@ pub(crate) fn idle_update(c: &mut Criterion) { // Run startup schedule. app.update(); - c.bench_function("idle_update", |b| { - b.iter(|| { - app.update(); - }); + bencher.bench_local(move || { + app.update(); }); } diff --git a/benches/main.rs b/benches/main.rs index d5068d546..284985e09 100644 --- a/benches/main.rs +++ b/benches/main.rs @@ -1,5 +1,3 @@ -use criterion::{criterion_group, criterion_main}; - mod anvil; mod block; mod decode_array; @@ -9,15 +7,6 @@ mod packet; mod var_int; mod var_long; -criterion_group! { - benches, - block::block, - decode_array::decode_array, - idle::idle_update, - packet::packet, - var_int::var_int, - var_long::var_long, - many_players::many_players, +fn main() { + divan::main(); } - -criterion_main!(benches); diff --git a/benches/many_players.rs b/benches/many_players.rs index 85d286e91..54d100af7 100644 --- a/benches/many_players.rs +++ b/benches/many_players.rs @@ -1,7 +1,7 @@ use std::time::Duration; use bevy_app::prelude::*; -use criterion::Criterion; +use divan::Bencher; use rand::Rng; use valence::entity::Position; use valence::keepalive::KeepaliveSettings; @@ -15,18 +15,17 @@ use valence::testing::create_mock_client; use valence::{ident, ChunkPos, DefaultPlugins, Hand, Server, ServerSettings}; use valence_server::CompressionThreshold; -pub(crate) fn many_players(c: &mut Criterion) { - run_many_players(c, "many_players", 3000, 16, 16); - run_many_players(c, "many_players_spread_out", 3000, 8, 200); +#[divan::bench] +fn many_players(bencher: Bencher) { + run_many_players(bencher, 3000, 16, 16); } -fn run_many_players( - c: &mut Criterion, - func_name: &str, - client_count: usize, - view_dist: u8, - world_size: i32, -) { +#[divan::bench] +fn many_players_spread_out(bencher: Bencher) { + run_many_players(bencher, 3000, 8, 200); +} + +fn run_many_players(bencher: Bencher, client_count: usize, view_dist: u8, world_size: i32) { let mut app = App::new(); app.insert_resource(ServerSettings { @@ -91,34 +90,32 @@ fn run_many_players( app.update(); - c.bench_function(func_name, |b| { - b.iter(|| { - let mut rng = rand::thread_rng(); + bencher.bench_local(|| { + let mut rng = rand::thread_rng(); - // Move the clients around randomly. They'll cross chunk borders and cause - // interesting things to happen. - for (id, helper) in &mut clients { - let pos = query.get(&app.world, *id).unwrap().get(); + // Move the clients around randomly. They'll cross chunk borders and cause + // interesting things to happen. + for (id, helper) in &mut clients { + let pos = query.get(&app.world, *id).unwrap().get(); - let offset = DVec3::new(rng.gen_range(-1.0..=1.0), 0.0, rng.gen_range(-1.0..=1.0)); + let offset = DVec3::new(rng.gen_range(-1.0..=1.0), 0.0, rng.gen_range(-1.0..=1.0)); - helper.send(&FullC2s { - position: pos + offset, - yaw: rng.gen_range(0.0..=360.0), - pitch: rng.gen_range(0.0..=360.0), - on_ground: rng.gen(), - }); + helper.send(&FullC2s { + position: pos + offset, + yaw: rng.gen_range(0.0..=360.0), + pitch: rng.gen_range(0.0..=360.0), + on_ground: rng.gen(), + }); - helper.send(&HandSwingC2s { hand: Hand::Main }); - } + helper.send(&HandSwingC2s { hand: Hand::Main }); + } - drop(rng); + drop(rng); - app.update(); // The important part. + app.update(); // The important part. - for (_, helper) in &mut clients { - helper.clear_received(); - } - }); + for (_, helper) in &mut clients { + helper.clear_received(); + } }); } diff --git a/benches/packet.rs b/benches/packet.rs index 55cfb1f41..560dd6e1f 100644 --- a/benches/packet.rs +++ b/benches/packet.rs @@ -1,7 +1,7 @@ use std::borrow::Cow; use std::hint::black_box; -use criterion::Criterion; +use divan::Bencher; use valence::nbt::{compound, List}; use valence::prelude::*; use valence::protocol::decode::PacketDecoder; @@ -12,10 +12,13 @@ use valence::text::IntoText; use valence_server::protocol::Velocity; use valence_server::CompressionThreshold; -pub(crate) fn packet(c: &mut Criterion) { - let mut group = c.benchmark_group("packet"); - - let mut encoder = PacketEncoder::new(); +pub fn setup<'a>() -> ( + PacketEncoder, + ChunkDataS2c<'a>, + PlayerListHeaderS2c<'a>, + EntitySpawnS2c, +) { + let encoder = PacketEncoder::new(); const BLOCKS_AND_BIOMES: [u8; 2000] = [0x80; 2000]; const SKY_LIGHT_ARRAYS: [FixedArray; 26] = [FixedArray([0xff; 2048]); 26]; @@ -57,190 +60,240 @@ pub(crate) fn packet(c: &mut Criterion) { velocity: Velocity([12, 34, 56]), }; - group.bench_function("encode_chunk_data", |b| { - b.iter(|| { - let encoder = black_box(&mut encoder); + ( + encoder, + chunk_data_packet, + player_list_header_packet, + spawn_entity_packet, + ) +} +#[divan::bench] +fn encode_chunk_data(bencher: Bencher) { + let (mut encoder, chunk_data_packet, _, _) = setup(); + bencher.bench_local(|| { + let encoder = black_box(&mut encoder); - encoder.clear(); - encoder.append_packet(&chunk_data_packet).unwrap(); + encoder.clear(); + encoder.append_packet(&chunk_data_packet).unwrap(); - black_box(encoder); - }); + black_box(encoder); }); +} - group.bench_function("encode_player_list_header", |b| { - b.iter(|| { - let encoder = black_box(&mut encoder); +#[divan::bench] +fn encode_player_list_header(bencher: Bencher) { + let (mut encoder, _, player_list_header_packet, _) = setup(); + bencher.bench_local(|| { + let encoder = black_box(&mut encoder); - encoder.clear(); - encoder.append_packet(&player_list_header_packet).unwrap(); + encoder.clear(); + encoder.append_packet(&player_list_header_packet).unwrap(); - black_box(encoder); - }); + black_box(encoder); }); +} - group.bench_function("encode_spawn_entity", |b| { - b.iter(|| { - let encoder = black_box(&mut encoder); +#[divan::bench] +fn encode_spawn_entity(bencher: Bencher) { + let (mut encoder, _, _, spawn_entity_packet) = setup(); + bencher.bench_local(|| { + let encoder = black_box(&mut encoder); - encoder.clear(); - encoder.append_packet(&spawn_entity_packet).unwrap(); + encoder.clear(); + encoder.append_packet(&spawn_entity_packet).unwrap(); - black_box(encoder); - }); + black_box(encoder); }); +} +#[divan::bench] +fn encode_chunk_data_compressed(bencher: Bencher) { + let (mut encoder, chunk_data_packet, _, _) = setup(); encoder.set_compression(CompressionThreshold(-1)); - group.bench_function("encode_chunk_data_compressed", |b| { - b.iter(|| { - let encoder = black_box(&mut encoder); + bencher.bench_local(|| { + let encoder = black_box(&mut encoder); - encoder.clear(); - encoder.append_packet(&chunk_data_packet).unwrap(); + encoder.clear(); + encoder.append_packet(&chunk_data_packet).unwrap(); - black_box(encoder); - }); + black_box(encoder); }); +} - group.bench_function("encode_player_list_header_compressed", |b| { - b.iter(|| { - let encoder = black_box(&mut encoder); +#[divan::bench] +fn encode_player_list_header_compressed(bencher: Bencher) { + let (mut encoder, _, player_list_header_packet, _) = setup(); + encoder.set_compression(CompressionThreshold(-1)); + + bencher.bench_local(|| { + let encoder = black_box(&mut encoder); - encoder.clear(); - encoder.append_packet(&player_list_header_packet).unwrap(); + encoder.clear(); + encoder.append_packet(&player_list_header_packet).unwrap(); - black_box(encoder); - }); + black_box(encoder); }); +} + +#[divan::bench] +fn encode_spawn_entity_compressed(bencher: Bencher) { + let (mut encoder, _, _, spawn_entity_packet) = setup(); + encoder.set_compression(CompressionThreshold(-1)); - group.bench_function("encode_spawn_entity_compressed", |b| { - b.iter(|| { - let encoder = black_box(&mut encoder); + bencher.bench_local(|| { + let encoder = black_box(&mut encoder); - encoder.clear(); - encoder.append_packet(&spawn_entity_packet).unwrap(); + encoder.clear(); + encoder.append_packet(&spawn_entity_packet).unwrap(); - black_box(encoder); - }); + black_box(encoder); }); +} + +#[divan::bench] +fn decode_chunk_data(bencher: Bencher) { + let (_, chunk_data_packet, _, _) = setup(); let mut decoder = PacketDecoder::new(); let mut packet_buf = vec![]; PacketWriter::new(&mut packet_buf, CompressionThreshold(-1)).write_packet(&chunk_data_packet); + bencher.bench_local(|| { + let decoder = black_box(&mut decoder); + + decoder.queue_slice(&packet_buf); + decoder + .try_next_packet() + .unwrap() + .unwrap() + .decode::() + .unwrap(); + + black_box(decoder); + }); +} - group.bench_function("decode_chunk_data", |b| { - b.iter(|| { - let decoder = black_box(&mut decoder); - - decoder.queue_slice(&packet_buf); - decoder - .try_next_packet() - .unwrap() - .unwrap() - .decode::() - .unwrap(); +#[divan::bench] +fn decode_player_list_header(bencher: Bencher) { + let (_, _, player_list_header_packet, _) = setup(); - black_box(decoder); - }); - }); + let mut decoder = PacketDecoder::new(); + let mut packet_buf = vec![]; - packet_buf.clear(); PacketWriter::new(&mut packet_buf, CompressionThreshold(-1)) .write_packet(&player_list_header_packet); + bencher.bench_local(move || { + let decoder = black_box(&mut decoder); + + decoder.queue_slice(&packet_buf); + decoder + .try_next_packet() + .unwrap() + .unwrap() + .decode::() + .unwrap(); + + black_box(decoder); + }); +} - group.bench_function("decode_player_list_header", |b| { - b.iter(|| { - let decoder = black_box(&mut decoder); - - decoder.queue_slice(&packet_buf); - decoder - .try_next_packet() - .unwrap() - .unwrap() - .decode::() - .unwrap(); +#[divan::bench] +fn decode_entity_spawn(bencher: Bencher) { + let (_, _, _, spawn_entity_packet) = setup(); - black_box(decoder); - }); - }); + let mut decoder = PacketDecoder::new(); + let mut packet_buf = vec![]; - packet_buf.clear(); PacketWriter::new(&mut packet_buf, CompressionThreshold(-1)).write_packet(&spawn_entity_packet); + bencher.bench_local(|| { + let decoder = black_box(&mut decoder); + + decoder.queue_slice(&packet_buf); + decoder + .try_next_packet() + .unwrap() + .unwrap() + .decode::() + .unwrap(); + + black_box(decoder); + }); +} - group.bench_function("decode_entity_spawn", |b| { - b.iter(|| { - let decoder = black_box(&mut decoder); - - decoder.queue_slice(&packet_buf); - decoder - .try_next_packet() - .unwrap() - .unwrap() - .decode::() - .unwrap(); +#[divan::bench] +fn decode_chunk_data_compressed(bencher: Bencher) { + let (_, chunk_data_packet, _, _) = setup(); - black_box(decoder); - }); - }); + let mut decoder = PacketDecoder::new(); + let mut packet_buf = vec![]; decoder.set_compression(256.into()); - packet_buf.clear(); PacketWriter::new(&mut packet_buf, 256.into()).write_packet(&chunk_data_packet); - group.bench_function("decode_chunk_data_compressed", |b| { - b.iter(|| { - let decoder = black_box(&mut decoder); + bencher.bench_local(|| { + let decoder = black_box(&mut decoder); - decoder.queue_slice(&packet_buf); - decoder - .try_next_packet() - .unwrap() - .unwrap() - .decode::() - .unwrap(); + decoder.queue_slice(&packet_buf); + decoder + .try_next_packet() + .unwrap() + .unwrap() + .decode::() + .unwrap(); - black_box(decoder); - }); + black_box(decoder); }); +} + +#[divan::bench] +fn decode_player_list_header_compressed(bencher: Bencher) { + let (_, _, player_list_header_packet, _) = setup(); + + let mut decoder = PacketDecoder::new(); + let mut packet_buf = vec![]; + + decoder.set_compression(256.into()); - packet_buf.clear(); PacketWriter::new(&mut packet_buf, 256.into()).write_packet(&player_list_header_packet); - group.bench_function("decode_player_list_header_compressed", |b| { - b.iter(|| { - let decoder = black_box(&mut decoder); + bencher.bench_local(|| { + let decoder = black_box(&mut decoder); - decoder.queue_slice(&packet_buf); - decoder - .try_next_packet() - .unwrap() - .unwrap() - .decode::() - .unwrap(); + decoder.queue_slice(&packet_buf); + decoder + .try_next_packet() + .unwrap() + .unwrap() + .decode::() + .unwrap(); - black_box(decoder); - }); + black_box(decoder); }); +} - packet_buf.clear(); +#[divan::bench] +fn decode_spawn_data_compressed(bencher: Bencher) { + let (_, _, _, spawn_entity_packet) = setup(); + + let mut decoder = PacketDecoder::new(); + let mut packet_buf = vec![]; + + decoder.set_compression(256.into()); PacketWriter::new(&mut packet_buf, 256.into()).write_packet(&spawn_entity_packet); - group.bench_function("decode_spawn_entity_compressed", |b| { - b.iter(|| { - let decoder = black_box(&mut decoder); + bencher.bench_local(|| { + let decoder = black_box(&mut decoder); - decoder.queue_slice(&packet_buf); - decoder - .try_next_packet() - .unwrap() - .unwrap() - .decode::() - .unwrap(); + decoder.queue_slice(&packet_buf); + decoder + .try_next_packet() + .unwrap() + .unwrap() + .decode::() + .unwrap(); - black_box(decoder); - }); + black_box(decoder); }); } diff --git a/benches/var_int.rs b/benches/var_int.rs index f97d582ad..b1f67f12f 100644 --- a/benches/var_int.rs +++ b/benches/var_int.rs @@ -1,37 +1,33 @@ use std::hint::black_box; -use criterion::Criterion; +use divan::Bencher; use rand::Rng; use valence::protocol::{Decode, Encode, VarInt}; -pub(crate) fn var_int(c: &mut Criterion) { - let mut group = c.benchmark_group("varint"); - +#[divan::bench] +fn varint_encode(bencher: Bencher) { let mut rng = rand::thread_rng(); - group.bench_function("VarInt::encode", |b| { - b.iter_with_setup( - || rng.gen(), - |i| { - let i: i32 = black_box(i); + bencher.with_inputs(|| rng.gen()).bench_local_values(|i| { + let i: i32 = black_box(i); - let mut buf = [0; VarInt::MAX_SIZE]; - let _ = black_box(VarInt(i).encode(buf.as_mut_slice())); - }, - ); + let mut buf = [0; VarInt::MAX_SIZE]; + let _ = black_box(VarInt(i).encode(buf.as_mut_slice())); }); +} - group.bench_function("VarInt::decode", |b| { - b.iter_with_setup( - || { - let mut buf = [0; VarInt::MAX_SIZE]; - VarInt(rng.gen()).encode(buf.as_mut_slice()).unwrap(); - buf - }, - |buf| { - let mut r = black_box(buf.as_slice()); - let _ = black_box(VarInt::decode(&mut r)); - }, - ) - }); +#[divan::bench] +fn varint_decode(bencher: Bencher) { + let mut rng = rand::thread_rng(); + + bencher + .with_inputs(|| { + let mut buf = [0; VarInt::MAX_SIZE]; + VarInt(rng.gen()).encode(buf.as_mut_slice()).unwrap(); + buf + }) + .bench_local_values(|buf| { + let mut r = black_box(buf.as_slice()); + let _ = black_box(VarInt::decode(&mut r)); + }); } diff --git a/benches/var_long.rs b/benches/var_long.rs index 340613fd8..a896ade52 100644 --- a/benches/var_long.rs +++ b/benches/var_long.rs @@ -1,37 +1,33 @@ use std::hint::black_box; -use criterion::Criterion; +use divan::Bencher; use rand::Rng; use valence::protocol::{Decode, Encode, VarLong}; -pub(crate) fn var_long(c: &mut Criterion) { - let mut group = c.benchmark_group("varlong"); - +#[divan::bench] +fn varlong_encode(bencher: Bencher) { let mut rng = rand::thread_rng(); - group.bench_function("VarLong::encode", |b| { - b.iter_with_setup( - || rng.gen(), - |i| { - let i: i64 = black_box(i); + bencher.with_inputs(|| rng.gen()).bench_local_values(|i| { + let i: i64 = black_box(i); - let mut buf = [0; VarLong::MAX_SIZE]; - let _ = black_box(VarLong(i).encode(buf.as_mut_slice())); - }, - ); + let mut buf = [0; VarLong::MAX_SIZE]; + let _ = black_box(VarLong(i).encode(buf.as_mut_slice())); }); +} - group.bench_function("VarLong::decode", |b| { - b.iter_with_setup( - || { - let mut buf = [0; VarLong::MAX_SIZE]; - VarLong(rng.gen()).encode(buf.as_mut_slice()).unwrap(); - buf - }, - |buf| { - let mut r = black_box(buf.as_slice()); - let _ = black_box(VarLong::decode(&mut r)); - }, - ) - }); +#[divan::bench] +fn varlong_decode(bencher: Bencher) { + let mut rng = rand::thread_rng(); + + bencher + .with_inputs(|| { + let mut buf = [0; VarLong::MAX_SIZE]; + VarLong(rng.gen()).encode(buf.as_mut_slice()).unwrap(); + buf + }) + .bench_local_values(|buf| { + let mut r = black_box(buf.as_slice()); + let _ = black_box(VarLong::decode(&mut r)); + }); }