Skip to content

Commit

Permalink
add benchmark for bytes into pyobject
Browse files Browse the repository at this point in the history
  • Loading branch information
davidhewitt committed Aug 7, 2024
1 parent 16120ec commit e532e0d
Show file tree
Hide file tree
Showing 2 changed files with 97 additions and 0 deletions.
4 changes: 4 additions & 0 deletions pyo3-benches/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -47,6 +47,10 @@ harness = false
name = "bench_gil"
harness = false

[[bench]]
name = "bench_intopyobject"
harness = false

[[bench]]
name = "bench_list"
harness = false
Expand Down
93 changes: 93 additions & 0 deletions pyo3-benches/benches/bench_intopyobject.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,93 @@
use std::hint::black_box;

use codspeed_criterion_compat::{criterion_group, criterion_main, Bencher, Criterion};

use pyo3::conversion::IntoPyObject;
use pyo3::prelude::*;
use pyo3::types::PyBytes;

fn bench_bytes_new(b: &mut Bencher<'_>, data: &[u8]) {
Python::with_gil(|py| {
b.iter_with_large_drop(|| PyBytes::new(py, black_box(data)));
});
}

fn bytes_new_small(b: &mut Bencher<'_>) {
bench_bytes_new(b, &[]);
}

fn bytes_new_medium(b: &mut Bencher<'_>) {
let data = (0..u8::MAX).into_iter().collect::<Vec<u8>>();
bench_bytes_new(b, &data);
}

fn bytes_new_large(b: &mut Bencher<'_>) {
let data = vec![10u8; 100_000];
bench_bytes_new(b, &data);
}

fn bench_bytes_into_pyobject(b: &mut Bencher<'_>, data: &[u8]) {
Python::with_gil(|py| {
b.iter_with_large_drop(|| black_box(data).into_pyobject(py));
});
}

fn byte_slice_into_pyobject_small(b: &mut Bencher<'_>) {
bench_bytes_into_pyobject(b, &[]);
}

fn byte_slice_into_pyobject_medium(b: &mut Bencher<'_>) {
let data = (0..u8::MAX).into_iter().collect::<Vec<u8>>();
bench_bytes_into_pyobject(b, &data);
}

fn byte_slice_into_pyobject_large(b: &mut Bencher<'_>) {
let data = vec![10u8; 100_000];
bench_bytes_into_pyobject(b, &data);
}

fn byte_slice_into_py(b: &mut Bencher<'_>) {
Python::with_gil(|py| {
let data = (0..u8::MAX).into_iter().collect::<Vec<u8>>();
let bytes = data.as_slice();
b.iter_with_large_drop(|| black_box(bytes).into_py(py));
});
}

fn vec_into_pyobject(b: &mut Bencher<'_>) {
Python::with_gil(|py| {
let bytes = (0..u8::MAX).into_iter().collect::<Vec<u8>>();
b.iter_with_large_drop(|| black_box(&bytes).clone().into_pyobject(py));
});
}

fn vec_into_py(b: &mut Bencher<'_>) {
Python::with_gil(|py| {
let bytes = (0..u8::MAX).into_iter().collect::<Vec<u8>>();
b.iter_with_large_drop(|| black_box(&bytes).clone().into_py(py));
});
}

fn criterion_benchmark(c: &mut Criterion) {
c.bench_function("bytes_new_small", bytes_new_small);
c.bench_function("bytes_new_medium", bytes_new_medium);
c.bench_function("bytes_new_large", bytes_new_large);
c.bench_function(
"byte_slice_into_pyobject_small",
byte_slice_into_pyobject_small,
);
c.bench_function(
"byte_slice_into_pyobject_medium",
byte_slice_into_pyobject_medium,
);
c.bench_function(
"byte_slice_into_pyobject_large",
byte_slice_into_pyobject_large,
);
c.bench_function("byte_slice_into_py", byte_slice_into_py);
c.bench_function("vec_into_pyobject", vec_into_pyobject);
c.bench_function("vec_into_py", vec_into_py);
}

criterion_group!(benches, criterion_benchmark);
criterion_main!(benches);

0 comments on commit e532e0d

Please sign in to comment.