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

optimize float -> f64 conversions on non-abi3 #3345

Merged
merged 1 commit into from
Jul 28, 2023

Conversation

davidhewitt
Copy link
Member

On non-abi3 builds we can use PyFloat_AS_DOUBLE to avoid an FFI function call and instead read the double value directly from the PyFloatObject structure.

This gives nearly a 2x speedup to f64 extraction when the input type is float according to the benchmark I added:

main

f64_from_pyobject       time:   [1.9698 ns 1.9870 ns 2.0047 ns]

branch

f64_from_pyobject       time:   [1.0878 ns 1.0977 ns 1.1081 ns]
                        change: [-45.172% -44.325% -43.403%] (p = 0.00 < 0.05)

Given I would expect this is the majority case of f64 conversions I think the slight bump in complexity is worth it given the drop in overhead.

While I was at it I moved the ffi definitions around to match their positioning in 3.12's headers.

src/types/floatob.rs Outdated Show resolved Hide resolved
src/types/floatob.rs Outdated Show resolved Hide resolved
This was referenced Jul 28, 2023
@davidhewitt davidhewitt force-pushed the pyfloat-value branch 3 times, most recently from 57e8251 to 2decb65 Compare July 28, 2023 19:42
@adamreichold adamreichold added this pull request to the merge queue Jul 28, 2023
Merged via the queue into PyO3:main with commit eb88596 Jul 28, 2023
31 checks passed
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

2 participants