Skip to content

Commit 15d9260

Browse files
authored
Optimize is_total_slice for chunksize == 1 (#2782)
1 parent fc08f31 commit 15d9260

File tree

4 files changed

+23
-4
lines changed

4 files changed

+23
-4
lines changed

Diff for: changes/2782.bugfix.rst

+1
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
Optimize full chunk writes

Diff for: src/zarr/core/indexing.py

+7-4
Original file line numberDiff line numberDiff line change
@@ -1373,10 +1373,13 @@ def is_total_slice(item: Selection, shape: ChunkCoords) -> bool:
13731373
item = (item,)
13741374
if isinstance(item, tuple):
13751375
return all(
1376-
isinstance(dim_sel, slice)
1377-
and (
1378-
(dim_sel == slice(None))
1379-
or ((dim_sel.stop - dim_sel.start == dim_len) and (dim_sel.step in [1, None]))
1376+
(isinstance(dim_sel, int) and dim_len == 1)
1377+
or (
1378+
isinstance(dim_sel, slice)
1379+
and (
1380+
(dim_sel == slice(None))
1381+
or ((dim_sel.stop - dim_sel.start == dim_len) and (dim_sel.step in [1, None]))
1382+
)
13801383
)
13811384
for dim_sel, dim_len in zip(item, shape, strict=False)
13821385
)

Diff for: tests/test_array.py

+9
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@
55
import re
66
from itertools import accumulate
77
from typing import TYPE_CHECKING, Any, Literal
8+
from unittest import mock
89

910
import numcodecs
1011
import numpy as np
@@ -1328,3 +1329,11 @@ async def test_scalar_array() -> None:
13281329
assert arr[...] == 1.5
13291330
assert arr[()] == 1.5
13301331
assert arr.shape == ()
1332+
1333+
1334+
async def test_orthogonal_set_total_slice() -> None:
1335+
"""Ensure that a whole chunk overwrite does not read chunks"""
1336+
store = MemoryStore()
1337+
array = zarr.create_array(store, shape=(20, 20), chunks=(1, 2), dtype=int, fill_value=-1)
1338+
with mock.patch("zarr.storage.MemoryStore.get", side_effect=ValueError):
1339+
array[0, slice(4, 10)] = np.arange(6)

Diff for: tests/test_indexing.py

+6
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,7 @@
1919
OrthogonalSelection,
2020
Selection,
2121
_iter_grid,
22+
is_total_slice,
2223
make_slice_selection,
2324
normalize_integer_selection,
2425
oindex,
@@ -1953,3 +1954,8 @@ def test_vectorized_indexing_incompatible_shape(store) -> None:
19531954
)
19541955
with pytest.raises(ValueError, match="Attempting to set"):
19551956
arr[np.array([1, 2]), np.array([1, 2])] = np.array([[-1, -2], [-3, -4]])
1957+
1958+
1959+
def test_is_total_slice():
1960+
assert is_total_slice((0, slice(4, 6)), (1, 2))
1961+
assert is_total_slice((slice(0, 1, None), slice(4, 6)), (1, 2))

0 commit comments

Comments
 (0)