Skip to content

Commit

Permalink
Drop unnecessary usage of deprecated GILPool from array benchmark.
Browse files Browse the repository at this point in the history
  • Loading branch information
adamreichold committed Mar 31, 2024
1 parent d7e87b2 commit 0832b28
Show file tree
Hide file tree
Showing 2 changed files with 35 additions and 35 deletions.
68 changes: 34 additions & 34 deletions benches/array.rs
Original file line number Diff line number Diff line change
Expand Up @@ -6,15 +6,17 @@ use test::{black_box, Bencher};
use std::ops::Range;

use numpy::{PyArray1, PyArray2, PyArray3};
use pyo3::{types::PyAnyMethods, Python, ToPyObject};
use pyo3::{types::PyAnyMethods, Bound, Python, ToPyObject};

#[bench]
fn extract_success(bencher: &mut Bencher) {
Python::with_gil(|py| {
let any = PyArray2::<f64>::zeros_bound(py, (10, 10), false).into_any();

bencher.iter(|| {
black_box(&any).extract::<&PyArray2<f64>>().unwrap();
black_box(&any)
.extract::<Bound<'_, PyArray2<f64>>>()
.unwrap()
});
});
}
Expand All @@ -25,7 +27,9 @@ fn extract_failure(bencher: &mut Bencher) {
let any = PyArray2::<f64>::zeros_bound(py, (10, 10), false).into_any();

bencher.iter(|| {
black_box(&any).extract::<&PyArray2<f64>>().unwrap_err();
black_box(&any)
.extract::<Bound<'_, PyArray2<f64>>>()
.unwrap_err()
});
});
}
Expand All @@ -35,9 +39,7 @@ fn downcast_success(bencher: &mut Bencher) {
Python::with_gil(|py| {
let any = PyArray2::<f64>::zeros_bound(py, (10, 10), false).into_any();

bencher.iter(|| {
black_box(&any).downcast::<PyArray2<f64>>().unwrap();
});
bencher.iter(|| black_box(&any).downcast::<PyArray2<f64>>().unwrap());
});
}

Expand All @@ -46,9 +48,7 @@ fn downcast_failure(bencher: &mut Bencher) {
Python::with_gil(|py| {
let any = PyArray2::<f64>::zeros_bound(py, (10, 10), false).into_any();

bencher.iter(|| {
black_box(&any).downcast::<PyArray2<f64>>().unwrap_err();
});
bencher.iter(|| black_box(&any).downcast::<PyArray2<f64>>().unwrap_err());
});
}

Expand All @@ -63,10 +63,12 @@ impl Iterator for Iter {
}

fn from_iter(bencher: &mut Bencher, size: usize) {
iter_with_gil(bencher, |py| {
let iter = black_box(Iter(0..size));
Python::with_gil(|py| {
bencher.iter(|| {
let iter = black_box(Iter(0..size));

PyArray1::from_iter_bound(py, iter);
PyArray1::from_iter_bound(py, iter)
});
});
}

Expand All @@ -88,10 +90,12 @@ fn from_iter_large(bencher: &mut Bencher) {
fn from_slice(bencher: &mut Bencher, size: usize) {
let vec = (0..size).collect::<Vec<_>>();

iter_with_gil(bencher, |py| {
let slice = black_box(&vec);
Python::with_gil(|py| {
bencher.iter(|| {
let slice = black_box(&vec);

PyArray1::from_slice_bound(py, slice);
PyArray1::from_slice_bound(py, slice)
});
});
}

Expand All @@ -113,10 +117,12 @@ fn from_slice_large(bencher: &mut Bencher) {
fn from_object_slice(bencher: &mut Bencher, size: usize) {
let vec = Python::with_gil(|py| (0..size).map(|val| val.to_object(py)).collect::<Vec<_>>());

iter_with_gil(bencher, |py| {
let slice = black_box(&vec);
Python::with_gil(|py| {
bencher.iter(|| {
let slice = black_box(&vec);

PyArray1::from_slice_bound(py, slice);
PyArray1::from_slice_bound(py, slice)
});
});
}

Expand All @@ -138,10 +144,12 @@ fn from_object_slice_large(bencher: &mut Bencher) {
fn from_vec2(bencher: &mut Bencher, size: usize) {
let vec2 = vec![vec![0; size]; size];

iter_with_gil(bencher, |py| {
let vec2 = black_box(&vec2);
Python::with_gil(|py| {
bencher.iter(|| {
let vec2 = black_box(&vec2);

PyArray2::from_vec2_bound(py, vec2).unwrap();
PyArray2::from_vec2_bound(py, vec2).unwrap()
});
});
}

Expand All @@ -163,10 +171,12 @@ fn from_vec2_large(bencher: &mut Bencher) {
fn from_vec3(bencher: &mut Bencher, size: usize) {
let vec3 = vec![vec![vec![0; size]; size]; size];

iter_with_gil(bencher, |py| {
let vec3 = black_box(&vec3);
Python::with_gil(|py| {
bencher.iter(|| {
let vec3 = black_box(&vec3);

PyArray3::from_vec3_bound(py, vec3).unwrap();
PyArray3::from_vec3_bound(py, vec3).unwrap()
});
});
}

Expand All @@ -184,13 +194,3 @@ fn from_vec3_medium(bencher: &mut Bencher) {
fn from_vec3_large(bencher: &mut Bencher) {
from_vec3(bencher, 2_usize.pow(5));
}

fn iter_with_gil(bencher: &mut Bencher, mut f: impl FnMut(Python<'_>)) {
Python::with_gil(|py| {
bencher.iter(|| {
let pool = unsafe { py.new_pool() };

f(pool.python());
});
});
}
2 changes: 1 addition & 1 deletion src/sum_products.rs
Original file line number Diff line number Diff line change
Expand Up @@ -225,7 +225,7 @@ where
obj.extract()
}

/// Deprecated form of [`einsum_bound!`]
/// Deprecated form of [`einsum_bound!`][crate::einsum_bound!]
#[deprecated(
since = "0.21.0",
note = "will be replaced by `einsum_bound!` in the future"
Expand Down

0 comments on commit 0832b28

Please sign in to comment.