Skip to content

Commit 2dd1cdf

Browse files
committed
Use memcpy to speedup copies in fletcher32
1 parent b6d91ce commit 2dd1cdf

File tree

1 file changed

+9
-4
lines changed

1 file changed

+9
-4
lines changed

numcodecs/fletcher32.pyx

Lines changed: 9 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@
44

55

66
from libc.stdint cimport uint8_t, uint16_t, uint32_t
7+
from libc.string cimport memcpy
78

89
from cpython.bytes cimport PyBytes_FromStringAndSize
910

@@ -68,22 +69,24 @@ class Fletcher32(Codec):
6869
"""Return buffer plus 4-byte fletcher checksum"""
6970
buf = ensure_contiguous_ndarray(buf).ravel().view('uint8')
7071
cdef const uint8_t[::1] b_mv = buf
72+
cdef uint8_t* b_ptr = &b_mv[0]
7173
cdef Py_ssize_t b_len = len(b_mv)
7274

7375
cdef Py_ssize_t out_len = b_len + 4
7476
cdef bytes out = PyBytes_FromStringAndSize(NULL, out_len)
7577
cdef uint8_t* out_ptr = <uint8_t*>out
76-
cdef uint8_t[::1] out_mv = (<uint8_t[:(out_len + 1):1]>out_ptr)[:out_len]
7778

78-
out_mv[:-4] = b_mv
79-
store_le32(&out_mv[-4], _fletcher32(b_mv))
79+
memcpy(out_ptr, b_ptr, b_len)
80+
store_le32(out_ptr + b_len, _fletcher32(b_mv))
8081

8182
return out
8283

8384
def decode(self, buf, out=None):
8485
"""Check fletcher checksum, and return buffer without it"""
8586
b = ensure_contiguous_ndarray(buf).view('uint8')
8687
cdef const uint8_t[::1] b_mv = b
88+
cdef uint8_t* b_ptr = &b_mv[0]
89+
cdef Py_ssize_t b_len = len(b_mv)
8790

8891
val = _fletcher32(b_mv[:-4])
8992
found = load_le32(&b_mv[-4])
@@ -95,8 +98,10 @@ class Fletcher32(Codec):
9598
)
9699

97100
cdef uint8_t[::1] out_mv
101+
cdef uint8_t* out_ptr
98102
if out is not None:
99103
out_mv = ensure_contiguous_ndarray(out).view("uint8")
100-
out_mv[:] = b_mv[:-4]
104+
out_ptr = &out_mv[0]
105+
memcpy(out_ptr, b_ptr, b_len - 4)
101106
return out
102107
return memoryview(b[:-4])

0 commit comments

Comments
 (0)