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

Use tobytes in hex to make data C contiguous #12

Merged
merged 2 commits into from
Nov 24, 2018

Conversation

jakirkham
Copy link
Owner

Only copy the data in hex using tobytes if it is not C contiguous. That way if the data is already C contiguous, a view onto the original data can be used instead saving time and memory. The copy is needed for non-C contiguous data to get a consistent representation in hex as memoryview and other objects do. As Python 2's binascii.hexlify supports the (new) buffer protocol, it won't introduce any additional copies as long as something implementing the (new) buffer protocol is provided. On Python 3, we leverage memoryview's hex method, which also avoids copying if the data is C contiguous. It provides a fast path to hex, which is equivalent to the bytes method hex. Thus in both cases, the hex conversion will avoid any additional copies of the data. So this should in the best case avoid a copy when constructing the hex result, but may still copy the data if it is not contiguous in the way we expect, which is better than before.

To cutdown on attribute inspection within `hex`, simply import `hexlify`
from `binascii` in the global scope. That way `hexlify` can be accessed
quickly and used easily from the `hex` function.
Only copy the data in `hex` using `tobytes` if it is not C contiguous.
That way if the data is already C contiguous, a view onto the original
data can be used instead saving time and memory. The copy is needed for
non-C contiguous data to get a consistent representation in hex as
`memoryview` and other objects do. As Python 2's `binascii.hexlify`
supports the (new) buffer protocol, it won't introduce any additional
copies as long as something implementing the (new) buffer protocol is
provided. On Python 3, we leverage `memoryview`'s `hex` method, which
also avoids copying if the data is C contiguous. It provides a fast path
to `hex`, which is equivalent to the `bytes` method `hex`. Thus in both
cases, the hex conversion will avoid any additional copies of the data.
So this should in the best case avoid a copy when constructing the hex
result, but may still copy the data if it is not contiguous in the way
we expect, which is better than before.
@jakirkham jakirkham merged commit 3a3d414 into master Nov 24, 2018
@jakirkham jakirkham deleted the hex_ensure_c_contiguous branch November 24, 2018 03:02
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.

None yet

1 participant