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 more IntoPyObject impls #4446

Merged
merged 21 commits into from
Aug 17, 2024
Merged

Add more IntoPyObject impls #4446

merged 21 commits into from
Aug 17, 2024

Conversation

Icxolu
Copy link
Contributor

@Icxolu Icxolu commented Aug 16, 2024

This adds more IntoPyObject impls, mostly for reference types, plus a few that I missed in the first round. I generally tried to provide one where ever we also have a ToPyObject impl, but there could be still some that I missed somewhere. They will hopefully show up once I start migrating trait bounds.

@Icxolu Icxolu added the CI-skip-changelog Skip checking changelog entry label Aug 16, 2024
Copy link
Member

@davidhewitt davidhewitt left a comment

Choose a reason for hiding this comment

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

Overall looks great to me 👍

How should we go about testing these? Probably some level of coverage will be reached once we start migrating off the older traits, though it would be nice to drive that coverage closer to 100% if possible. Coverage got a bit slack with all the migrating off the GIL Refs and the huge pile of churn / deprecations. Maybe I should accept it'll be lower for the next couple of releases and then once we get to a steadier place again, we can work to drive it back up...

src/conversions/either.rs Show resolved Hide resolved
src/conversions/hashbrown.rs Show resolved Hide resolved
}
}

impl<'a, 'py, T: Copy + IntoPyObject<'py>> IntoPyObject<'py> for &'a Cell<T> {
Copy link
Member

Choose a reason for hiding this comment

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

This one is interesting; because of the nature of the cell as a copy-on-read container it means that we have T: IntoPyObject instead of &T, despite this being a reference to a container.

If we wanted to keep it consistent with the ToPyObject implementation above (and the general trend), this implementation should probably go through &T. But is that actually correct, or was the original ToPyObject implementation just wrong? 🤔

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Interesting thought... I'd argue that this implementation makes more sense, since we get an owned T out. There would be nothing to tie the lifetime of that reference to (since it is a temporary inside the impl) and we would have to write HRTB to express that. In the end it basically describes the same thing, with a lot more syntax.

I think in the ToPyObject is masks this HRTB kind of because the implementation can't define or restrict the "'self" lifetime and has to be valid for any lifetime.

Copy link
Member

Choose a reason for hiding this comment

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

Yes, I think I agree that this new implementation is the correct formulation 👍

src/conversions/std/string.rs Outdated Show resolved Hide resolved
@Icxolu
Copy link
Contributor Author

Icxolu commented Aug 16, 2024

How should we go about testing these? Probably some level of coverage will be reached once we start migrating off the older traits, though it would be nice to drive that coverage closer to 100% if possible.

I definitely agree here, we should strive to keep coverage high. Maybe we can try to implement the IntoPy<PyObject>/ToPyObject impls in terms of IntoPyObject impls, that would make sure that they're in sync and also cover them with the existing tests. What do you think of that.

@davidhewitt
Copy link
Member

davidhewitt commented Aug 16, 2024

Maybe we can try to implement the IntoPy/ToPyObject impls in terms of IntoPyObject impls, that would make sure that they're in sync and also cover them with the existing tests.

👍 I think that'd be a good way to reduce the amount of duplication, and then as we rotate out the old traits all that will be left behind is thin wrappers which won't be a huge concern if deprecated & not covered.

There might be a few cases where the old traits can't use these new ones (e.g. different bytes specialization behaviour maybe?), where I guess we just live with the duplication.

@Icxolu
Copy link
Contributor Author

Icxolu commented Aug 16, 2024

There might be a few cases where the old traits can't use these new ones (e.g. different bytes specialization behaviour maybe?), where I guess we just live with the duplication.

True, I guess all generic types can't do this, because of the bounds... Anyway doing it for "leaf" types will be better than not doing at all.

Copy link

codspeed-hq bot commented Aug 17, 2024

CodSpeed Performance Report

Merging #4446 will improve performances by 17.36%

Comparing Icxolu:intopyobject-ref (2ab5a01) with main (144b199)

Summary

⚡ 1 improvements
✅ 76 untouched benchmarks

Benchmarks breakdown

Benchmark main Icxolu:intopyobject-ref Change
bytes_new_small 394.4 ns 336.1 ns +17.36%

@Icxolu
Copy link
Contributor Author

Icxolu commented Aug 17, 2024

I looks like coverage has improved significantly from that. Now its pretty much just the generic collections (and for a lot of them just the reference) impls that are not covered. (I also found a few impls that I've missed previously, so I added those as well)

Copy link
Member

@davidhewitt davidhewitt left a comment

Choose a reason for hiding this comment

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

This looks fantastic to me, thanks! 👍

@Icxolu Icxolu added this pull request to the merge queue Aug 17, 2024
Merged via the queue into PyO3:main with commit f38be29 Aug 17, 2024
42 of 43 checks passed
@Icxolu Icxolu deleted the intopyobject-ref branch August 17, 2024 14:00
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.

2 participants