Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Use
tobytes
in hex
to make data C contiguous
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.
- Loading branch information