Performance issue #3904
-
Hi everyone, Thanks for the amazing work on pyo3! I'm currently adding python binding to one of my library, and I'm trying to understand a huge drop in performance (100x) between two versions of my code. In short, I have a Rust struct For the python bindings, I created a use mylib::Model;
use anyhow::Result;
#[pyclass(name="Model")]
struct PyModel {
inner: Model,
}
#[pymethods]
impl PyModel {
#[staticmethod]
pub fn load_file(
file: &str,
) -> Result<PyModel> { // I'm using anyhow
let m = Model::load_file(
Path::new(file),
)?;
Ok(PyModel { inner: m })
}
pub fn infer(&mut self) -> Result<()> {
// Performance bottleneck, lots of loop + fairly high memory usage
self.inner.infer()?;
Ok(())
}
}
#[pyfunction]
pub fn test() -> Result<()> {
let mut model = Model::load_file(Path::new("myfile.txt"))?;
model.infer()?;
Ok(())
} When I compile ( import mylib
mylib.test() is 100x faster (400ms vs 40s) than this code: import mylib
m = mylib.Model.load_file("myfile.txt")
m.infer() Sorry for not providing a minimal example, it's a bit complicated with this type of performance issue. I've also partially simplified the code, Additional info:
That's it, I'd appreciate any ideas or potential tests I could do. Thanks a lot! EDIT: Adding flamegraphs |
Beta Was this translation helpful? Give feedback.
Replies: 4 comments 3 replies
-
I assume doing the |
Beta Was this translation helpful? Give feedback.
-
Are you able to attach a flamegraph or any other profile data ? There's nothing immediately obvious, but more information may spark ideas. |
Beta Was this translation helpful? Give feedback.
-
If you didn't say it also happens in a debug build, I'd have suggested that the compiler is optimizing away everything that |
Beta Was this translation helpful? Give feedback.
-
Given it's reportedly spending all the time in your inference loop, maybe print some basic stats about number of iterations, check the input data is the same, check the inference result is the same? |
Beta Was this translation helpful? Give feedback.
Given it's reportedly spending all the time in your inference loop, maybe print some basic stats about number of iterations, check the input data is the same, check the inference result is the same?