Skip to content

Does PyO3 know to skip PyType conversions when it can? #4915

Discussion options

You must be logged in to vote

If you call foo from baz, there will be no conversion needed. It remains a Rust function that takes a usize and returns a usize or PyErr. PyResult is simply a type alias for Result<T, PyErr>, so in the happy case no overhead is added. In the error case, constructing a PyErr is maybe a little more heavy than other error types, but in your example you need one anyway to return from baz.

Around your fn foo, PyO3 constructs a wrapper function that is exposed to the Python/C API, which takes PyObject and converts when called. But that is not used when you call the original function from Rust.

It's different if you take Python types as function parameters, e.g. fn foo(&self, bar: &PyString) or …

Replies: 1 comment 1 reply

Comment options

You must be logged in to vote
1 reply
@jamestwebber
Comment options

Answer selected by jamestwebber
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
2 participants