Skip to content

Commit 54b3d44

Browse files
authored
Avoid memory copy in local store write (#2944)
* Avoid memory copy in local store write
1 parent 9e8b50a commit 54b3d44

File tree

2 files changed

+5
-2
lines changed

2 files changed

+5
-2
lines changed

changes/2944.misc.rst

+1
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
Avoid an unnecessary memory copy when writing Zarr to a local file

src/zarr/storage/_local.py

+4-2
Original file line numberDiff line numberDiff line change
@@ -51,15 +51,17 @@ def _put(
5151
if start is not None:
5252
with path.open("r+b") as f:
5353
f.seek(start)
54-
f.write(value.as_numpy_array().tobytes())
54+
# write takes any object supporting the buffer protocol
55+
f.write(value.as_numpy_array()) # type: ignore[arg-type]
5556
return None
5657
else:
57-
view = memoryview(value.as_numpy_array().tobytes())
58+
view = memoryview(value.as_numpy_array()) # type: ignore[arg-type]
5859
if exclusive:
5960
mode = "xb"
6061
else:
6162
mode = "wb"
6263
with path.open(mode=mode) as f:
64+
# write takes any object supporting the buffer protocol
6365
return f.write(view)
6466

6567

0 commit comments

Comments
 (0)