diff --git a/packages/zarr-indexing/changes/4603.misc.md b/packages/zarr-indexing/changes/4603.misc.md new file mode 100644 index 0000000000..3015c99f22 --- /dev/null +++ b/packages/zarr-indexing/changes/4603.misc.md @@ -0,0 +1 @@ +Writes whose indices land on a chunk in sorted order no longer sort them to drop repeated indices, so deduplication is linear in the number of points; writes without repeats also skip a gather copy. diff --git a/packages/zarr-indexing/src/zarr_indexing/writer.py b/packages/zarr-indexing/src/zarr_indexing/writer.py index f18146b71b..db69b6e691 100644 --- a/packages/zarr-indexing/src/zarr_indexing/writer.py +++ b/packages/zarr-indexing/src/zarr_indexing/writer.py @@ -167,8 +167,22 @@ def _domain_cells(shape: tuple[int, ...]) -> np.ndarray[Any, np.dtype[np.intp]]: return np.indices(shape, dtype=np.intp).reshape(len(shape), -1).T -def _last_occurrences(flat: np.ndarray[Any, Any]) -> np.ndarray[Any, np.dtype[np.intp]]: - """Positions to keep so that repeated flat indices take their last value.""" +def _last_occurrences( + flat: np.ndarray[Any, Any], +) -> slice | np.ndarray[Any, np.dtype[np.intp]]: + """Positions to keep so that repeated flat indices take their last value. + + A full slice means every position is kept, so callers index without a copy. + """ + if flat.size < 2: + return slice(None) + if (flat[1:] >= flat[:-1]).all(): + # Non-decreasing input, the usual C-order case: repeats are adjacent, + # so no sort is needed. + steps = flat[1:] != flat[:-1] + if steps.all(): + return slice(None) + return np.flatnonzero(np.append(steps, True)) _, first_in_reversed = np.unique(flat[::-1], return_index=True) return np.asarray(len(flat) - 1 - first_in_reversed, dtype=np.intp) diff --git a/packages/zarr-indexing/tests/test_writer.py b/packages/zarr-indexing/tests/test_writer.py index af9623b26e..e3405d824c 100644 --- a/packages/zarr-indexing/tests/test_writer.py +++ b/packages/zarr-indexing/tests/test_writer.py @@ -460,3 +460,31 @@ def read_into(self, source: Any, context: ReadContext, out: Any, /) -> None: view.write([7, 8, 9]) np.testing.assert_array_equal(source, [0, 7, 8, 9, 4, 5]) np.testing.assert_array_equal(view.result(), [1, 2, 3]) + + +@pytest.mark.parametrize( + "flat", + [ + [], + [3], + [0, 2, 5, 7], + [0, 0, 2, 5, 5, 5, 7], + [5, 0, 5, 2, 0, 7], + [7, 5, 5, 2, 0], + ], + ids=["empty", "single", "sorted", "sorted_repeats", "unsorted_repeats", "reversed"], +) +def test_last_occurrences_matches_sequential_assignment(flat: list[int]) -> None: + from zarr_indexing.writer import _last_occurrences + + indices = np.asarray(flat, dtype=np.intp) + values = np.arange(1, indices.size + 1) + expected = np.zeros(8, dtype=values.dtype) + for index, value in zip(indices, values, strict=True): + expected[index] = value + keep = _last_occurrences(indices) + kept = indices[keep] + assert np.unique(kept).size == kept.size + actual = np.zeros(8, dtype=values.dtype) + actual[kept] = values[keep] + np.testing.assert_array_equal(actual, expected)