Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

improve deserialize benchmark #375

Merged
merged 1 commit into from
Feb 8, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@ readme = "README.md"
[lib]
name = "clvmr"
crate-type = ["rlib"]
bench = false

[features]
# when enabling the "counters" features, the CLVM interpreter is instrumented to
Expand Down
Binary file modified benches/block_af9c3d98.bin
Binary file not shown.
90 changes: 58 additions & 32 deletions benches/deserialize.rs
Original file line number Diff line number Diff line change
@@ -1,52 +1,78 @@
use clvmr::allocator::Allocator;
use clvmr::serde::node_from_bytes;
use clvmr::serde::node_from_bytes_backrefs;
use clvmr::serde::serialized_length_from_bytes;
use clvmr::serde::serialized_length_from_bytes_trusted;
use criterion::{criterion_group, criterion_main, Criterion, SamplingMode};
use clvmr::serde::{
node_from_bytes, node_from_bytes_backrefs, node_to_bytes_backrefs,
serialized_length_from_bytes, serialized_length_from_bytes_trusted, tree_hash_from_stream,
};
use criterion::{criterion_group, criterion_main, Criterion};
use std::include_bytes;
use std::time::Instant;

fn deserialize_benchmark(c: &mut Criterion) {
let block = include_bytes!("block_af9c3d98.bin");
let compressed_block = {
let mut a = Allocator::new();
let input = node_from_bytes(&mut a, block).expect("failed to parse input file");
node_to_bytes_backrefs(&a, input).expect("failed to compress generator")
};

let mut group = c.benchmark_group("deserialize");
group.sample_size(10);
group.sampling_mode(SamplingMode::Flat);

group.bench_function("serialized_length_from_bytes", |b| {
b.iter(|| {
let start = Instant::now();
let _ = serialized_length_from_bytes(block);
start.elapsed()
})
});
for (bl, name_suffix) in &[
(block as &[u8], ""),
(compressed_block.as_ref(), "-compressed"),
] {
group.bench_function(format!("serialized_length_from_bytes{name_suffix}"), |b| {
b.iter(|| {
let start = Instant::now();
serialized_length_from_bytes(bl).expect("serialized_length_from_bytes");
start.elapsed()
})
});

group.bench_function("serialized_length_from_bytes_trusted", |b| {
b.iter(|| {
let start = Instant::now();
let _ = serialized_length_from_bytes_trusted(block);
start.elapsed()
})
});
group.bench_function(
format!("serialized_length_from_bytes_trusted{name_suffix}"),
|b| {
b.iter(|| {
let start = Instant::now();
serialized_length_from_bytes_trusted(bl)
.expect("serialized_length_from_bytes_truested");
start.elapsed()
})
},
);

let mut a = Allocator::new();
let iter_checkpoint = a.checkpoint();
// we don't support compressed CLVM in tree_hash_from_stream yet
if name_suffix.is_empty() {
group.bench_function(format!("tree_hash_from_stream{name_suffix}"), |b| {
b.iter(|| {
let mut cur = std::io::Cursor::new(*bl);
let start = Instant::now();
tree_hash_from_stream(&mut cur).expect("tree_hash_from_stream");
start.elapsed()
})
});
}

group.bench_function("node_from_bytes_backrefs", |b| {
b.iter(|| {
a.restore_checkpoint(&iter_checkpoint);
let start = Instant::now();
let _ = node_from_bytes_backrefs(&mut a, block);
start.elapsed()
})
});
let mut a = Allocator::new();
let iter_checkpoint = a.checkpoint();

group.bench_function(format!("node_from_bytes_backrefs{name_suffix}"), |b| {
b.iter(|| {
a.restore_checkpoint(&iter_checkpoint);
let start = Instant::now();
node_from_bytes_backrefs(&mut a, bl).expect("node_from_bytes_backrefs");
start.elapsed()
})
});
}

let mut a = Allocator::new();
let iter_checkpoint = a.checkpoint();
group.bench_function("node_from_bytes", |b| {
b.iter(|| {
a.restore_checkpoint(&iter_checkpoint);
let start = Instant::now();
let _ = node_from_bytes(&mut a, block);
node_from_bytes(&mut a, block).expect("node_from_bytes");
start.elapsed()
})
});
Expand Down
Loading