-
Notifications
You must be signed in to change notification settings - Fork 763
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #2068 from davidhewitt/optimize-frompyobject
macros: optimize generated code for #[derive(FromPyObject)]
- Loading branch information
Showing
9 changed files
with
165 additions
and
85 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,26 @@ | ||
use criterion::{criterion_group, criterion_main, Bencher, Criterion}; | ||
|
||
use pyo3::{prelude::*, types::PyString}; | ||
|
||
#[derive(FromPyObject)] | ||
enum ManyTypes { | ||
Int(i32), | ||
Bytes(Vec<u8>), | ||
String(String), | ||
} | ||
|
||
fn enum_from_pyobject(b: &mut Bencher) { | ||
Python::with_gil(|py| { | ||
let obj = PyString::new(py, "hello world"); | ||
b.iter(|| { | ||
let _: ManyTypes = obj.extract().unwrap(); | ||
}); | ||
}) | ||
} | ||
|
||
fn criterion_benchmark(c: &mut Criterion) { | ||
c.bench_function("enum_from_pyobject", enum_from_pyobject); | ||
} | ||
|
||
criterion_group!(benches, criterion_benchmark); | ||
criterion_main!(benches); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,64 +1,49 @@ | ||
#[cfg(feature = "macros")] | ||
use criterion::{criterion_group, criterion_main, Criterion}; | ||
use pyo3::{class::PyObjectProtocol, prelude::*, type_object::LazyStaticType}; | ||
|
||
#[cfg(feature = "macros")] | ||
mod m { | ||
use pyo3::{class::PyObjectProtocol, prelude::*, type_object::LazyStaticType}; | ||
/// This is a feature-rich class instance used to benchmark various parts of the pyclass lifecycle. | ||
#[pyclass] | ||
struct MyClass { | ||
#[pyo3(get, set)] | ||
elements: Vec<i32>, | ||
} | ||
|
||
/// This is a feature-rich class instance used to benchmark various parts of the pyclass lifecycle. | ||
#[pyclass] | ||
struct MyClass { | ||
#[pyo3(get, set)] | ||
elements: Vec<i32>, | ||
#[pymethods] | ||
impl MyClass { | ||
#[new] | ||
fn new(elements: Vec<i32>) -> Self { | ||
Self { elements } | ||
} | ||
|
||
#[pymethods] | ||
impl MyClass { | ||
#[new] | ||
fn new(elements: Vec<i32>) -> Self { | ||
Self { elements } | ||
} | ||
|
||
fn __call__(&mut self, new_element: i32) -> usize { | ||
self.elements.push(new_element); | ||
self.elements.len() | ||
} | ||
fn __call__(&mut self, new_element: i32) -> usize { | ||
self.elements.push(new_element); | ||
self.elements.len() | ||
} | ||
} | ||
|
||
#[pyproto] | ||
impl PyObjectProtocol for MyClass { | ||
/// A basic __str__ implementation. | ||
fn __str__(&self) -> &'static str { | ||
"MyClass" | ||
} | ||
#[pyproto] | ||
impl PyObjectProtocol for MyClass { | ||
/// A basic __str__ implementation. | ||
fn __str__(&self) -> &'static str { | ||
"MyClass" | ||
} | ||
} | ||
|
||
pub fn first_time_init(b: &mut criterion::Bencher) { | ||
let gil = Python::acquire_gil(); | ||
let py = gil.python(); | ||
b.iter(|| { | ||
// This is using an undocumented internal PyO3 API to measure pyclass performance; please | ||
// don't use this in your own code! | ||
let ty = LazyStaticType::new(); | ||
ty.get_or_init::<MyClass>(py); | ||
}); | ||
} | ||
pub fn first_time_init(b: &mut criterion::Bencher) { | ||
let gil = Python::acquire_gil(); | ||
let py = gil.python(); | ||
b.iter(|| { | ||
// This is using an undocumented internal PyO3 API to measure pyclass performance; please | ||
// don't use this in your own code! | ||
let ty = LazyStaticType::new(); | ||
ty.get_or_init::<MyClass>(py); | ||
}); | ||
} | ||
|
||
#[cfg(feature = "macros")] | ||
fn criterion_benchmark(c: &mut Criterion) { | ||
c.bench_function("first_time_init", m::first_time_init); | ||
c.bench_function("first_time_init", first_time_init); | ||
} | ||
|
||
#[cfg(feature = "macros")] | ||
criterion_group!(benches, criterion_benchmark); | ||
|
||
#[cfg(feature = "macros")] | ||
criterion_main!(benches); | ||
|
||
#[cfg(not(feature = "macros"))] | ||
fn main() { | ||
unimplemented!( | ||
"benchmarking `bench_pyclass` is only available with the `macros` feature enabled" | ||
); | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -6,3 +6,5 @@ | |
|
||
pub mod deprecations; | ||
pub mod freelist; | ||
#[doc(hidden)] | ||
pub mod frompyobject; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,26 @@ | ||
use crate::{exceptions::PyTypeError, PyErr, Python}; | ||
|
||
#[cold] | ||
pub fn failed_to_extract_enum( | ||
py: Python, | ||
type_name: &str, | ||
variant_names: &[&str], | ||
error_names: &[&str], | ||
errors: &[PyErr], | ||
) -> PyErr { | ||
let mut err_msg = format!( | ||
"failed to extract enum {} ('{}')", | ||
type_name, | ||
error_names.join(" | ") | ||
); | ||
for ((variant_name, error_name), error) in variant_names.iter().zip(error_names).zip(errors) { | ||
err_msg.push('\n'); | ||
err_msg.push_str(&format!( | ||
"- variant {variant_name} ({error_name}): {error_msg}", | ||
variant_name = variant_name, | ||
error_name = error_name, | ||
error_msg = error.value(py).str().unwrap().to_str().unwrap(), | ||
)); | ||
} | ||
PyTypeError::new_err(err_msg) | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters