Skip to content

Commit 7bd8a2a

Browse files
committed
Take flattened array views to avoid some copies
Make use of Numcodecs' `ensure_contiguous_ndarray` to take `ndarray` views onto buffers to be stored in a few cases so as to reshape them and avoid a copy (thanks to the buffer protocol). This ensures that datetime/timedeltas are handled by default. Also catches things like object arrays. Finally this handles flattening the array if needed. All-in-all this gets as close to a `bytes` object as possible while not copying and doing its best to preserve type information while constructing something that fits the buffer protocol.
1 parent 9badf39 commit 7bd8a2a

File tree

1 file changed

+4
-8
lines changed

1 file changed

+4
-8
lines changed

zarr/storage.py

Lines changed: 4 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -31,16 +31,13 @@
3131
import warnings
3232

3333

34-
import numpy as np
35-
36-
3734
from zarr.util import (normalize_shape, normalize_chunks, normalize_order,
3835
normalize_storage_path, buffer_size,
3936
normalize_fill_value, nolock, normalize_dtype)
4037
from zarr.meta import encode_array_metadata, encode_group_metadata
4138
from zarr.compat import PY2, OrderedDict_move_to_end
4239
from numcodecs.registry import codec_registry
43-
from numcodecs.compat import ensure_bytes
40+
from numcodecs.compat import ensure_bytes, ensure_contiguous_ndarray
4441
from zarr.errors import (err_contains_group, err_contains_array, err_bad_compressor,
4542
err_fspath_exists_notdir, err_read_only, MetadataError)
4643

@@ -725,9 +722,8 @@ def __getitem__(self, key):
725722

726723
def __setitem__(self, key, value):
727724

728-
# handle F-contiguous numpy arrays
729-
if isinstance(value, np.ndarray) and value.flags.f_contiguous:
730-
value = ensure_bytes(value)
725+
# coerce to flat, contiguous array (ideally without copying)
726+
value = ensure_contiguous_ndarray(value)
731727

732728
# destination path for key
733729
file_path = os.path.join(self.path, key)
@@ -1176,7 +1172,7 @@ def __getitem__(self, key):
11761172
def __setitem__(self, key, value):
11771173
if self.mode == 'r':
11781174
err_read_only()
1179-
value = ensure_bytes(value)
1175+
value = ensure_contiguous_ndarray(value)
11801176
with self.mutex:
11811177
self.zf.writestr(key, value)
11821178

0 commit comments

Comments
 (0)