internal: Fix double copy creating PyBackedBytes from PyByteArray - #6357
Merged
Merged
Conversation
`Arc::<[u8]>::from(py_bytearray.to_vec())` creates a `Vec` copy of the `PyByteArray` contents, then immediately copies that `Vec` into an `Arc<[u8]>`. It should copy the `PyByteArray` contents directly into the `Arc<[u8]>`. This PR inlines `PyByteArrayMethods::to_vec` into `PyBackedBytes::from` without the unnecessary copy. See: <https://github.com/PyO3/pyo3/blob/75507833367e5267dd6fdd7e48de628e4b8e53e0/src/types/bytearray.rs#L291-L300>.
davidhewitt
approved these changes
Aug 28, 2026
davidhewitt
left a comment
Member
There was a problem hiding this comment.
Thanks, seems fine to me!
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
Arc::<[u8]>::from(py_bytearray.to_vec())creates aVeccopy of thePyByteArraycontents, then immediately copies thatVecinto anArc<[u8]>. It should copy thePyByteArraycontents directly into theArc<[u8]>. This PR inlinesPyByteArrayMethods::to_vecintoPyBackedBytes::fromwithout the unnecessary copy.pyo3/src/types/bytearray.rs
Lines 291 to 300 in 7550783