Skip to content

Extra memory copies in blosc, lz4, and zstd compress functions #717

Closed

Description

@tomwhite

To reproduce, put the following in repro.py:

import numpy as np
from numcodecs import blosc
from numcodecs.blosc import Blosc

if __name__ == "__main__":
    arr = np.random.random(25_000_000)  # 200MB
    blosc.compress(arr, b'lz4', 5, Blosc.SHUFFLE)

Then run the following (with numcodecs 0.15.1 and memray 1.16.0 installed):

memray run -f -o output.bin repro.py
memray flamegraph -f output.bin

The blosc.compress() function allocates 400MB, rather than the expected 200MB (this is in addition to the original 200MB arr). The reason is that on this line, where the destination buffer is resized by slicing, another memory allocation occurs, since slicing a bytes object creates a copy.

Image

lz4 and zstd suffer from the same problem.

#656 is possibly related, but doesn't fix the issue.

Activity

  1. added a commit that references this issue on Mar 25, 2025
  2. d-v-b commented on Mar 25, 2025

    @d-v-b
    Contributor

    thanks for finding this. do you have any ideas for how we could avoid the extra allocation when slicing?

  3. tomwhite commented on Mar 25, 2025

    @tomwhite
    MemberAuthor

    do you have any ideas for how we could avoid the extra allocation when slicing?

    Possibly by using a memoryview for dest?

  4. d-v-b commented on Mar 26, 2025

    @d-v-b
    Contributor

    #656 is possibly related, but doesn't fix the issue.

    @tomwhite i believe #656 implements the switch to memoryviews, do you have any idea why it doesn't fix this issue? That might be valuable feedback for @jakirkham.

  5. jakirkham commented on Mar 27, 2025

    @jakirkham
    Member

    It's actually a different issue. However have gone ahead and added a fix in the same PR

    For more context slicing bytes creates a copy as shown below

    b1 = b"abcdef"
    b2 = b1[:3]

    There were places in the codecs where they truncated the result. However that copied it into a new buffer

    To fix that we really don't even want to take a memoryview as it holds onto the old memory allocation, which is larger than we need. Further there are some code paths in Zarr & Numcodecs that can be sensitive to whether they get bytes or something else

    So added logic to resize existing bytes objects instead of copying. This is effectively realloc, which can return the excess memory to the underlying memory pool

    Noted how this works in this review: #656 (review)

  6. tomwhite commented on Mar 27, 2025

    @tomwhite
    MemberAuthor

    @jakirkham thanks for the explanation and for the fix in #656! As I mentioned there I can see the memory saving with your code.

  7. tomwhite commented on Mar 31, 2025

    @tomwhite
    MemberAuthor

    Fixed in #656

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions