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

add Borrowed::as_ptr #4520

Merged
merged 1 commit into from
Sep 6, 2024
Merged

add Borrowed::as_ptr #4520

merged 1 commit into from
Sep 6, 2024

Conversation

davidhewitt
Copy link
Member

Following the addition of the IntoPyObject trait, I noticed there are a few places in src/types/any.rs where we were creating a Borrowed<'_, '_, PyAny>, then temporarily deref-ing it to &Bound<'_, PyAny>, then immediately calling .as_ptr().

This PR adds Borrowed::as_ptr to avoid the need to go through the deref. I doubt this will actually change performance, but it'll at least make less work for the optimizer to chew on.

While working through this I found a few cases of unnecessary calls to .as_borrowed() (probably left over from GIL Ref migration work), which I removed at the same time.

Copy link
Contributor

@Icxolu Icxolu left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I noticed there are a few places in src/types/any.rs where we were creating a Borrowed<'_, '_, PyAny>, then temporarily deref-ing it to &Bound<'_, PyAny>, then immediately calling .as_ptr().

I wasn't sure how much we wanted to sprinkle Borrowed around in my initial conversion, that's why I opted for &Bound initially. Will take this into account in the other trait bound migrations.

/// The reference is borrowed; callers should not decrease the reference count
/// when they are finished with the pointer.
#[inline]
pub fn as_ptr(self) -> *mut ffi::PyObject {
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

A bit unrelated, but is there a particular reason why we didn't opt for NonNull<PyObject> for these pointer methods?

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I think it's a historical thing from before I was involved with PyO3. Given that mostly these pointers are just immediately passed to FFI, maybe it was more convenient? We could add non-null variants for cases where these pointers get handled by Rust I guess...

@davidhewitt davidhewitt added this pull request to the merge queue Sep 6, 2024
Merged via the queue into PyO3:main with commit e2a1da0 Sep 6, 2024
42 of 43 checks passed
@davidhewitt davidhewitt deleted the borrowed-as-ptr branch September 6, 2024 05:50
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