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

implement PyTypeMethods #3705

Merged
merged 12 commits into from
Feb 18, 2024
Merged

implement PyTypeMethods #3705

merged 12 commits into from
Feb 18, 2024

Conversation

davidhewitt
Copy link
Member

@davidhewitt davidhewitt commented Dec 26, 2023

Split from #3606

This PR implements the PyTypeMethods trait and the bound variants of the constructors for PyType.

The most interesting thing in this PR is the bound variant of PyType::from_type_ptr, which I decided to introduce as PyType::from_type_ptr_borrowed, because that pointer doesn't carry ownership.

There's possibly arguments that one (or both) of the PyType constructors should not have bound variants introduced and we should rework the APIs, e.g.:

  • Deprecate PyType::new in favour of T::type_object (or Python::get_type)
  • Find a way to make PyType::from_type_ptr_borrowed less like std::slice::from_raw_parts, e.g. should it force a reference count increase?

@davidhewitt davidhewitt added the CI-skip-changelog Skip checking changelog entry label Dec 26, 2023
@davidhewitt davidhewitt changed the title implement PyTypeMethods implement PyTypeMethods Dec 26, 2023
@davidhewitt
Copy link
Member Author

Let's make a decision about #3703 (comment) before reviewing this; if we agree to slow things down I'll remove the deprecations from here.

@davidhewitt
Copy link
Member Author

Okay, rebased. To make the deprecation in the macro output conditional I added a gil-refs feature to pyo3-macros. I think that's fine as we'll eventually want that feature to enable / disable creating a GILPool in #[pyfunction] calls.

Also, I'm really not feeling keen on from_type_ptr_borrowed so I might ponder better replacements for that. Suggestions welcome.

@adamreichold
Copy link
Member

Okay, rebased. To make the deprecation in the macro output conditional I added a gil-refs feature to pyo3-macros. I think that's fine as we'll eventually want that feature to enable / disable creating a GILPool in #[pyfunction] calls.

IIRC, all GILPool usages are already moved into the impl_ module. Nevertheless, I don't see a reason to avoid forwarding that feature into our macro helper crate.

Copy link

codspeed-hq bot commented Jan 5, 2024

CodSpeed Performance Report

Merging #3705 will degrade performances by 19.27%

Comparing davidhewitt:type2 (410133d) with main (0dd568d)

Summary

⚡ 2 improvements
❌ 3 regressions
✅ 74 untouched benchmarks

⚠️ Please fix the performance issues or acknowledge them on CodSpeed.

Benchmarks breakdown

Benchmark main davidhewitt:type2 Change
f64_from_pyobject 488.9 ns 405.6 ns +20.55%
list_via_downcast 185 ns 157.2 ns +17.67%
not_a_list_via_downcast 216.7 ns 244.4 ns -11.36%
sequence_from_list 300 ns 355.6 ns -15.63%
sequence_from_tuple 232.8 ns 288.3 ns -19.27%

@davidhewitt
Copy link
Member Author

Ok, this is finally reviewable! 🎉

@LilyFoote
Copy link
Contributor

I would like to review this, but it would be helpful if the conflicts were fixed first.

@davidhewitt
Copy link
Member Author

Ahh aha as soon as I'd got it ready the new changes to PyErr conflicted! I'll aim to update tonight 👍

@davidhewitt
Copy link
Member Author

Fixed!

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 added one remark, otherwise this looks good to me 🚀

src/types/typeobject.rs Outdated Show resolved Hide resolved
src/types/typeobject.rs Outdated Show resolved Hide resolved
src/types/typeobject.rs Outdated Show resolved Hide resolved
Comment on lines 60 to 62
/// This does not take ownership of the FFI pointer's "reference". The target type
/// object will have its reference count increased by 1, which will be released when
/// the `Bound` is dropped.
Copy link
Contributor

Choose a reason for hiding this comment

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

This does now take ownership, or did I misunderstand what you meant here?

Copy link
Member Author

@davidhewitt davidhewitt Feb 17, 2024

Choose a reason for hiding this comment

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

Uff, it's assuming that the reference is a borrowed pointer and creates a new reference. I agree my wording here was not the best.

Maybe it's better if we go a little bit inconsistent here and introduce this method with the new clearer name from_borrowed_type_ptr (forget the bound bit)?

That'd then match the from_borrowed methods which I think I'm about to introduce in a new PR...

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 pushed that as a new commit.

Copy link
Contributor

Choose a reason for hiding this comment

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

This reads clearer to me 👍

src/types/typeobject.rs Outdated Show resolved Hide resolved
davidhewitt and others added 2 commits February 17, 2024 23:16
Co-authored-by: Icxolu <10486322+Icxolu@users.noreply.github.com>
src/types/typeobject.rs Outdated Show resolved Hide resolved
Co-authored-by: Lily Foote <code@lilyf.org>
@davidhewitt
Copy link
Member Author

🎉 thanks @adamreichold @Icxolu @LilyFoote - this was a thorny one and it feels like quite the milestone to finally get this one rounded out!

Comment on lines 66 to 69
pub unsafe fn from_borrowed_type_ptr<'py>(
py: Python<'py>,
p: *mut ffi::PyTypeObject,
) -> Bound<'py, PyType> {
Copy link
Contributor

Choose a reason for hiding this comment

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

Suggested change
pub unsafe fn from_borrowed_type_ptr<'py>(
py: Python<'py>,
p: *mut ffi::PyTypeObject,
) -> Bound<'py, PyType> {
pub unsafe fn from_borrowed_type_ptr(
py: Python<'_>,
p: *mut ffi::PyTypeObject,
) -> Bound<'_, PyType> {

@davidhewitt davidhewitt added this pull request to the merge queue Feb 18, 2024
@github-merge-queue github-merge-queue bot removed this pull request from the merge queue due to a conflict with the base branch Feb 18, 2024
@davidhewitt davidhewitt added this pull request to the merge queue Feb 18, 2024
@github-merge-queue github-merge-queue bot removed this pull request from the merge queue due to failed status checks Feb 18, 2024
@alex alex added this pull request to the merge queue Feb 18, 2024
Merged via the queue into PyO3:main with commit f04ad56 Feb 18, 2024
37 of 39 checks passed
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
CI-skip-changelog Skip checking changelog entry
Projects
None yet
Development

Successfully merging this pull request may close these issues.

5 participants