Commit a35106c2 authored by Kirill Smelkov's avatar Kirill Smelkov

lib/mem: Allow memcpy & friends to work on arbitrary-length buffer

- not only multiple of 8. We can do it by using uint8 typed arrays, and
it does not hurt performance:

In [1]: from wendelin.lib.mem import bzero, memset, memcpy
In [2]: A = bytearray(2*1024*1024)
In [3]: B = bytearray(2*1024*1024)

        memcpy(B, A)    bzero(A)        memset(A, 0xff)

old:    718 µs          227 µs / 1116   228 µs / 1055 (*)
new:    718 µs          176 µs / 1080   175 µs / 1048

    (*) the second number comes from e.g.

        In [8]: timeit bzero(A)
        The slowest run took 4.63 times longer than the fastest.
        This could mean that an intermediate result is being cached
        10000 loops, best of 3: 228 µs per loop

        so the second number is more realistic and says performance
        stays aproximately the same and only slightly improves.
parent e5b7c31b
......@@ -16,22 +16,20 @@
# warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
#
# See COPYING file for full licensing terms.
from numpy import ndarray, uint64, copyto
from numpy import ndarray, uint8, copyto
# zero buffer memory
def bzero(buf):
assert len(buf)%8 == 0 # XXX for simplicity
a = ndarray(len(buf)//8, buffer=buf, dtype=uint64)
a = ndarray(len(buf), buffer=buf, dtype=uint8)
a[:] = 0
# set bytes in memory
def memset(buf, c):
assert 0 <= c <= 0xff
assert len(buf)%8 == 0 # XXX for simplicity
a = ndarray(len(buf)//8, buffer=buf, dtype=uint64)
a[:] = (c * 0x0101010101010101)
a = ndarray(len(buf), buffer=buf, dtype=uint8)
a[:] = c
# copy src buffer memory to dst
......@@ -39,7 +37,6 @@ def memset(buf, c):
def memcpy(dst, src):
l = len(src)
assert len(dst) == l
assert l % 8 == 0 # XXX for simplicity
adst = ndarray(l//8, buffer=dst, dtype=uint64)
asrc = ndarray(l//8, buffer=src, dtype=uint64)
adst = ndarray(l, buffer=dst, dtype=uint8)
asrc = ndarray(l, buffer=src, dtype=uint8)
copyto(adst, asrc)
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment