-
Notifications
You must be signed in to change notification settings - Fork 784
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
Add conversion from Python int to either long or BigInt properly #1966
Comments
Thank you for the proposal. I would recommend instead solving this using #[derive(FromPyObject)]
enum IntTypes {
Small(i64),
Big(BigInt),
} As the above is straightfoward (although not super well documented, sorry), I don't think there's need to add a more specific function for this to PyO3. |
@davidhewitt Thanks for the information! I indeed found that the documentation for enums is quite lacky. This solution is quite neat! However I think using my code snippet above will reduce the overhead for conversion (without depending on Python throwing errors), although it might not be critical in most cases. |
I agree that your snippet will be more efficient, however I think a small enough optimisation that at this point I don't feel a strong need to add this to PyO3. If this proves to be a popular request, I'm happy to change my mind later. FYI - If you tweaked your |
Just a quick note, I found about 0.2 s improvement on my laptop with 10000 evaluations of a function taking |
After #2068 this performance gap should hopefully be a lot smaller 🤞 |
Cool! Thanks for the efforts! |
Sometimes it's convenient to have a conversion to proper type in rust, i.e. if the Python int is larger than 2^64, then convert to BigInt, if smaller, then convert to native i64. This could reduce some overhead if you want to be able to deal with big number and with small number efficiently at the same time.
The following code is what I figured out and it's working, but it might be better if it's integrated into the support for
num_bigint
The text was updated successfully, but these errors were encountered: