Skip to content

Instantly share code, notes, and snippets.

@ihciah
Created August 14, 2024 12:22
Show Gist options
  • Select an option

  • Save ihciah/dcfaf13393bfada1ff8f372b4fa16e3f to your computer and use it in GitHub Desktop.

Select an option

Save ihciah/dcfaf13393bfada1ff8f372b4fa16e3f to your computer and use it in GitHub Desktop.
Benchmark for cloud-hypervisor/pull/6636

This gist is for benchmark cloud-hypervisor/pull/6636.

How to Reproduce

  1. Setup project following quick start of https://github.com/bheisler/criterion.rs
  2. Copy 2 files of this gist to projects
  3. Run cargo bench

Result

smallvec-small time: [7.3033 ns 7.3213 ns 7.3414 ns] Found 1 outliers among 100 measurements (1.00%) 1 (1.00%) high mild

borrowed-small time: [3.5628 ns 3.5832 ns 3.6043 ns]

borrowed-small-pre-alloc time: [3.5657 ns 3.5848 ns 3.6037 ns] Found 1 outliers among 100 measurements (1.00%) 1 (1.00%) high mild

smallvec-mid time: [20.415 ns 20.467 ns 20.526 ns] Found 2 outliers among 100 measurements (2.00%) 2 (2.00%) high mild

borrowed-mid time: [8.3222 ns 8.3512 ns 8.3804 ns] Found 3 outliers among 100 measurements (3.00%) 3 (3.00%) high mild

smallvec-big time: [62.989 ns 63.117 ns 63.247 ns] Found 1 outliers among 100 measurements (1.00%) 1 (1.00%) high mild

borrowed-big time: [9.9654 ns 10.009 ns 10.053 ns] Found 1 outliers among 100 measurements (1.00%) 1 (1.00%) high mild

Conclusion

  1. For small data(size=3): borrowed vec only cost 48% CPU time of smallvec(size=4), and borrowed vec with pre-alloc's performance is near the result without it.
  2. For mid data(size=6): borrowed vec only cost 41% CPU time of smallvec(size=4)
  3. For big data(size=10): borrowed vec only cost 16% CPU time of smallvec(size=4)

Environment

Linux arch-ch 6.8.12-1-pve #1 SMP PREEMPT_DYNAMIC PMX 6.8.12-1 (2024-08-05T16:17Z) x86_64 GNU/Linux

AMD Ryzen 7 7840HS w/ Radeon 780M Graphics

# File: Cargo.toml
[package]
name = "plarground"
version = "0.1.0"
edition = "2021"
[dependencies]
libc = "*"
smallvec = "*"
[dev-dependencies]
criterion = { version = "0.5", features = ["html_reports"] }
[[bench]]
name = "my_benchmark"
harness = false
// File: benches/my_benchmark.rs
use criterion::{criterion_group, criterion_main, Bencher, Criterion};
use libc::iovec;
use smallvec::SmallVec;
use std::hint::black_box;
const INPUTS: [*mut libc::c_void; 3] = [
0 as *mut libc::c_void,
1 as *mut libc::c_void,
2 as *mut libc::c_void,
];
const INPUTS_MID: [*mut libc::c_void; 6] = [
0 as *mut libc::c_void,
1 as *mut libc::c_void,
2 as *mut libc::c_void,
0 as *mut libc::c_void,
1 as *mut libc::c_void,
2 as *mut libc::c_void,
];
const INPUTS_BIG: [*mut libc::c_void; 10] = [
0 as *mut libc::c_void,
1 as *mut libc::c_void,
2 as *mut libc::c_void,
0 as *mut libc::c_void,
1 as *mut libc::c_void,
2 as *mut libc::c_void,
0 as *mut libc::c_void,
1 as *mut libc::c_void,
2 as *mut libc::c_void,
0 as *mut libc::c_void,
];
fn created_vec(ptrs: &[*mut libc::c_void]) -> SmallVec<[iovec; 4]> {
let mut v = SmallVec::new();
for p in ptrs {
v.push(iovec {
iov_base: *p,
iov_len: 1,
});
}
v
}
fn borrowed_vec(borrowed: &mut Vec<iovec>, ptrs: &[*mut libc::c_void]) {
borrowed.clear();
for p in ptrs {
borrowed.push(iovec {
iov_base: *p,
iov_len: 1,
});
}
}
#[inline(never)]
fn simulate_writev(iovecs: &[iovec]) -> usize {
let mut acc = 0;
for i in iovecs {
acc += i.iov_len;
}
acc
}
fn bench_smallvec(b: &mut Bencher, input: &[*mut libc::c_void]) {
b.iter(|| {
let smallvec = created_vec(black_box(input));
simulate_writev(&smallvec)
});
}
fn bench_borrowed(b: &mut Bencher, input: &[*mut libc::c_void]) {
let mut v = Vec::new();
b.iter(|| {
borrowed_vec(&mut v, black_box(input));
simulate_writev(&v)
});
}
fn bench_borrowed_pre_alloc(b: &mut Bencher, input: &[*mut libc::c_void]) {
let mut v = Vec::with_capacity(4);
b.iter(|| {
borrowed_vec(&mut v, black_box(input));
simulate_writev(&v)
});
}
fn criterion_benchmark(c: &mut Criterion) {
c.bench_function("smallvec-small", |b| bench_smallvec(b, &INPUTS));
c.bench_function("borrowed-small", |b| bench_borrowed(b, &INPUTS));
c.bench_function("borrowed-small-pre-alloc", |b| {
bench_borrowed_pre_alloc(b, &INPUTS)
});
c.bench_function("smallvec-mid", |b| bench_smallvec(b, &INPUTS_MID));
c.bench_function("borrowed-mid", |b| bench_borrowed(b, &INPUTS_MID));
c.bench_function("smallvec-big", |b| bench_smallvec(b, &INPUTS_BIG));
c.bench_function("borrowed-big", |b| bench_borrowed(b, &INPUTS_BIG));
}
criterion_group!(benches, criterion_benchmark);
criterion_main!(benches);
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment