diff --git a/changes/2944.misc.rst b/changes/2944.misc.rst new file mode 100644 index 0000000000..48356a1fef --- /dev/null +++ b/changes/2944.misc.rst @@ -0,0 +1 @@ +Avoid an unnecessary memory copy when writing Zarr to a local file diff --git a/src/zarr/storage/_local.py b/src/zarr/storage/_local.py index b20a601bed..bd5bfc1da2 100644 --- a/src/zarr/storage/_local.py +++ b/src/zarr/storage/_local.py @@ -51,15 +51,17 @@ def _put( if start is not None: with path.open("r+b") as f: f.seek(start) - f.write(value.as_numpy_array().tobytes()) + # write takes any object supporting the buffer protocol + f.write(value.as_numpy_array()) # type: ignore[arg-type] return None else: - view = memoryview(value.as_numpy_array().tobytes()) + view = memoryview(value.as_numpy_array()) # type: ignore[arg-type] if exclusive: mode = "xb" else: mode = "wb" with path.open(mode=mode) as f: + # write takes any object supporting the buffer protocol return f.write(view)