Skip to content

Commit 9b82146

Browse files
d-v-bclaude
andauthored
perf(zarr-indexing): skip the sort in _last_occurrences for sorted indices (zarr-developers#4603)
* 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 <noreply@anthropic.com> * docs(zarr-indexing): add changelog fragment for #442 Assisted-by: ClaudeCode:claude-opus-5-5 Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com> * docs(zarr-indexing): number the changelog fragment for zarr-developers#4603 Assisted-by: ClaudeCode:claude-opus-5-5 Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com> --------- Co-authored-by: Claude Opus 5.5 <noreply@anthropic.com>
1 parent 17c049c commit 9b82146

3 files changed

Lines changed: 45 additions & 2 deletions

File tree

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
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.

‎packages/zarr-indexing/src/zarr_indexing/writer.py‎

Lines changed: 16 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -167,8 +167,22 @@ def _domain_cells(shape: tuple[int, ...]) -> np.ndarray[Any, np.dtype[np.intp]]:
167167
return np.indices(shape, dtype=np.intp).reshape(len(shape), -1).T
168168

169169

170-
def _last_occurrences(flat: np.ndarray[Any, Any]) -> np.ndarray[Any, np.dtype[np.intp]]:
171-
"""Positions to keep so that repeated flat indices take their last value."""
170+
def _last_occurrences(
171+
flat: np.ndarray[Any, Any],
172+
) -> slice | np.ndarray[Any, np.dtype[np.intp]]:
173+
"""Positions to keep so that repeated flat indices take their last value.
174+
175+
A full slice means every position is kept, so callers index without a copy.
176+
"""
177+
if flat.size < 2:
178+
return slice(None)
179+
if (flat[1:] >= flat[:-1]).all():
180+
# Non-decreasing input, the usual C-order case: repeats are adjacent,
181+
# so no sort is needed.
182+
steps = flat[1:] != flat[:-1]
183+
if steps.all():
184+
return slice(None)
185+
return np.flatnonzero(np.append(steps, True))
172186
_, first_in_reversed = np.unique(flat[::-1], return_index=True)
173187
return np.asarray(len(flat) - 1 - first_in_reversed, dtype=np.intp)
174188

‎packages/zarr-indexing/tests/test_writer.py‎

Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -460,3 +460,31 @@ def read_into(self, source: Any, context: ReadContext, out: Any, /) -> None:
460460
view.write([7, 8, 9])
461461
np.testing.assert_array_equal(source, [0, 7, 8, 9, 4, 5])
462462
np.testing.assert_array_equal(view.result(), [1, 2, 3])
463+
464+
465+
@pytest.mark.parametrize(
466+
"flat",
467+
[
468+
[],
469+
[3],
470+
[0, 2, 5, 7],
471+
[0, 0, 2, 5, 5, 5, 7],
472+
[5, 0, 5, 2, 0, 7],
473+
[7, 5, 5, 2, 0],
474+
],
475+
ids=["empty", "single", "sorted", "sorted_repeats", "unsorted_repeats", "reversed"],
476+
)
477+
def test_last_occurrences_matches_sequential_assignment(flat: list[int]) -> None:
478+
from zarr_indexing.writer import _last_occurrences
479+
480+
indices = np.asarray(flat, dtype=np.intp)
481+
values = np.arange(1, indices.size + 1)
482+
expected = np.zeros(8, dtype=values.dtype)
483+
for index, value in zip(indices, values, strict=True):
484+
expected[index] = value
485+
keep = _last_occurrences(indices)
486+
kept = indices[keep]
487+
assert np.unique(kept).size == kept.size
488+
actual = np.zeros(8, dtype=values.dtype)
489+
actual[kept] = values[keep]
490+
np.testing.assert_array_equal(actual, expected)

0 commit comments

Comments
 (0)