From 74db438238a6da14a008958fa290c5762fc9244c Mon Sep 17 00:00:00 2001 From: Tom White Date: Wed, 26 Mar 2025 10:06:09 +0000 Subject: [PATCH 1/5] Avoid memory copy in local store write --- src/zarr/storage/_local.py | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/src/zarr/storage/_local.py b/src/zarr/storage/_local.py index b20a601bed..a561beef90 100644 --- a/src/zarr/storage/_local.py +++ b/src/zarr/storage/_local.py @@ -51,10 +51,10 @@ def _put( if start is not None: with path.open("r+b") as f: f.seek(start) - f.write(value.as_numpy_array().tobytes()) + f.write(value.as_numpy_array()) return None else: - view = memoryview(value.as_numpy_array().tobytes()) + view = memoryview(value.as_numpy_array()) if exclusive: mode = "xb" else: From 22592ec40f4c97324a4aca0fe6bee5a116ba3ca3 Mon Sep 17 00:00:00 2001 From: Tom White Date: Thu, 3 Apr 2025 09:35:55 +0100 Subject: [PATCH 2/5] Update src/zarr/storage/_local.py Co-authored-by: Tom Augspurger --- src/zarr/storage/_local.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/zarr/storage/_local.py b/src/zarr/storage/_local.py index a561beef90..5d359a886f 100644 --- a/src/zarr/storage/_local.py +++ b/src/zarr/storage/_local.py @@ -51,7 +51,7 @@ def _put( if start is not None: with path.open("r+b") as f: f.seek(start) - f.write(value.as_numpy_array()) + f.write(value.as_numpy_array()) # type: ignore[arg-type] return None else: view = memoryview(value.as_numpy_array()) From 6ad144ddb0b16d0f1b369f256181734bf77d26ea Mon Sep 17 00:00:00 2001 From: Tom White Date: Thu, 3 Apr 2025 09:36:05 +0100 Subject: [PATCH 3/5] Update src/zarr/storage/_local.py Co-authored-by: Tom Augspurger --- src/zarr/storage/_local.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/zarr/storage/_local.py b/src/zarr/storage/_local.py index 5d359a886f..5638daf417 100644 --- a/src/zarr/storage/_local.py +++ b/src/zarr/storage/_local.py @@ -54,7 +54,7 @@ def _put( f.write(value.as_numpy_array()) # type: ignore[arg-type] return None else: - view = memoryview(value.as_numpy_array()) + view = memoryview(value.as_numpy_array()) # type: ignore[arg-type] if exclusive: mode = "xb" else: From 8d633d25604e16c971e817145f5b7d715e256f97 Mon Sep 17 00:00:00 2001 From: Tom White Date: Thu, 3 Apr 2025 09:38:00 +0100 Subject: [PATCH 4/5] Add comment explaining file write method takes any object supporting the buffer protocol --- src/zarr/storage/_local.py | 2 ++ 1 file changed, 2 insertions(+) diff --git a/src/zarr/storage/_local.py b/src/zarr/storage/_local.py index 5638daf417..bd5bfc1da2 100644 --- a/src/zarr/storage/_local.py +++ b/src/zarr/storage/_local.py @@ -51,6 +51,7 @@ def _put( if start is not None: with path.open("r+b") as f: f.seek(start) + # write takes any object supporting the buffer protocol f.write(value.as_numpy_array()) # type: ignore[arg-type] return None else: @@ -60,6 +61,7 @@ def _put( else: mode = "wb" with path.open(mode=mode) as f: + # write takes any object supporting the buffer protocol return f.write(view) From 45b2b55d6397941792a42ab2d38cdab8e2fcbff9 Mon Sep 17 00:00:00 2001 From: Tom White Date: Thu, 3 Apr 2025 09:41:23 +0100 Subject: [PATCH 5/5] Add changelog entry --- changes/2944.misc.rst | 1 + 1 file changed, 1 insertion(+) create mode 100644 changes/2944.misc.rst 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