Skip to content

Commit

Permalink
Use tobytes in hex to make data C contiguous
Browse files Browse the repository at this point in the history
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
jakirkham committed Nov 24, 2018
1 parent 60b6e99 commit a3f7a45
Showing 1 changed file with 9 additions and 2 deletions.
11 changes: 9 additions & 2 deletions src/cybuffer.pyx
Original file line number Diff line number Diff line change
Expand Up @@ -269,11 +269,18 @@ cdef class cybuffer(object):


cpdef str hex(self):
cdef object d
cdef str s

if self.c_contiguous:
d = self
else:
d = self.tobytes()

if PY2K:
s = binascii.hexlify(self.tobytes())
s = binascii.hexlify(d)
else:
s = self.tobytes().hex()
s = memoryview(d).hex()

return s

Expand Down

0 comments on commit a3f7a45

Please sign in to comment.