pyo3: Class creation is slow

My environment is rustc 1.40, python 3.8, Windows 10.

💥 Reproducing

Make a pyclass and benchmark it!

I realize this could be a quirk of my current setup, but I was doing some benchmarking, and realized that it can take up to 440ns to create a Python class object. This is fine in isolation, but when performance matters, and you are making a lot of these objects (or want to make them as fast as possible) it matters. I created a simple Cython test file to compare, and it was more than 2x faster in creating the class (~200ns).

Here is the sample Cython code:

# cython: language_level=3
cdef class Example:
    def __cinit__(self):
        self.a = 4
        self.b = "Test"
    cdef public long a
    cdef public str b

You can compile an extension from it with cythonize -b test.pyx.

The Rust code is the obvious:

use pyo3::prelude::*;

#[pyclass]
pub struct Example {
    #[pyo3(get, set)]
    pub a: i64,
    #[pyo3(get, set)]
    pub b: String,
}

#[pymethods]
impl Example {
    #[new]
    fn new(obj: &PyRawObject) {
        obj.init({
            Example {
                a: 4,
                b: String::from("Hello"),
            }
        });
    }
}

#[pymodule]
fn example(_py: Python, m: &PyModule) -> PyResult<()> {
    m.add_class::<Example>()?;

    Ok(())
}

I realize this is probably not the biggest priority, but it seems odd that class creation would be so much slower.

About this issue

  • Original URL
  • State: closed
  • Created 7 years ago
  • Comments: 17 (15 by maintainers)

Most upvoted comments

Just as an update to this, I updated the clscreate test to PyO3 0.14.5 - https://github.com/davidhewitt/clscreate

On my Core i7-10750H, with Python 3.9.5, now I get mean times of:

Cython 52ns PyO3 107ns Python 174ns

… so we’re now 2x Cython rather than 5x, and we’re notably faster than plain Python! 🎉

I’m going to close this issue; also we track a benchmark at https://github.com/PyO3/pyo3/blob/56ef7c9f6d98d66e5e8cee3a31695bec555c76db/examples/pyo3-benchmarks/tests/test_benchmarks.py#L95 (though we don’t yet compare against Cython).

@davidhewitt I ended up making a quick repo of my test for reproducibility. https://github.com/ethanhs/clscreate.

The mean times I got on my Ryzen 9 3900x on Windows with nightly 2020-05-12: Cython: 75.9655 ns Python: 247.2162 ns PyO3: 354.3962 ns

Edit: I realized I should probably also try a non-static class and other things which may get optimized in unrealistic ways by C, but not in Rust, but I don’t really have the time atm.

@ijl I think I had some discussions about this… but now only found this comment https://github.com/PyO3/pyo3/issues/271#issuecomment-438225851

Now I’m too drunk to do more detailed explanation…, but I’ll do it soon and I want to rethink about this problem. If we can remove ReleasePool, it’s really cool.

Sure! I just used cargo build --release (I’d have used target-cpu=native but I’m getting a heap corruption ICE if I do that). Then I write a simple benchmark using pytest-benchmark like the following:

import example
def test_class_creation_rust(benchmark):
    benchmark(example.Example)

This calls example.Example() which is the best benchmark I could think of atm.