seung-lab / fpzip

Cython bindings for fpzip, a floating point image compression algorithm.
BSD 3-Clause "New" or "Revised" License
34 stars 5 forks source link

Implement Buffer Protocol to Save Memory Copy #9

Open william-silversmith opened 6 years ago

william-silversmith commented 6 years ago

I believe the following line is causing two memory copies:

bytes_out = bytearray(bufviewf[:outbytes])[:outbytes]

If we instead return a bytes-like object that implements the buffer protocol we can save a copy.

https://cython.readthedocs.io/en/latest/src/userguide/buffer.html

william-silversmith commented 6 years ago

This is difficult to do without making a copy, after all, we have to allocate a buffer equal in size to the original buffer anyway to write to. However, we might not need to make an additional copy on top of it. It might be possible to use realloc in order to shrink the existing buffer.