Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

macros: optimize generated code for #[derive(FromPyObject)] #2068

merged 2 commits into from Dec 23, 2021


Copy link

Inspired by #1966, I decided to take a look at the generated code for #[derive(FromPyObject)] for enums.

There seemed to be some pretty easy wins as well as fixes to error messages, so I just went ahead and made them. Mostly what I did is delay all string formatting until creation of the final error message, to avoid intermediate costs when the conversion still ultimately succeeds. To keep generated code smaller I also extracted this into a #[cold] helper function that gets re-used for all enum derive implementations.

Added a benchmark for a simple enum case; it shows quite significant wins:

enum_from_pyobject      time:   [536.93 ns 541.67 ns 547.81 ns]
                        change: [-69.528% -69.164% -68.793%] (p = 0.00 < 0.05)
                        Performance has improved.

... there's probably more that can be done here to improve the other generated code from this macro too; this is enough for today.

@davidhewitt davidhewitt changed the title Optimize frompyobject macros: optimize generated code for #[derive(FromPyObject)] Dec 22, 2021
@davidhewitt davidhewitt merged commit 947055d into PyO3:main Dec 23, 2021
@davidhewitt davidhewitt deleted the optimize-frompyobject branch December 23, 2021 00:42
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
None yet
None yet

Successfully merging this pull request may close these issues.

None yet

1 participant