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 PyBytes::new_bound #3777

Merged
merged 1 commit into from
Feb 1, 2024
Merged

Conversation

davidhewitt
Copy link
Member

For #3684

This adds PyBytes::new_bound and PyBytes::new_bound_with, deprecating the old constructors and migrating internal code.

@davidhewitt davidhewitt added the CI-skip-changelog Skip checking changelog entry label Jan 29, 2024
Copy link

codspeed-hq bot commented Jan 29, 2024

CodSpeed Performance Report

Merging #3777 will degrade performances by 15.02%

Comparing davidhewitt:bytes-new-bound (4c94be5) with main (aa1a986)

Summary

⚡ 2 improvements
❌ 1 regressions
✅ 76 untouched benchmarks

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

Benchmarks breakdown

Benchmark main davidhewitt:bytes-new-bound Change
list_via_downcast 157.2 ns 185 ns -15.02%
not_a_list_via_downcast 242.8 ns 215 ns +12.92%
extract_int_downcast_fail 266.1 ns 238.3 ns +11.66%

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.

Left some small suggestions above.

Also, do we want implement to Index for Bound/&Bound as well, or do we want to require calling as_bytes() before indexing?

Otherwise LGTM

Comment on lines 45 to 46
fn return_memoryview(py: Python<'_>) -> PyResult<&PyMemoryView> {
let bytes: &PyAny = PyBytes::new(py, b"hello world").into();
let bytes: &PyAny = PyBytes::new_bound(py, b"hello world").into_gil_ref();
Copy link
Contributor

Choose a reason for hiding this comment

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

after #3786 this could return a Bound<PyMemoryView> now 👼

Copy link
Member Author

Choose a reason for hiding this comment

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

Ah, lovely! 🚀

@@ -80,10 +113,23 @@ impl PyBytes {
/// `std::slice::from_raw_parts`, this is
/// unsafe](https://doc.rust-lang.org/std/slice/fn.from_raw_parts.html#safety).
pub unsafe fn from_ptr(py: Python<'_>, ptr: *const u8, len: usize) -> &PyBytes {
Copy link
Contributor

Choose a reason for hiding this comment

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

Since this returns a gil-ref, should we deprecate it as well, in favor of bound_from_ptr()?

Copy link
Member Author

Choose a reason for hiding this comment

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

Yes, my mistake to miss this; that's the downside of the commits I've been cherry-picking from #3606 - they were mostly trying to get a complete compile with a good range of the types rather than a complete implementation 🙈

I'll add here 👍

@davidhewitt
Copy link
Member Author

Thanks for the review!

Also, do we want implement to Index for Bound/&Bound as well, or do we want to require calling as_bytes() before indexing?

Ah great spot - unfortunately Index for Bound isn't possible due to ownership requirements, there's some discussion at #3680 (comment), I'll add to the migration notes here.

@davidhewitt
Copy link
Member Author

Ah great spot - unfortunately Index for Bound isn't possible due to ownership requirements, there's some discussion at #3680 (comment), I'll add to the migration notes here.

Oh actually looking again here it is possible in this case, because we can still return &u8! Even better that you made me check 👍

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.

Oh actually looking again here it is possible in this case, because we can still return &u8! Even better that you made me check 👍

Great, I could not see the reason why this would not work here. Glad I'm not blind 😆

guide/src/migration.md Outdated Show resolved Hide resolved
@@ -142,6 +186,15 @@ impl<I: SliceIndex<[u8]>> Index<I> for PyBytes {
}
}

/// This is the same way [Vec] is indexed.
impl<I: SliceIndex<[u8]>> Index<I> for Bound<'_, PyBytes> {
Copy link
Contributor

Choose a reason for hiding this comment

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

we could additionally implement it on &Bound as well, so it also works an a borrowed bound

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 not needed due to the way bytes[i] gets desugared into something like *bytes.index(i).

I've pushed an additional part to test_bound_bytes_index which indexes on &Bound<PyBytes>, see if you agree with me that the one implementation is sufficient?

Copy link
Contributor

Choose a reason for hiding this comment

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

Perfect, that even better! 👍

@davidhewitt
Copy link
Member Author

Thanks both! 🙏

@davidhewitt davidhewitt added this pull request to the merge queue Feb 1, 2024
Merged via the queue into PyO3:main with commit 516c085 Feb 1, 2024
35 of 36 checks passed
@davidhewitt davidhewitt deleted the bytes-new-bound branch February 1, 2024 10:01
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.

None yet

3 participants