From bcbc87dfceefe60d8161f38e39ab11fe2bddf85d Mon Sep 17 00:00:00 2001 From: Davis Vann Bennett Date: Fri, 6 Nov 2026 15:19:22 +0100 Subject: [PATCH 1/3] perf(zarr-indexing): skip the sort in _last_occurrences for sorted indices When the flat in-hull positions are non-decreasing (the usual C-order case), repeats are adjacent and the last occurrence of each can be found in O(n) without np.unique. When there are no repeats at all, return a full slice so callers index without a gather copy. Assisted-by: ClaudeCode:claude-opus-5-5 Co-Authored-By: Claude Opus 5.5 --- .../zarr-indexing/src/zarr_indexing/writer.py | 18 ++++++++++-- packages/zarr-indexing/tests/test_writer.py | 28 +++++++++++++++++++ 2 files changed, 44 insertions(+), 2 deletions(-) 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) From c471577b5352cd7cd944dfeefbb8674de7b2c614 Mon Sep 17 00:00:00 2001 From: Davis Vann Bennett Date: Fri, 6 Nov 2026 15:19:54 +0100 Subject: [PATCH 2/3] docs(zarr-indexing): add changelog fragment for #442 Assisted-by: ClaudeCode:claude-opus-5-5 Co-Authored-By: Claude Opus 5.5 --- packages/zarr-indexing/changes/442.misc.md | 1 + 1 file changed, 1 insertion(+) create mode 100644 packages/zarr-indexing/changes/442.misc.md diff --git a/packages/zarr-indexing/changes/442.misc.md b/packages/zarr-indexing/changes/442.misc.md new file mode 100644 index 0000000000..3015c99f22 --- /dev/null +++ b/packages/zarr-indexing/changes/442.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. From 8d0775bcdb94b8bc0c0f80c61b1155e10d3f95fb Mon Sep 17 00:00:00 2001 From: Davis Vann Bennett Date: Fri, 6 Nov 2026 15:30:56 +0100 Subject: [PATCH 3/3] docs(zarr-indexing): number the changelog fragment for #4603 Assisted-by: ClaudeCode:claude-opus-5-5 Co-Authored-By: Claude Opus 5.5 --- packages/zarr-indexing/changes/{442.misc.md => 4603.misc.md} | 0 1 file changed, 0 insertions(+), 0 deletions(-) rename packages/zarr-indexing/changes/{442.misc.md => 4603.misc.md} (100%) diff --git a/packages/zarr-indexing/changes/442.misc.md b/packages/zarr-indexing/changes/4603.misc.md similarity index 100% rename from packages/zarr-indexing/changes/442.misc.md rename to packages/zarr-indexing/changes/4603.misc.md