From fc58439ab2e336f2fff083c62f5053b07b885ede Mon Sep 17 00:00:00 2001 From: Davis Vann Bennett Date: Mon, 16 Nov 2026 17:46:10 +0100 Subject: [PATCH 1/9] fix(indexing): keep the last value when setitem gets repeated indices The indexers group selected items by chunk with an unstable argsort, so repeated indices reached the in-chunk assignment in an arbitrary order and the stored value depended on the platform, the chunk layout and the number of selected items. Sorting stably keeps repeats in the order given, and the in-chunk NumPy assignment then stores the last value. Refs zarr-developers/zarr-python#2855 Assisted-by: ClaudeCode:claude-fable-5.2 --- src/zarr/core/indexing.py | 10 +++++---- tests/test_indexing.py | 47 ++++++++++++++++++++++++++++++--------- tests/test_properties.py | 24 ++++---------------- 3 files changed, 46 insertions(+), 35 deletions(-) diff --git a/src/zarr/core/indexing.py b/src/zarr/core/indexing.py index 8ea59eb960..3f564023e6 100644 --- a/src/zarr/core/indexing.py +++ b/src/zarr/core/indexing.py @@ -828,8 +828,9 @@ def __init__( dim_out_sel = np.arange(nitems - 1, -1, -1) dim_sel_chunk_sorted = dim_sel_chunk[::-1] else: - # sort indices to group by chunk - dim_out_sel = np.argsort(dim_sel_chunk) + # sort indices to group by chunk; a stable sort keeps repeated indices in + # the order given, so the last value assigned to an item is the one stored + dim_out_sel = np.argsort(dim_sel_chunk, kind="stable") dim_sel = np.take(dim_sel, dim_out_sel) dim_sel_chunk_sorted = dim_sel_chunk[dim_out_sel] @@ -1343,8 +1344,9 @@ def __init__( # group points by chunk if np.any(np.diff(chunks_raveled_indices) < 0): - # optimisation, only sort if needed - sel_sort = np.argsort(chunks_raveled_indices) + # optimisation, only sort if needed; a stable sort keeps repeated points in + # the order given, so the last value assigned to a point is the one stored + sel_sort = np.argsort(chunks_raveled_indices, kind="stable") selection_broadcast = tuple(dim_sel[sel_sort] for dim_sel in selection_broadcast) chunks_raveled_indices = chunks_raveled_indices[sel_sort] else: diff --git a/tests/test_indexing.py b/tests/test_indexing.py index 785f089513..a0931d00a8 100644 --- a/tests/test_indexing.py +++ b/tests/test_indexing.py @@ -507,17 +507,42 @@ def test_setitem_zarr_array_as_value() -> None: assert_array_equal(dst2[2:7], np.arange(2, 7)) -@pytest.mark.skip(reason="fails on ubuntu, windows; numpy=2.2; in CI") -def test_setitem_repeated_index(): - """oindex assignment with repeated indices writes the last value for each duplicated index position.""" - array = zarr.array(data=np.zeros((4,)), chunks=(1,)) - indexer = np.array([-1, -1, 0, 0]) - array.oindex[(indexer,)] = [0, 1, 2, 3] - np.testing.assert_array_equal(array[:], np.array([3, 0, 0, 1])) - - indexer = np.array([-1, 0, 0, -1]) - array.oindex[(indexer,)] = [0, 1, 2, 3] - np.testing.assert_array_equal(array[:], np.array([2, 0, 0, 3])) +@pytest.mark.parametrize("method", ["oindex", "vindex"]) +@pytest.mark.parametrize("layout", ["unit_chunks", "partial_chunks", "one_chunk", "sharded"]) +@pytest.mark.parametrize( + ("shape", "selection"), + [ + ((24,), ([-1, -1, 0, 0],)), + ((24,), ([-1, 0, 0, -1],)), + # long enough that an unstable sort by chunk reorders the repeats + ((24,), ([1] + [0] * 23,)), + ((6, 6), ([0, 5, 0, -1, 2] * 4, [1, 1, 4, 4, 2] * 4)), + ], + ids=["adjacent", "interleaved", "many_repeats", "two_dimensions"], +) +def test_setitem_repeated_index( + method: str, layout: str, shape: tuple[int, ...], selection: tuple[list[int], ...] +) -> None: + """Assignment with repeated indices stores the last value given for each repeated position, as NumPy does.""" + chunks = { + "unit_chunks": (1,) * len(shape), + "partial_chunks": (4,) * len(shape), + "one_chunk": shape, + "sharded": (2,) * len(shape), + }[layout] + shards = (6,) * len(shape) if layout == "sharded" else None + array = zarr.create_array(store={}, shape=shape, chunks=chunks, shards=shards, dtype="i8") + expected = np.zeros(shape, dtype="i8") + if method == "oindex": + np_selection = np.ix_(*selection) + value = np.arange(1, 1 + expected[np_selection].size).reshape(expected[np_selection].shape) + expected[np_selection] = value + array.oindex[selection] = value + else: + value = np.arange(1, 1 + len(selection[0])) + expected[selection] = value + array.vindex[selection] = value + assert_array_equal(array[:], expected) Index = list[int] | tuple[slice | int | list[int], ...] diff --git a/tests/test_properties.py b/tests/test_properties.py index b448a524aa..d5d385accf 100644 --- a/tests/test_properties.py +++ b/tests/test_properties.py @@ -17,7 +17,7 @@ import hypothesis.extra.numpy as npst import hypothesis.strategies as st -from hypothesis import assume, event, given, settings +from hypothesis import event, given, settings from tests.conftest import declared_chunk_data_sizes from zarr.abc.store import Store @@ -199,12 +199,8 @@ async def test_oindex(data: st.DataObject) -> None: actual = await async_zarray.oindex.getitem(zindexer) assert_array_equal(nparray[npindexer], actual) - # sync set - for idxr, size in zip(zindexer, nparray.shape, strict=True): - if isinstance(idxr, np.ndarray) and idxr.size != np.unique(idxr % size).size: - # behaviour of setitem with repeated indices is not guaranteed in practice - # Negative and positive spellings of the same index are duplicates too. - assume(False) + # sync set: an index that repeats a position (in either spelling) keeps the last value + # written to it, as in NumPy, so repeated indices are drawn like any other # The sharding codec sees a coordinate selection (the GH4284 path) when the # chunk selection has more than one array axis or drops an integer axis. n_array_axes = sum(isinstance(idxr, np.ndarray) for idxr in zindexer) @@ -248,19 +244,7 @@ async def test_vindex(data: st.DataObject) -> None: actual = await async_zarray.vindex.getitem(indexer) assert_array_equal(nparray[indexer], actual) - # sync set - # Reads preserve the supplied indices; normalize negative indices explicitly when - # detecting repeated points rather than relying on a read to mutate the indexer. - points = np.stack( - [ - (idxr % size).ravel() - for idxr, size in zip(np.broadcast_arrays(*indexer), nparray.shape, strict=True) - ], - axis=-1, - ) - if len(np.unique(points, axis=0)) != len(points): - # behaviour of setitem with repeated coordinates is not guaranteed in practice - assume(False) + # sync set: a repeated point keeps the last value written to it, as in NumPy new_data = data.draw(numpy_arrays(shapes=st.just(actual.shape), dtype=nparray.dtype)) nparray[indexer] = new_data zarray.vindex[indexer] = new_data From aa58a965e548a30c3eefbed2a2a96ef6b62e80ce Mon Sep 17 00:00:00 2001 From: Davis Vann Bennett Date: Mon, 16 Nov 2026 17:46:41 +0100 Subject: [PATCH 2/9] docs(changes): add changelog fragment for repeated-index writes Assisted-by: ClaudeCode:claude-fable-5.2 --- changes/453.bugfix.md | 1 + 1 file changed, 1 insertion(+) create mode 100644 changes/453.bugfix.md diff --git a/changes/453.bugfix.md b/changes/453.bugfix.md new file mode 100644 index 0000000000..1aec76d22a --- /dev/null +++ b/changes/453.bugfix.md @@ -0,0 +1 @@ +Assigning through `oindex`, `vindex` or `__setitem__` with an index array that repeats a position now stores the last value given for that position, as NumPy does. Previously the stored value depended on the platform, the chunk layout and the number of selected items. From 91b7c2c35e43a71d7ee7257c5e37e263a8ff77af Mon Sep 17 00:00:00 2001 From: Davis Vann Bennett Date: Mon, 16 Nov 2026 17:50:08 +0100 Subject: [PATCH 3/9] docs(changes): renumber the changelog fragment to the upstream PR Assisted-by: ClaudeCode:claude-fable-5.2 --- changes/{453.bugfix.md => 4643.bugfix.md} | 0 1 file changed, 0 insertions(+), 0 deletions(-) rename changes/{453.bugfix.md => 4643.bugfix.md} (100%) diff --git a/changes/453.bugfix.md b/changes/4643.bugfix.md similarity index 100% rename from changes/453.bugfix.md rename to changes/4643.bugfix.md From 1366d30978e6f3dd0ed398e65e10b7ffe518a161 Mon Sep 17 00:00:00 2001 From: Davis Vann Bennett Date: Wed, 18 Nov 2026 12:38:30 +0100 Subject: [PATCH 4/9] perf(indexing): radix-sort chunk indices when grouping selections by chunk The stable argsort that keeps repeated indices in order was 2-3x slower than NumPy's default sort for large unordered selections, on reads as well as writes. NumPy sorts integers of at most 16 bits with a stable radix sort, so chunk indices are narrowed to uint16 when there are at most 2**16 chunks. Building an indexer for 10M unordered points then takes about 165-200 ms, against 560-690 ms on main and 1.5-2.0 s with the plain stable sort. Assisted-by: ClaudeCode:claude-opus-5-5 Co-Authored-By: Claude Opus 5.5 --- src/zarr/core/indexing.py | 25 +++++++++++++++++++------ tests/test_indexing.py | 12 ++++++++++++ 2 files changed, 31 insertions(+), 6 deletions(-) diff --git a/src/zarr/core/indexing.py b/src/zarr/core/indexing.py index 3b9f972961..0b5c9dc387 100644 --- a/src/zarr/core/indexing.py +++ b/src/zarr/core/indexing.py @@ -762,6 +762,21 @@ def sorted_run_ends( return a[run_starts].astype(np.intp, copy=False), run_ends +def stable_argsort_chunk_indices( + chunk_indices: npt.NDArray[np.intp], nchunks: int +) -> npt.NDArray[np.intp]: + """Return the indices that stably sort `chunk_indices`, each in `range(nchunks)`. + + A stable sort keeps items that share a chunk in the order given, so repeated + indices are assigned in order and the last value given is the one stored. NumPy + sorts integers of at most 16 bits with a stable radix sort, which is several times + faster than its default sort, so the indices are narrowed when `nchunks` allows it. + """ + if nchunks <= 2**16: + chunk_indices = chunk_indices.astype(np.uint16) + return np.argsort(chunk_indices, kind="stable") + + @dataclass(frozen=True) class IntArrayDimIndexer: """Integer array selection against a single dimension.""" @@ -830,9 +845,8 @@ def __init__( dim_out_sel = np.arange(nitems - 1, -1, -1) dim_sel_chunk_sorted = dim_sel_chunk[::-1] else: - # sort indices to group by chunk; a stable sort keeps repeated indices in - # the order given, so the last value assigned to an item is the one stored - dim_out_sel = np.argsort(dim_sel_chunk, kind="stable") + # sort indices to group by chunk + dim_out_sel = stable_argsort_chunk_indices(dim_sel_chunk, nchunks) dim_sel = np.take(dim_sel, dim_out_sel) dim_sel_chunk_sorted = dim_sel_chunk[dim_out_sel] @@ -1346,9 +1360,8 @@ def __init__( # group points by chunk if np.any(np.diff(chunks_raveled_indices) < 0): - # optimisation, only sort if needed; a stable sort keeps repeated points in - # the order given, so the last value assigned to a point is the one stored - sel_sort = np.argsort(chunks_raveled_indices, kind="stable") + # optimisation, only sort if needed + sel_sort = stable_argsort_chunk_indices(chunks_raveled_indices, product(cdata_shape)) selection_broadcast = tuple(dim_sel[sel_sort] for dim_sel in selection_broadcast) chunks_raveled_indices = chunks_raveled_indices[sel_sort] else: diff --git a/tests/test_indexing.py b/tests/test_indexing.py index 53fe1be8a1..718f2883fb 100644 --- a/tests/test_indexing.py +++ b/tests/test_indexing.py @@ -37,6 +37,7 @@ oindex, oindex_set, replace_ellipsis, + stable_argsort_chunk_indices, ) from zarr.registry import get_ndbuffer_class from zarr.storage import MemoryStore, StorePath @@ -545,6 +546,17 @@ def test_setitem_repeated_index( assert_array_equal(array[:], expected) +@pytest.mark.parametrize("nchunks", [1, 2**8, 2**16, 2**16 + 1, 2**40]) +def test_stable_argsort_chunk_indices(nchunks: int) -> None: + """The sort matches NumPy's stable sort whether or not the chunk indices are narrowed.""" + rng = np.random.default_rng(0) + chunk_indices = np.concatenate( + [[0, nchunks - 1, nchunks - 1, 0], rng.integers(0, nchunks, 1000)] + ).astype(np.intp) + expected = np.argsort(chunk_indices, kind="stable") + assert_array_equal(stable_argsort_chunk_indices(chunk_indices, nchunks), expected) + + Index = list[int] | tuple[slice | int | list[int], ...] From 632784a0b52f101ff9263e917afa31b1da44e736 Mon Sep 17 00:00:00 2001 From: Davis Vann Bennett Date: Wed, 18 Nov 2026 14:17:48 +0100 Subject: [PATCH 5/9] fix(indexing): pick the last value for repeated indices explicitly per chunk The stable sort kept repeated indices in the order given, but the in-chunk assignment still left the stored value to the array library: NumPy does not document the order of repeated assignments and CuPy leaves it undefined. Following zarr-indexing, `_merge_chunk_array` now resolves a chunk selection that names an element more than once into one that names each element once, with the last value given for it in C order. Orthogonal selections are resolved one axis at a time; coordinate selections jointly, in linear time via `np.maximum.at` unless the chunk is much larger than the selection. Assisted-by: ClaudeCode:claude-opus-5-5 Co-Authored-By: Claude Opus 5.5 --- changes/4643.bugfix.md | 2 +- src/zarr/core/chunk_utils.py | 8 +- src/zarr/core/indexing.py | 186 ++++++++++++++++++++++++++++++++++- tests/test_indexing.py | 57 ++++++++++- 4 files changed, 246 insertions(+), 7 deletions(-) diff --git a/changes/4643.bugfix.md b/changes/4643.bugfix.md index 1aec76d22a..155367cabe 100644 --- a/changes/4643.bugfix.md +++ b/changes/4643.bugfix.md @@ -1 +1 @@ -Assigning through `oindex`, `vindex` or `__setitem__` with an index array that repeats a position now stores the last value given for that position, as NumPy does. Previously the stored value depended on the platform, the chunk layout and the number of selected items. +Assigning through `oindex`, `vindex` or `__setitem__` with an index array that repeats a position now stores the last value given for that position, as NumPy does. Previously the stored value depended on the platform, the chunk layout and the number of selected items. Zarr now picks the last value itself, so the result no longer depends on the order in which the array library applies repeated assignments. diff --git a/src/zarr/core/chunk_utils.py b/src/zarr/core/chunk_utils.py index b26d5478b2..c8fe937246 100644 --- a/src/zarr/core/chunk_utils.py +++ b/src/zarr/core/chunk_utils.py @@ -4,7 +4,7 @@ from typing import TYPE_CHECKING, Any, cast from zarr.abc.codec import GetResult, SupportsSyncCodec, _codec_supports_sync -from zarr.core.indexing import is_scalar +from zarr.core.indexing import is_scalar, last_assignments if TYPE_CHECKING: from collections.abc import Callable, Iterable @@ -158,6 +158,12 @@ def _merge_chunk_array( None if idx in drop_axes else slice(None) for idx in range(chunk_spec.ndim) ) chunk_value = chunk_value[item] + repeats = last_assignments(chunk_selection, chunk_spec.shape) + if repeats is not None: + # Write each element once, with the last value given for it, rather than + # leave the outcome to the array library's order of assignment. + chunk_array[repeats.selection] = repeats.pick(chunk_value.as_ndarray_like()) + return chunk_array chunk_array[chunk_selection] = chunk_value return chunk_array diff --git a/src/zarr/core/indexing.py b/src/zarr/core/indexing.py index 0b5c9dc387..cc7c8eddca 100644 --- a/src/zarr/core/indexing.py +++ b/src/zarr/core/indexing.py @@ -767,16 +767,194 @@ def stable_argsort_chunk_indices( ) -> npt.NDArray[np.intp]: """Return the indices that stably sort `chunk_indices`, each in `range(nchunks)`. - A stable sort keeps items that share a chunk in the order given, so repeated - indices are assigned in order and the last value given is the one stored. NumPy - sorts integers of at most 16 bits with a stable radix sort, which is several times - faster than its default sort, so the indices are narrowed when `nchunks` allows it. + A stable sort keeps items that share a chunk in the order given, so each chunk's + selection lists repeated indices in the order the user gave them, which is what + `last_assignments` relies on. NumPy sorts integers of at most 16 bits with a stable + radix sort, which is several times faster than its default sort, so the indices are + narrowed when `nchunks` allows it. """ if nchunks <= 2**16: chunk_indices = chunk_indices.astype(np.uint16) return np.argsort(chunk_indices, kind="stable") +class LastAssignments(NamedTuple): + """A selection that writes each element once, and how to pick the values it takes. + + `selection` names, once each, the elements an original selection wrote. Their + values are the original values broadcast to `selected_shape`, raveled when + `flatten` is set, then indexed along each axis by the matching entry of `keep`, + where `None` keeps the whole axis. + """ + + selection: tuple[Any, ...] + selected_shape: tuple[int, ...] + flatten: bool + keep: tuple[npt.NDArray[np.intp] | None, ...] + + def pick(self, values: Any) -> Any: + """Pick, from the values given for the original selection, those to write.""" + picked = np.broadcast_to(values, self.selected_shape) + if self.flatten: + picked = picked.reshape(-1) + for axis, keep in enumerate(self.keep): + if keep is not None: + picked = picked[(slice(None),) * axis + (keep,)] + return picked + + +def _last_occurrences(flat: npt.NDArray[Any], size: int) -> npt.NDArray[np.intp] | None: + """Positions to keep so that repeated indices, each in `range(size)`, take their last value. + + Returns `None` when no index repeats, so callers can use the selection as is. + """ + if flat.size < 2: + return None + if (flat[1:] >= flat[:-1]).all(): + # Non-decreasing input, the usual case: repeats are adjacent. + steps = flat[1:] != flat[:-1] + if steps.all(): + return None + return np.flatnonzero(np.append(steps, True)) + positions = np.arange(flat.size, dtype=np.intp) + if size <= 16 * flat.size: + # Few possible indices for the number given: record the last position of each + # in linear time. A maximum does not depend on the order `ufunc.at` applies + # repeated indices in. + last = np.full(size, -1, dtype=np.intp) + np.maximum.at(last, flat, positions) + keep = np.flatnonzero(last[flat] == positions) + else: + _, first_in_reversed = np.unique(flat[::-1], return_index=True) + keep = positions[-1] - first_in_reversed + if keep.size == flat.size: + return None + return keep + + +def _is_integer_array(selector: object) -> TypeGuard[npt.NDArray[np.integer[Any]]]: + return isinstance(selector, np.ndarray) and selector.dtype.kind in "iu" + + +def _orthogonal_last_assignments( + selection: tuple[Any, ...], shape: tuple[int, ...], mesh: bool +) -> LastAssignments | None: + """`last_assignments` for an orthogonal selection, one axis at a time. + + The selection is either one 1-D array among slices or, when `mesh` is set, an + `np.ix_` mesh. An element repeats only where some axis repeats an index, and the + last value for it in C order is the one at the last occurrence on each axis. + """ + new_selection = list(selection) + keep: list[npt.NDArray[np.intp] | None] = [] + selected_shape: list[int] = [] + for axis, (selector, length) in enumerate(zip(selection, shape, strict=True)): + if isinstance(selector, slice): + selected_shape.append(len(range(*selector.indices(length)))) + keep.append(None) + continue + selected_shape.append(selector.size) + kept = ( + _last_occurrences(selector.reshape(-1), length) if _is_integer_array(selector) else None + ) + keep.append(kept) + if kept is not None: + new_selection[axis] = np.take(selector, kept, axis=axis if mesh else 0) + if all(kept is None for kept in keep): + return None + return LastAssignments( + selection=tuple(new_selection), + selected_shape=tuple(selected_shape), + flatten=False, + keep=tuple(keep), + ) + + +def _selected_flat_indices( + selection: tuple[Any, ...], shape: tuple[int, ...] +) -> npt.NDArray[np.intp]: + """The flat index of each element `selection` names, in the layout of NumPy's result.""" + coords = [] + for axis, length in enumerate(shape): + axis_shape = tuple(length if d == axis else 1 for d in range(len(shape))) + # A broadcast view of each axis' coordinates, indexed by the selection, gives the + # coordinates the selection names without allocating an array of `shape`. + grid = np.broadcast_to(np.arange(length, dtype=np.intp).reshape(axis_shape), shape) + coords.append(grid[selection]) + return np.asarray(np.ravel_multi_index(tuple(coords), shape), dtype=np.intp) + + +def _selection_form( + selection: tuple[Any, ...], ndim: int +) -> Literal["array", "mesh", "coordinates", "other"] | None: + """Classify a selection by the forms zarr's indexers give chunk selections. + + `"array"` is one 1-D integer array among slices, `"mesh"` an `np.ix_` mesh of + integer arrays and `"coordinates"` one 1-D integer array per axis, all the same + length. `None` means the selection has no integer array, so cannot repeat an + element. + """ + arrays = [selector for selector in selection if isinstance(selector, np.ndarray)] + if not any(_is_integer_array(array) for array in arrays): + return None + if len(selection) != ndim: + return "other" + if len(arrays) == 1: + if arrays[0].ndim == 1 and all(isinstance(s, slice | np.ndarray) for s in selection): + return "array" + return "other" + if len(arrays) < ndim or not all(_is_integer_array(array) for array in arrays): + return "other" + if all( + array.ndim == ndim and all(array.shape[d] == 1 for d in range(ndim) if d != axis) + for axis, array in enumerate(arrays) + ): + return "mesh" + if all(array.ndim == 1 and array.size == arrays[0].size for array in arrays): + return "coordinates" + return "other" + + +def last_assignments(selection: SelectorTuple, shape: tuple[int, ...]) -> LastAssignments | None: + """Resolve the elements of an array of `shape` that `selection` assigns more than once. + + Assigning through a selection that repeats an element gives that element several + values. NumPy leaves the order of those assignments undocumented and CuPy leaves it + undefined, so the last value given in the selection's C order is chosen here, + explicitly. The choice depends only on the selection, not on the order chunks are + written in. Returns `None` when no element repeats. + """ + selectors: tuple[Any, ...] = selection if isinstance(selection, tuple) else (selection,) + form = _selection_form(selectors, len(shape)) + if form is None: + return None + if form == "array": + return _orthogonal_last_assignments(selectors, shape, mesh=False) + if form == "mesh": + return _orthogonal_last_assignments(selectors, shape, mesh=True) + if form == "coordinates": + flat = np.asarray(np.ravel_multi_index(selectors, shape), dtype=np.intp) + kept = _last_occurrences(flat, product(shape)) + if kept is None: + return None + return LastAssignments( + selection=tuple(array[kept] for array in selectors), + selected_shape=(flat.size,), + flatten=False, + keep=(kept,), + ) + flat = _selected_flat_indices(selectors, shape) + kept = _last_occurrences(flat.reshape(-1), product(shape)) + if kept is None: + return None + return LastAssignments( + selection=tuple(np.unravel_index(flat.reshape(-1)[kept], shape)), + selected_shape=flat.shape, + flatten=True, + keep=(kept,), + ) + + @dataclass(frozen=True) class IntArrayDimIndexer: """Integer array selection against a single dimension.""" diff --git a/tests/test_indexing.py b/tests/test_indexing.py index 718f2883fb..6c340eecd6 100644 --- a/tests/test_indexing.py +++ b/tests/test_indexing.py @@ -20,7 +20,7 @@ from zarr import Array from zarr.core.buffer import default_buffer_prototype from zarr.core.chunk_grids import ChunkGrid -from zarr.core.common import ceildiv_int +from zarr.core.common import ceildiv_int, product from zarr.core.indexing import ( BasicSelection, CoordinateIndexer, @@ -32,6 +32,7 @@ _ArrayIndexingOrder, _iter_grid, _iter_regions, + last_assignments, make_slice_selection, normalize_integer_selection, oindex, @@ -557,6 +558,60 @@ def test_stable_argsort_chunk_indices(nchunks: int) -> None: assert_array_equal(stable_argsort_chunk_indices(chunk_indices, nchunks), expected) +_rng = np.random.default_rng(0) + + +@pytest.mark.parametrize( + "selection", + [ + (np.array([1, 2, 1, 0, 2]), slice(None)), + (slice(1, 3), np.array([2, 2, 0])), + (slice(None), _rng.integers(0, 40, 100)), + (np.array([1, 2, 2]), np.array([3, 3, 3])), + (np.array([2, 1, 2]), np.array([3, 0, 3])), + (_rng.integers(0, 30, 1000), _rng.integers(0, 40, 1000)), + (np.array([1, 2, 1]), 2), + np.ix_(np.array([2, 0, 2]), np.array([1, 1, 3])), + (np.array([0, 1, 2]), slice(None)), + (slice(None), _rng.permutation(40)), + tuple(np.unravel_index(_rng.permutation(1200), (30, 40))), + np.ix_(np.array([2, 0]), np.array([1, 3])), + (slice(None), slice(1, 3)), + ], + ids=[ + "array_then_slice", + "slice_then_array", + "long_unsorted_array", + "sorted_coordinates", + "unsorted_coordinates", + "many_coordinates", + "array_and_integer", + "mesh", + "no_repeats_orthogonal", + "no_repeats_long_unsorted_array", + "no_repeats_many_coordinates", + "no_repeats_mesh", + "no_arrays", + ], +) +def test_last_assignments(selection: tuple[Any, ...]) -> None: + """Writing through the resolved selection matches assigning each value in C order.""" + shape = (30, 40) + targets = np.arange(product(shape)).reshape(shape)[selection] + values = np.arange(targets.size).reshape(targets.shape) + expected = np.full(product(shape), -1) + for target, value in zip(targets.ravel().tolist(), values.ravel().tolist(), strict=True): + expected[target] = value + result = last_assignments(selection, shape) + if np.unique(targets).size == targets.size: + assert result is None + return + assert result is not None + actual = np.full(shape, -1) + actual[result.selection] = result.pick(values) + assert_array_equal(actual.ravel(), expected) + + Index = list[int] | tuple[slice | int | list[int], ...] From fee0b33a37238e594b94ccd017ca4179082c90c3 Mon Sep 17 00:00:00 2001 From: Davis Vann Bennett Date: Wed, 18 Nov 2026 14:45:47 +0100 Subject: [PATCH 6/9] fix(indexing): resolve repeated indices by selection position, not chunk order Repeats are now resolved before any codec sees them: `last_writes` wraps the indexer in `_set_selection` and, for each chunk projection, keeps the entry with the highest position in `out_selection` for every element named more than once. This is the last value in the selection's C order, and it does not depend on the order items are listed within a chunk, so the sort that groups items by chunk no longer needs to be stable. Above 2**16 chunks the indexers return to NumPy's default sort, recovering main's indexer speed. The per-chunk merge no longer dedupes, and the sharding codec never receives a repeated index. Assisted-by: ClaudeCode:claude-opus-5-5 Co-Authored-By: Claude Opus 5.5 --- src/zarr/core/array.py | 6 +- src/zarr/core/chunk_utils.py | 8 +- src/zarr/core/indexing.py | 283 +++++++++++++++-------------------- tests/test_indexing.py | 121 +++++++++------ 4 files changed, 199 insertions(+), 219 deletions(-) diff --git a/src/zarr/core/array.py b/src/zarr/core/array.py index 3735a4da18..5da88aadf9 100644 --- a/src/zarr/core/array.py +++ b/src/zarr/core/array.py @@ -109,6 +109,7 @@ is_pure_fancy_indexing, is_pure_orthogonal_indexing, is_scalar, + last_writes, pop_fields, ) from zarr.core.metadata import ( @@ -5901,7 +5902,10 @@ async def _set_selection( out_selection, is_complete_chunk, ) - for chunk_coords, chunk_selection, out_selection, is_complete_chunk in indexer + # A scalar gives every element the same value, so repeats need no resolving. + for chunk_coords, chunk_selection, out_selection, is_complete_chunk in ( + indexer if value_buffer.shape == () else last_writes(indexer) + ) ], value_buffer, drop_axes=indexer.drop_axes, diff --git a/src/zarr/core/chunk_utils.py b/src/zarr/core/chunk_utils.py index c8fe937246..b26d5478b2 100644 --- a/src/zarr/core/chunk_utils.py +++ b/src/zarr/core/chunk_utils.py @@ -4,7 +4,7 @@ from typing import TYPE_CHECKING, Any, cast from zarr.abc.codec import GetResult, SupportsSyncCodec, _codec_supports_sync -from zarr.core.indexing import is_scalar, last_assignments +from zarr.core.indexing import is_scalar if TYPE_CHECKING: from collections.abc import Callable, Iterable @@ -158,12 +158,6 @@ def _merge_chunk_array( None if idx in drop_axes else slice(None) for idx in range(chunk_spec.ndim) ) chunk_value = chunk_value[item] - repeats = last_assignments(chunk_selection, chunk_spec.shape) - if repeats is not None: - # Write each element once, with the last value given for it, rather than - # leave the outcome to the array library's order of assignment. - chunk_array[repeats.selection] = repeats.pick(chunk_value.as_ndarray_like()) - return chunk_array chunk_array[chunk_selection] = chunk_value return chunk_array diff --git a/src/zarr/core/indexing.py b/src/zarr/core/indexing.py index cc7c8eddca..7a517c7377 100644 --- a/src/zarr/core/indexing.py +++ b/src/zarr/core/indexing.py @@ -762,197 +762,150 @@ def sorted_run_ends( return a[run_starts].astype(np.intp, copy=False), run_ends -def stable_argsort_chunk_indices( +def argsort_chunk_indices( chunk_indices: npt.NDArray[np.intp], nchunks: int ) -> npt.NDArray[np.intp]: - """Return the indices that stably sort `chunk_indices`, each in `range(nchunks)`. + """Return the indices that sort `chunk_indices`, each in `range(nchunks)`. - A stable sort keeps items that share a chunk in the order given, so each chunk's - selection lists repeated indices in the order the user gave them, which is what - `last_assignments` relies on. NumPy sorts integers of at most 16 bits with a stable - radix sort, which is several times faster than its default sort, so the indices are - narrowed when `nchunks` allows it. + The order of items that share a chunk is unspecified: writes resolve repeated + indices by their position in the selection (see `last_writes`), not by their order + within a chunk. NumPy sorts integers of at most 16 bits with a radix sort, which is + several times faster than its default sort, so the indices are narrowed when + `nchunks` allows it. """ if nchunks <= 2**16: - chunk_indices = chunk_indices.astype(np.uint16) - return np.argsort(chunk_indices, kind="stable") + return np.argsort(chunk_indices.astype(np.uint16), kind="stable") + return np.argsort(chunk_indices) -class LastAssignments(NamedTuple): - """A selection that writes each element once, and how to pick the values it takes. +def _last_by_rank( + targets: npt.NDArray[Any], ranks: npt.NDArray[Any] | None +) -> npt.NDArray[np.intp] | None: + """Positions of the entries to keep so that each target keeps its highest-ranked entry. - `selection` names, once each, the elements an original selection wrote. Their - values are the original values broadcast to `selected_shape`, raveled when - `flatten` is set, then indexed along each axis by the matching entry of `keep`, - where `None` keeps the whole axis. + `targets` holds non-negative integers that may repeat. `ranks` holds distinct + integers, one per entry; `None` means each entry's rank is its position. Returns + `None` when no target repeats. """ - - selection: tuple[Any, ...] - selected_shape: tuple[int, ...] - flatten: bool - keep: tuple[npt.NDArray[np.intp] | None, ...] - - def pick(self, values: Any) -> Any: - """Pick, from the values given for the original selection, those to write.""" - picked = np.broadcast_to(values, self.selected_shape) - if self.flatten: - picked = picked.reshape(-1) - for axis, keep in enumerate(self.keep): - if keep is not None: - picked = picked[(slice(None),) * axis + (keep,)] - return picked - - -def _last_occurrences(flat: npt.NDArray[Any], size: int) -> npt.NDArray[np.intp] | None: - """Positions to keep so that repeated indices, each in `range(size)`, take their last value. - - Returns `None` when no index repeats, so callers can use the selection as is. - """ - if flat.size < 2: + n = targets.size + if n < 2: return None - if (flat[1:] >= flat[:-1]).all(): - # Non-decreasing input, the usual case: repeats are adjacent. - steps = flat[1:] != flat[:-1] + if ranks is None and (targets[1:] >= targets[:-1]).all(): + # Non-decreasing targets ranked by position: repeats are adjacent, and the last + # of each run ranks highest. + steps = targets[1:] != targets[:-1] if steps.all(): return None return np.flatnonzero(np.append(steps, True)) - positions = np.arange(flat.size, dtype=np.intp) - if size <= 16 * flat.size: - # Few possible indices for the number given: record the last position of each - # in linear time. A maximum does not depend on the order `ufunc.at` applies - # repeated indices in. - last = np.full(size, -1, dtype=np.intp) - np.maximum.at(last, flat, positions) - keep = np.flatnonzero(last[flat] == positions) + if (targets[1:] > targets[:-1]).all(): + return None + if ranks is None: + ranks = np.arange(n, dtype=np.intp) + size = int(targets.max()) + 1 + if size <= 16 * n: + # Few possible targets for the number given: record the highest rank of each in + # linear time. A maximum does not depend on the order `ufunc.at` applies + # repeated targets in. + highest = np.full(size, np.iinfo(np.intp).min, dtype=np.intp) + np.maximum.at(highest, targets, ranks) + keep = np.flatnonzero(highest[targets] == ranks) else: - _, first_in_reversed = np.unique(flat[::-1], return_index=True) - keep = positions[-1] - first_in_reversed - if keep.size == flat.size: + order = np.lexsort((ranks, targets)) + ordered = targets[order] + keep = order[np.append(ordered[1:] != ordered[:-1], True)] + if keep.size == n: return None return keep -def _is_integer_array(selector: object) -> TypeGuard[npt.NDArray[np.integer[Any]]]: - return isinstance(selector, np.ndarray) and selector.dtype.kind in "iu" +def _as_positions(out_sel: slice | npt.NDArray[Any]) -> npt.NDArray[np.intp]: + """The output positions a 1-D output selection names, as an array. + + Indexers give output slices an explicit start and stop, and no step. + """ + if isinstance(out_sel, slice): + return np.arange(out_sel.start, out_sel.stop, dtype=np.intp) + return out_sel.reshape(-1) -def _orthogonal_last_assignments( - selection: tuple[Any, ...], shape: tuple[int, ...], mesh: bool -) -> LastAssignments | None: - """`last_assignments` for an orthogonal selection, one axis at a time. +def _orthogonal_last_writes( + projection: ChunkProjection, drop_axes: tuple[int, ...] +) -> ChunkProjection: + """`last_writes` for one projection of an `OrthogonalIndexer`. - The selection is either one 1-D array among slices or, when `mesh` is set, an - `np.ix_` mesh. An element repeats only where some axis repeats an index, and the - last value for it in C order is the one at the last occurrence on each axis. + An element repeats only where some axis repeats an index, and its last value in C + order is the one at the highest output position on each axis, so each axis is + resolved on its own. """ - new_selection = list(selection) - keep: list[npt.NDArray[np.intp] | None] = [] - selected_shape: list[int] = [] - for axis, (selector, length) in enumerate(zip(selection, shape, strict=True)): - if isinstance(selector, slice): - selected_shape.append(len(range(*selector.indices(length)))) - keep.append(None) + chunk_selection = cast("tuple[Any, ...]", projection.chunk_selection) + out_selection = cast("tuple[Any, ...]", projection.out_selection) + n_arrays = sum(isinstance(sel, np.ndarray) for sel in chunk_selection) + # The indexer meshes the selections with `ix_` exactly when this holds. + mesh = n_arrays > 1 or bool(drop_axes) + out_axes = [axis for axis in range(len(chunk_selection)) if axis not in drop_axes] + chunk_sels = list(chunk_selection) + out_sels = list(out_selection) + changed = False + for out_axis, axis in enumerate(out_axes): + chunk_sel = chunk_sels[axis] + if not (isinstance(chunk_sel, np.ndarray) and chunk_sel.dtype.kind in "iu"): continue - selected_shape.append(selector.size) - kept = ( - _last_occurrences(selector.reshape(-1), length) if _is_integer_array(selector) else None - ) - keep.append(kept) - if kept is not None: - new_selection[axis] = np.take(selector, kept, axis=axis if mesh else 0) - if all(kept is None for kept in keep): - return None - return LastAssignments( - selection=tuple(new_selection), - selected_shape=tuple(selected_shape), - flatten=False, - keep=tuple(keep), + out_sel = out_sels[out_axis] + ranks = None if isinstance(out_sel, slice) else out_sel.reshape(-1) + keep = _last_by_rank(chunk_sel.reshape(-1), ranks) + if keep is None: + continue + changed = True + chunk_sels[axis] = chunk_sel.reshape(-1)[keep] + out_sels[out_axis] = _as_positions(out_sel)[keep] + if not changed: + return projection + if mesh: + chunk_selection = np.ix_(*(sel.reshape(-1) for sel in chunk_sels)) + out_selection = np.ix_(*(_as_positions(sel) for sel in out_sels)) + else: + chunk_selection = tuple(chunk_sels) + out_selection = tuple(out_sels) + return projection._replace(chunk_selection=chunk_selection, out_selection=out_selection) + + +def _coordinate_last_writes(projection: ChunkProjection) -> ChunkProjection: + """`last_writes` for one projection of a `CoordinateIndexer`.""" + chunk_selection = cast("tuple[npt.NDArray[np.intp], ...]", projection.chunk_selection) + out_selection = cast("slice | npt.NDArray[np.intp]", projection.out_selection) + if chunk_selection[0].size < 2: + return projection + extent = tuple(int(sel.max()) + 1 for sel in chunk_selection) + targets = np.ravel_multi_index(chunk_selection, extent) + ranks = None if isinstance(out_selection, slice) else out_selection + keep = _last_by_rank(targets, ranks) + if keep is None: + return projection + return projection._replace( + chunk_selection=tuple(sel[keep] for sel in chunk_selection), + out_selection=_as_positions(out_selection)[keep], ) -def _selected_flat_indices( - selection: tuple[Any, ...], shape: tuple[int, ...] -) -> npt.NDArray[np.intp]: - """The flat index of each element `selection` names, in the layout of NumPy's result.""" - coords = [] - for axis, length in enumerate(shape): - axis_shape = tuple(length if d == axis else 1 for d in range(len(shape))) - # A broadcast view of each axis' coordinates, indexed by the selection, gives the - # coordinates the selection names without allocating an array of `shape`. - grid = np.broadcast_to(np.arange(length, dtype=np.intp).reshape(axis_shape), shape) - coords.append(grid[selection]) - return np.asarray(np.ravel_multi_index(tuple(coords), shape), dtype=np.intp) - - -def _selection_form( - selection: tuple[Any, ...], ndim: int -) -> Literal["array", "mesh", "coordinates", "other"] | None: - """Classify a selection by the forms zarr's indexers give chunk selections. - - `"array"` is one 1-D integer array among slices, `"mesh"` an `np.ix_` mesh of - integer arrays and `"coordinates"` one 1-D integer array per axis, all the same - length. `None` means the selection has no integer array, so cannot repeat an - element. - """ - arrays = [selector for selector in selection if isinstance(selector, np.ndarray)] - if not any(_is_integer_array(array) for array in arrays): - return None - if len(selection) != ndim: - return "other" - if len(arrays) == 1: - if arrays[0].ndim == 1 and all(isinstance(s, slice | np.ndarray) for s in selection): - return "array" - return "other" - if len(arrays) < ndim or not all(_is_integer_array(array) for array in arrays): - return "other" - if all( - array.ndim == ndim and all(array.shape[d] == 1 for d in range(ndim) if d != axis) - for axis, array in enumerate(arrays) - ): - return "mesh" - if all(array.ndim == 1 and array.size == arrays[0].size for array in arrays): - return "coordinates" - return "other" - - -def last_assignments(selection: SelectorTuple, shape: tuple[int, ...]) -> LastAssignments | None: - """Resolve the elements of an array of `shape` that `selection` assigns more than once. +def last_writes(indexer: Indexer) -> Iterator[ChunkProjection]: + """The projections of `indexer`, each writing every element it touches once. - Assigning through a selection that repeats an element gives that element several - values. NumPy leaves the order of those assignments undocumented and CuPy leaves it - undefined, so the last value given in the selection's C order is chosen here, - explicitly. The choice depends only on the selection, not on the order chunks are - written in. Returns `None` when no element repeats. + A selection that repeats an element gives it several values. The value kept is the + last one in the C order of the selection, the result of assigning the values in + turn. It is chosen from each item's position in the selection, so it does not + depend on how items are ordered within a chunk, on the order chunks are written + in, or on how an array library orders repeated assignments, which NumPy leaves + undocumented and CuPy undefined. """ - selectors: tuple[Any, ...] = selection if isinstance(selection, tuple) else (selection,) - form = _selection_form(selectors, len(shape)) - if form is None: - return None - if form == "array": - return _orthogonal_last_assignments(selectors, shape, mesh=False) - if form == "mesh": - return _orthogonal_last_assignments(selectors, shape, mesh=True) - if form == "coordinates": - flat = np.asarray(np.ravel_multi_index(selectors, shape), dtype=np.intp) - kept = _last_occurrences(flat, product(shape)) - if kept is None: - return None - return LastAssignments( - selection=tuple(array[kept] for array in selectors), - selected_shape=(flat.size,), - flatten=False, - keep=(kept,), - ) - flat = _selected_flat_indices(selectors, shape) - kept = _last_occurrences(flat.reshape(-1), product(shape)) - if kept is None: - return None - return LastAssignments( - selection=tuple(np.unravel_index(flat.reshape(-1)[kept], shape)), - selected_shape=flat.shape, - flatten=True, - keep=(kept,), - ) + if isinstance(indexer, CoordinateIndexer): + for projection in indexer: + yield _coordinate_last_writes(projection) + elif isinstance(indexer, OrthogonalIndexer): + for projection in indexer: + yield _orthogonal_last_writes(projection, indexer.drop_axes) + else: + # Basic and block selections name each element once. + yield from indexer @dataclass(frozen=True) @@ -1024,7 +977,7 @@ def __init__( dim_sel_chunk_sorted = dim_sel_chunk[::-1] else: # sort indices to group by chunk - dim_out_sel = stable_argsort_chunk_indices(dim_sel_chunk, nchunks) + dim_out_sel = argsort_chunk_indices(dim_sel_chunk, nchunks) dim_sel = np.take(dim_sel, dim_out_sel) dim_sel_chunk_sorted = dim_sel_chunk[dim_out_sel] @@ -1539,7 +1492,7 @@ def __init__( # group points by chunk if np.any(np.diff(chunks_raveled_indices) < 0): # optimisation, only sort if needed - sel_sort = stable_argsort_chunk_indices(chunks_raveled_indices, product(cdata_shape)) + sel_sort = argsort_chunk_indices(chunks_raveled_indices, product(cdata_shape)) selection_broadcast = tuple(dim_sel[sel_sort] for dim_sel in selection_broadcast) chunks_raveled_indices = chunks_raveled_indices[sel_sort] else: diff --git a/tests/test_indexing.py b/tests/test_indexing.py index 6c340eecd6..ed63274739 100644 --- a/tests/test_indexing.py +++ b/tests/test_indexing.py @@ -27,18 +27,19 @@ CoordinateSelection, IntArrayDimIndexer, MaskIndexer, + OrthogonalIndexer, OrthogonalSelection, Selection, _ArrayIndexingOrder, _iter_grid, _iter_regions, - last_assignments, + argsort_chunk_indices, + last_writes, make_slice_selection, normalize_integer_selection, oindex, oindex_set, replace_ellipsis, - stable_argsort_chunk_indices, ) from zarr.registry import get_ndbuffer_class from zarr.storage import MemoryStore, StorePath @@ -548,68 +549,96 @@ def test_setitem_repeated_index( @pytest.mark.parametrize("nchunks", [1, 2**8, 2**16, 2**16 + 1, 2**40]) -def test_stable_argsort_chunk_indices(nchunks: int) -> None: - """The sort matches NumPy's stable sort whether or not the chunk indices are narrowed.""" +def test_argsort_chunk_indices(nchunks: int) -> None: + """The sort groups chunk indices whether or not they are narrowed.""" rng = np.random.default_rng(0) chunk_indices = np.concatenate( [[0, nchunks - 1, nchunks - 1, 0], rng.integers(0, nchunks, 1000)] ).astype(np.intp) - expected = np.argsort(chunk_indices, kind="stable") - assert_array_equal(stable_argsort_chunk_indices(chunk_indices, nchunks), expected) + order = argsort_chunk_indices(chunk_indices, nchunks) + assert_array_equal(np.sort(order), np.arange(chunk_indices.size)) + assert_array_equal(chunk_indices[order], np.sort(chunk_indices)) _rng = np.random.default_rng(0) +def _reversed_within_chunk_argsort( + chunk_indices: npt.NDArray[np.intp], nchunks: int +) -> npt.NDArray[np.intp]: + """Group by chunk, listing each chunk's items in the reverse of the order given.""" + return np.lexsort((-np.arange(chunk_indices.size), chunk_indices)) + + +@pytest.mark.parametrize("within_chunk_order", ["given", "reversed"]) @pytest.mark.parametrize( - "selection", + ("method", "selection"), [ - (np.array([1, 2, 1, 0, 2]), slice(None)), - (slice(1, 3), np.array([2, 2, 0])), - (slice(None), _rng.integers(0, 40, 100)), - (np.array([1, 2, 2]), np.array([3, 3, 3])), - (np.array([2, 1, 2]), np.array([3, 0, 3])), - (_rng.integers(0, 30, 1000), _rng.integers(0, 40, 1000)), - (np.array([1, 2, 1]), 2), - np.ix_(np.array([2, 0, 2]), np.array([1, 1, 3])), - (np.array([0, 1, 2]), slice(None)), - (slice(None), _rng.permutation(40)), - tuple(np.unravel_index(_rng.permutation(1200), (30, 40))), - np.ix_(np.array([2, 0]), np.array([1, 3])), - (slice(None), slice(1, 3)), + ("oindex", ([5, 1, 5, 20, 1, 29, 5], slice(None))), + ("oindex", (slice(2, 30), [8, 8, 0, 39, 8, 3])), + ("oindex", ([1, 1, 2, 9, 9], slice(None))), + ("oindex", (_rng.integers(0, 30, 200), slice(None))), + ("oindex", ([5, 1, 5, 20], [8, 8, 0, 39, 8])), + ("oindex", (3, [8, 8, 0, 39, 8])), + ("oindex", ([20, 3, 9], [0, 39])), + ("vindex", ([5, 1, 5, 20, 5], [8, 8, 8, 0, 8])), + ("vindex", ([5, 1, 5], [8, 0, 8])), + ("vindex", ([0, 0, 1], [0, 0, 2])), + ("vindex", (_rng.integers(0, 30, 2000), _rng.integers(0, 40, 2000))), + ("vindex", ([20, 3, 9], [0, 39, 0])), ], ids=[ - "array_then_slice", - "slice_then_array", - "long_unsorted_array", - "sorted_coordinates", - "unsorted_coordinates", - "many_coordinates", - "array_and_integer", - "mesh", - "no_repeats_orthogonal", - "no_repeats_long_unsorted_array", - "no_repeats_many_coordinates", - "no_repeats_mesh", - "no_arrays", + "oindex_array_unordered", + "oindex_array_after_slice", + "oindex_array_increasing", + "oindex_array_many", + "oindex_mesh", + "oindex_mesh_with_integer", + "oindex_no_repeats", + "vindex_unordered", + "vindex_sparse", + "vindex_increasing", + "vindex_many", + "vindex_no_repeats", ], ) -def test_last_assignments(selection: tuple[Any, ...]) -> None: - """Writing through the resolved selection matches assigning each value in C order.""" - shape = (30, 40) - targets = np.arange(product(shape)).reshape(shape)[selection] - values = np.arange(targets.size).reshape(targets.shape) +def test_last_writes( + method: str, + selection: tuple[Any, ...], + within_chunk_order: str, + monkeypatch: pytest.MonkeyPatch, +) -> None: + """Each projection writes every element once, and the result is assigning the values in C order. + + The result must not depend on how the indexer orders the items within a chunk. + """ + if within_chunk_order == "reversed": + monkeypatch.setattr( + zarr.core.indexing, "argsort_chunk_indices", _reversed_within_chunk_argsort + ) + shape, chunks = (30, 40), (7, 9) + selection = tuple(np.asarray(s) if isinstance(s, list) else s for s in selection) + indexer: OrthogonalIndexer | CoordinateIndexer + if method == "oindex": + indexer = OrthogonalIndexer(selection, shape, ChunkGrid.from_sizes(shape, chunks)) + else: + indexer = CoordinateIndexer(selection, shape, ChunkGrid.from_sizes(shape, chunks)) + flat_ids = np.arange(product(shape)).reshape(shape) + targets = oindex(flat_ids, selection) if method == "oindex" else flat_ids[selection] + values = np.arange(targets.size).reshape(indexer.shape) expected = np.full(product(shape), -1) for target, value in zip(targets.ravel().tolist(), values.ravel().tolist(), strict=True): expected[target] = value - result = last_assignments(selection, shape) - if np.unique(targets).size == targets.size: - assert result is None - return - assert result is not None - actual = np.full(shape, -1) - actual[result.selection] = result.pick(values) - assert_array_equal(actual.ravel(), expected) + actual = np.full(product(shape), -1) + for projection in last_writes(indexer): + region = tuple( + slice(c * size, (c + 1) * size) + for c, size in zip(projection.chunk_coords, chunks, strict=True) + ) + written = flat_ids[region][projection.chunk_selection].ravel() + assert np.unique(written).size == written.size + actual[written] = values[projection.out_selection].ravel() + assert_array_equal(actual, expected) Index = list[int] | tuple[slice | int | list[int], ...] From c5ae78fec624b0d126701847a83aa5b440f185be Mon Sep 17 00:00:00 2001 From: Davis Vann Bennett Date: Wed, 18 Nov 2026 19:29:17 +0100 Subject: [PATCH 7/9] perf(indexing): leave repeated-index writes unspecified and radix-sort up to 2**32 chunks Match zarr-indexing (#4649): the value an element receives when an index array names it more than once is unspecified, as in NumPy. This removes `last_writes` and its per-chunk resolution from the write path, and documents the rule in the user guide and the setter docstrings. Chunk indices are now radix-sorted 16 bits at a time up to 2**32 chunks, which also covers arrays with more than 2**16 chunks: building an orthogonal indexer for 10M points over 1M chunks takes 233 ms instead of 817 ms on main. Assisted-by: ClaudeCode:claude-opus-5-5 Co-Authored-By: Claude Opus 5.5 --- changes/4643.bugfix.md | 1 - changes/4643.misc.md | 1 + docs/user-guide/arrays.md | 7 ++ src/zarr/core/array.py | 14 ++-- src/zarr/core/indexing.py | 144 +++----------------------------------- tests/test_indexing.py | 129 +++++++++------------------------- 6 files changed, 60 insertions(+), 236 deletions(-) delete mode 100644 changes/4643.bugfix.md create mode 100644 changes/4643.misc.md diff --git a/changes/4643.bugfix.md b/changes/4643.bugfix.md deleted file mode 100644 index 155367cabe..0000000000 --- a/changes/4643.bugfix.md +++ /dev/null @@ -1 +0,0 @@ -Assigning through `oindex`, `vindex` or `__setitem__` with an index array that repeats a position now stores the last value given for that position, as NumPy does. Previously the stored value depended on the platform, the chunk layout and the number of selected items. Zarr now picks the last value itself, so the result no longer depends on the order in which the array library applies repeated assignments. diff --git a/changes/4643.misc.md b/changes/4643.misc.md new file mode 100644 index 0000000000..d5a9a850e9 --- /dev/null +++ b/changes/4643.misc.md @@ -0,0 +1 @@ +When an `oindex`, `vindex` or `__setitem__` index array names an element more than once, the value the element receives is now documented as unspecified, as in NumPy. Remove repeated indices if the result matters. Grouping a fancy selection by chunk is also several times faster. diff --git a/docs/user-guide/arrays.md b/docs/user-guide/arrays.md index 5b025047b6..17f9b82b0d 100644 --- a/docs/user-guide/arrays.md +++ b/docs/user-guide/arrays.md @@ -549,6 +549,13 @@ z[:] = data print(np.all(z.oindex[[0, 2], :] == z[[0, 2], :])) ``` +### Repeated indices + +If an index array used for writing names an element more than once, the element is +set to one of the values given for it, and which one is unspecified, as in NumPy. +This applies to coordinate and orthogonal writes, through `vindex`, `oindex` +or the square bracket operator. Remove repeated indices if the result matters. + ### Block indexing Zarr also supports block indexing, which allows selections of whole chunks based on their diff --git a/src/zarr/core/array.py b/src/zarr/core/array.py index 5da88aadf9..e895b74958 100644 --- a/src/zarr/core/array.py +++ b/src/zarr/core/array.py @@ -109,7 +109,6 @@ is_pure_fancy_indexing, is_pure_orthogonal_indexing, is_scalar, - last_writes, pop_fields, ) from zarr.core.metadata import ( @@ -3314,6 +3313,10 @@ def set_orthogonal_selection( Slices with step > 1 are supported, but slices with negative step are not. + If `selection` names an element more than once, the element is set to one of the + values given for it, and which one is unspecified, as in NumPy. Remove repeated + indices if the result matters. + Related ------- [get_basic_selection][zarr.Array.get_basic_selection], @@ -3669,6 +3672,10 @@ def set_coordinate_selection( Slices are not supported. Coordinate arrays must be provided for all dimensions of the array. + If `selection` names an element more than once, the element is set to one of the + values given for it, and which one is unspecified, as in NumPy. Remove repeated + indices if the result matters. + Related ------- [get_basic_selection][zarr.Array.get_basic_selection], @@ -5902,10 +5909,7 @@ async def _set_selection( out_selection, is_complete_chunk, ) - # A scalar gives every element the same value, so repeats need no resolving. - for chunk_coords, chunk_selection, out_selection, is_complete_chunk in ( - indexer if value_buffer.shape == () else last_writes(indexer) - ) + for chunk_coords, chunk_selection, out_selection, is_complete_chunk in indexer ], value_buffer, drop_axes=indexer.drop_axes, diff --git a/src/zarr/core/indexing.py b/src/zarr/core/indexing.py index 7a517c7377..d5d8217522 100644 --- a/src/zarr/core/indexing.py +++ b/src/zarr/core/indexing.py @@ -767,147 +767,21 @@ def argsort_chunk_indices( ) -> npt.NDArray[np.intp]: """Return the indices that sort `chunk_indices`, each in `range(nchunks)`. - The order of items that share a chunk is unspecified: writes resolve repeated - indices by their position in the selection (see `last_writes`), not by their order - within a chunk. NumPy sorts integers of at most 16 bits with a radix sort, which is - several times faster than its default sort, so the indices are narrowed when - `nchunks` allows it. + NumPy sorts integers of at most 16 bits with a radix sort, several times faster than + its default sort, so up to `2**32` chunks the indices are sorted 16 bits at a time, + low half first. Each pass is stable, so items that share a chunk keep their order. + That order is an implementation detail: the value stored for a repeated index is + unspecified. """ if nchunks <= 2**16: return np.argsort(chunk_indices.astype(np.uint16), kind="stable") + if nchunks <= 2**32: + low = np.argsort((chunk_indices & 0xFFFF).astype(np.uint16), kind="stable") + high = (chunk_indices[low] >> 16).astype(np.uint16) + return low[np.argsort(high, kind="stable")] return np.argsort(chunk_indices) -def _last_by_rank( - targets: npt.NDArray[Any], ranks: npt.NDArray[Any] | None -) -> npt.NDArray[np.intp] | None: - """Positions of the entries to keep so that each target keeps its highest-ranked entry. - - `targets` holds non-negative integers that may repeat. `ranks` holds distinct - integers, one per entry; `None` means each entry's rank is its position. Returns - `None` when no target repeats. - """ - n = targets.size - if n < 2: - return None - if ranks is None and (targets[1:] >= targets[:-1]).all(): - # Non-decreasing targets ranked by position: repeats are adjacent, and the last - # of each run ranks highest. - steps = targets[1:] != targets[:-1] - if steps.all(): - return None - return np.flatnonzero(np.append(steps, True)) - if (targets[1:] > targets[:-1]).all(): - return None - if ranks is None: - ranks = np.arange(n, dtype=np.intp) - size = int(targets.max()) + 1 - if size <= 16 * n: - # Few possible targets for the number given: record the highest rank of each in - # linear time. A maximum does not depend on the order `ufunc.at` applies - # repeated targets in. - highest = np.full(size, np.iinfo(np.intp).min, dtype=np.intp) - np.maximum.at(highest, targets, ranks) - keep = np.flatnonzero(highest[targets] == ranks) - else: - order = np.lexsort((ranks, targets)) - ordered = targets[order] - keep = order[np.append(ordered[1:] != ordered[:-1], True)] - if keep.size == n: - return None - return keep - - -def _as_positions(out_sel: slice | npt.NDArray[Any]) -> npt.NDArray[np.intp]: - """The output positions a 1-D output selection names, as an array. - - Indexers give output slices an explicit start and stop, and no step. - """ - if isinstance(out_sel, slice): - return np.arange(out_sel.start, out_sel.stop, dtype=np.intp) - return out_sel.reshape(-1) - - -def _orthogonal_last_writes( - projection: ChunkProjection, drop_axes: tuple[int, ...] -) -> ChunkProjection: - """`last_writes` for one projection of an `OrthogonalIndexer`. - - An element repeats only where some axis repeats an index, and its last value in C - order is the one at the highest output position on each axis, so each axis is - resolved on its own. - """ - chunk_selection = cast("tuple[Any, ...]", projection.chunk_selection) - out_selection = cast("tuple[Any, ...]", projection.out_selection) - n_arrays = sum(isinstance(sel, np.ndarray) for sel in chunk_selection) - # The indexer meshes the selections with `ix_` exactly when this holds. - mesh = n_arrays > 1 or bool(drop_axes) - out_axes = [axis for axis in range(len(chunk_selection)) if axis not in drop_axes] - chunk_sels = list(chunk_selection) - out_sels = list(out_selection) - changed = False - for out_axis, axis in enumerate(out_axes): - chunk_sel = chunk_sels[axis] - if not (isinstance(chunk_sel, np.ndarray) and chunk_sel.dtype.kind in "iu"): - continue - out_sel = out_sels[out_axis] - ranks = None if isinstance(out_sel, slice) else out_sel.reshape(-1) - keep = _last_by_rank(chunk_sel.reshape(-1), ranks) - if keep is None: - continue - changed = True - chunk_sels[axis] = chunk_sel.reshape(-1)[keep] - out_sels[out_axis] = _as_positions(out_sel)[keep] - if not changed: - return projection - if mesh: - chunk_selection = np.ix_(*(sel.reshape(-1) for sel in chunk_sels)) - out_selection = np.ix_(*(_as_positions(sel) for sel in out_sels)) - else: - chunk_selection = tuple(chunk_sels) - out_selection = tuple(out_sels) - return projection._replace(chunk_selection=chunk_selection, out_selection=out_selection) - - -def _coordinate_last_writes(projection: ChunkProjection) -> ChunkProjection: - """`last_writes` for one projection of a `CoordinateIndexer`.""" - chunk_selection = cast("tuple[npt.NDArray[np.intp], ...]", projection.chunk_selection) - out_selection = cast("slice | npt.NDArray[np.intp]", projection.out_selection) - if chunk_selection[0].size < 2: - return projection - extent = tuple(int(sel.max()) + 1 for sel in chunk_selection) - targets = np.ravel_multi_index(chunk_selection, extent) - ranks = None if isinstance(out_selection, slice) else out_selection - keep = _last_by_rank(targets, ranks) - if keep is None: - return projection - return projection._replace( - chunk_selection=tuple(sel[keep] for sel in chunk_selection), - out_selection=_as_positions(out_selection)[keep], - ) - - -def last_writes(indexer: Indexer) -> Iterator[ChunkProjection]: - """The projections of `indexer`, each writing every element it touches once. - - A selection that repeats an element gives it several values. The value kept is the - last one in the C order of the selection, the result of assigning the values in - turn. It is chosen from each item's position in the selection, so it does not - depend on how items are ordered within a chunk, on the order chunks are written - in, or on how an array library orders repeated assignments, which NumPy leaves - undocumented and CuPy undefined. - """ - if isinstance(indexer, CoordinateIndexer): - for projection in indexer: - yield _coordinate_last_writes(projection) - elif isinstance(indexer, OrthogonalIndexer): - for projection in indexer: - yield _orthogonal_last_writes(projection, indexer.drop_axes) - else: - # Basic and block selections name each element once. - yield from indexer - - @dataclass(frozen=True) class IntArrayDimIndexer: """Integer array selection against a single dimension.""" diff --git a/tests/test_indexing.py b/tests/test_indexing.py index ed63274739..00f4957449 100644 --- a/tests/test_indexing.py +++ b/tests/test_indexing.py @@ -27,14 +27,12 @@ CoordinateSelection, IntArrayDimIndexer, MaskIndexer, - OrthogonalIndexer, OrthogonalSelection, Selection, _ArrayIndexingOrder, _iter_grid, _iter_regions, argsort_chunk_indices, - last_writes, make_slice_selection, normalize_integer_selection, oindex, @@ -510,6 +508,13 @@ def test_setitem_zarr_array_as_value() -> None: assert_array_equal(dst2[2:7], np.arange(2, 7)) +def _reversed_within_chunk_argsort( + chunk_indices: npt.NDArray[np.intp], nchunks: int +) -> npt.NDArray[np.intp]: + """Group `chunk_indices` by chunk, reversing the order of items within each chunk.""" + return np.lexsort((-np.arange(chunk_indices.size), chunk_indices)) + + @pytest.mark.parametrize("method", ["oindex", "vindex"]) @pytest.mark.parametrize("layout", ["unit_chunks", "partial_chunks", "one_chunk", "sharded"]) @pytest.mark.parametrize( @@ -517,16 +522,25 @@ def test_setitem_zarr_array_as_value() -> None: [ ((24,), ([-1, -1, 0, 0],)), ((24,), ([-1, 0, 0, -1],)), - # long enough that an unstable sort by chunk reorders the repeats ((24,), ([1] + [0] * 23,)), ((6, 6), ([0, 5, 0, -1, 2] * 4, [1, 1, 4, 4, 2] * 4)), ], ids=["adjacent", "interleaved", "many_repeats", "two_dimensions"], ) +@pytest.mark.parametrize("within_chunk_order", ["default", "reversed"]) def test_setitem_repeated_index( - method: str, layout: str, shape: tuple[int, ...], selection: tuple[list[int], ...] + method: str, + layout: str, + shape: tuple[int, ...], + selection: tuple[list[int], ...], + within_chunk_order: str, + monkeypatch: pytest.MonkeyPatch, ) -> None: - """Assignment with repeated indices stores the last value given for each repeated position, as NumPy does.""" + """Assignment with repeated indices sets each repeated element to one of its values, whole, and leaves the rest as NumPy would.""" + if within_chunk_order == "reversed": + monkeypatch.setattr( + zarr.core.indexing, "argsort_chunk_indices", _reversed_within_chunk_argsort + ) chunks = { "unit_chunks": (1,) * len(shape), "partial_chunks": (4,) * len(shape), @@ -535,22 +549,26 @@ def test_setitem_repeated_index( }[layout] shards = (6,) * len(shape) if layout == "sharded" else None array = zarr.create_array(store={}, shape=shape, chunks=chunks, shards=shards, dtype="i8") - expected = np.zeros(shape, dtype="i8") + flat_ids = np.arange(product(shape)).reshape(shape) if method == "oindex": - np_selection = np.ix_(*selection) - value = np.arange(1, 1 + expected[np_selection].size).reshape(expected[np_selection].shape) - expected[np_selection] = value + targets = flat_ids[np.ix_(*selection)] + value = np.arange(1, 1 + targets.size).reshape(targets.shape) array.oindex[selection] = value else: - value = np.arange(1, 1 + len(selection[0])) - expected[selection] = value + targets = flat_ids[selection] + value = np.arange(1, 1 + targets.size) array.vindex[selection] = value - assert_array_equal(array[:], expected) + written = array[:].reshape(-1) + targets = targets.reshape(-1) + value = value.reshape(-1) + assert (np.delete(written, targets) == 0).all() + for target in np.unique(targets): + assert written[target] in value[targets == target] -@pytest.mark.parametrize("nchunks", [1, 2**8, 2**16, 2**16 + 1, 2**40]) +@pytest.mark.parametrize("nchunks", [1, 2**8, 2**16, 2**16 + 1, 2**20, 2**32, 2**32 + 1, 2**40]) def test_argsort_chunk_indices(nchunks: int) -> None: - """The sort groups chunk indices whether or not they are narrowed.""" + """The sort groups items by chunk, and up to `2**32` chunks matches NumPy's stable sort.""" rng = np.random.default_rng(0) chunk_indices = np.concatenate( [[0, nchunks - 1, nchunks - 1, 0], rng.integers(0, nchunks, 1000)] @@ -558,87 +576,8 @@ def test_argsort_chunk_indices(nchunks: int) -> None: order = argsort_chunk_indices(chunk_indices, nchunks) assert_array_equal(np.sort(order), np.arange(chunk_indices.size)) assert_array_equal(chunk_indices[order], np.sort(chunk_indices)) - - -_rng = np.random.default_rng(0) - - -def _reversed_within_chunk_argsort( - chunk_indices: npt.NDArray[np.intp], nchunks: int -) -> npt.NDArray[np.intp]: - """Group by chunk, listing each chunk's items in the reverse of the order given.""" - return np.lexsort((-np.arange(chunk_indices.size), chunk_indices)) - - -@pytest.mark.parametrize("within_chunk_order", ["given", "reversed"]) -@pytest.mark.parametrize( - ("method", "selection"), - [ - ("oindex", ([5, 1, 5, 20, 1, 29, 5], slice(None))), - ("oindex", (slice(2, 30), [8, 8, 0, 39, 8, 3])), - ("oindex", ([1, 1, 2, 9, 9], slice(None))), - ("oindex", (_rng.integers(0, 30, 200), slice(None))), - ("oindex", ([5, 1, 5, 20], [8, 8, 0, 39, 8])), - ("oindex", (3, [8, 8, 0, 39, 8])), - ("oindex", ([20, 3, 9], [0, 39])), - ("vindex", ([5, 1, 5, 20, 5], [8, 8, 8, 0, 8])), - ("vindex", ([5, 1, 5], [8, 0, 8])), - ("vindex", ([0, 0, 1], [0, 0, 2])), - ("vindex", (_rng.integers(0, 30, 2000), _rng.integers(0, 40, 2000))), - ("vindex", ([20, 3, 9], [0, 39, 0])), - ], - ids=[ - "oindex_array_unordered", - "oindex_array_after_slice", - "oindex_array_increasing", - "oindex_array_many", - "oindex_mesh", - "oindex_mesh_with_integer", - "oindex_no_repeats", - "vindex_unordered", - "vindex_sparse", - "vindex_increasing", - "vindex_many", - "vindex_no_repeats", - ], -) -def test_last_writes( - method: str, - selection: tuple[Any, ...], - within_chunk_order: str, - monkeypatch: pytest.MonkeyPatch, -) -> None: - """Each projection writes every element once, and the result is assigning the values in C order. - - The result must not depend on how the indexer orders the items within a chunk. - """ - if within_chunk_order == "reversed": - monkeypatch.setattr( - zarr.core.indexing, "argsort_chunk_indices", _reversed_within_chunk_argsort - ) - shape, chunks = (30, 40), (7, 9) - selection = tuple(np.asarray(s) if isinstance(s, list) else s for s in selection) - indexer: OrthogonalIndexer | CoordinateIndexer - if method == "oindex": - indexer = OrthogonalIndexer(selection, shape, ChunkGrid.from_sizes(shape, chunks)) - else: - indexer = CoordinateIndexer(selection, shape, ChunkGrid.from_sizes(shape, chunks)) - flat_ids = np.arange(product(shape)).reshape(shape) - targets = oindex(flat_ids, selection) if method == "oindex" else flat_ids[selection] - values = np.arange(targets.size).reshape(indexer.shape) - expected = np.full(product(shape), -1) - for target, value in zip(targets.ravel().tolist(), values.ravel().tolist(), strict=True): - expected[target] = value - actual = np.full(product(shape), -1) - for projection in last_writes(indexer): - region = tuple( - slice(c * size, (c + 1) * size) - for c, size in zip(projection.chunk_coords, chunks, strict=True) - ) - written = flat_ids[region][projection.chunk_selection].ravel() - assert np.unique(written).size == written.size - actual[written] = values[projection.out_selection].ravel() - assert_array_equal(actual, expected) + if nchunks <= 2**32: + assert_array_equal(order, np.argsort(chunk_indices, kind="stable")) Index = list[int] | tuple[slice | int | list[int], ...] From 96faa6bc772d13b08ed25410bb65c4d5d36e88f8 Mon Sep 17 00:00:00 2001 From: Davis Vann Bennett Date: Wed, 18 Nov 2026 19:29:50 +0100 Subject: [PATCH 8/9] test(properties): skip repeated indices again, since their written value is unspecified Assisted-by: ClaudeCode:claude-opus-5-5 Co-Authored-By: Claude Opus 5.5 --- tests/test_properties.py | 24 ++++++++++++++++++++---- 1 file changed, 20 insertions(+), 4 deletions(-) diff --git a/tests/test_properties.py b/tests/test_properties.py index d5d385accf..b448a524aa 100644 --- a/tests/test_properties.py +++ b/tests/test_properties.py @@ -17,7 +17,7 @@ import hypothesis.extra.numpy as npst import hypothesis.strategies as st -from hypothesis import event, given, settings +from hypothesis import assume, event, given, settings from tests.conftest import declared_chunk_data_sizes from zarr.abc.store import Store @@ -199,8 +199,12 @@ async def test_oindex(data: st.DataObject) -> None: actual = await async_zarray.oindex.getitem(zindexer) assert_array_equal(nparray[npindexer], actual) - # sync set: an index that repeats a position (in either spelling) keeps the last value - # written to it, as in NumPy, so repeated indices are drawn like any other + # sync set + for idxr, size in zip(zindexer, nparray.shape, strict=True): + if isinstance(idxr, np.ndarray) and idxr.size != np.unique(idxr % size).size: + # behaviour of setitem with repeated indices is not guaranteed in practice + # Negative and positive spellings of the same index are duplicates too. + assume(False) # The sharding codec sees a coordinate selection (the GH4284 path) when the # chunk selection has more than one array axis or drops an integer axis. n_array_axes = sum(isinstance(idxr, np.ndarray) for idxr in zindexer) @@ -244,7 +248,19 @@ async def test_vindex(data: st.DataObject) -> None: actual = await async_zarray.vindex.getitem(indexer) assert_array_equal(nparray[indexer], actual) - # sync set: a repeated point keeps the last value written to it, as in NumPy + # sync set + # Reads preserve the supplied indices; normalize negative indices explicitly when + # detecting repeated points rather than relying on a read to mutate the indexer. + points = np.stack( + [ + (idxr % size).ravel() + for idxr, size in zip(np.broadcast_arrays(*indexer), nparray.shape, strict=True) + ], + axis=-1, + ) + if len(np.unique(points, axis=0)) != len(points): + # behaviour of setitem with repeated coordinates is not guaranteed in practice + assume(False) new_data = data.draw(numpy_arrays(shapes=st.just(actual.shape), dtype=nparray.dtype)) nparray[indexer] = new_data zarray.vindex[indexer] = new_data From 31e9588645bcc3f09d784e63f95bea855aa6476f Mon Sep 17 00:00:00 2001 From: Davis Vann Bennett Date: Wed, 18 Nov 2026 20:42:55 +0100 Subject: [PATCH 9/9] perf(indexing): radix-sort chunk indices for any chunk count Sort 16 bits at a time for as many passes as the chunk count needs (up to four), instead of falling back to NumPy's default sort above 2**32 chunks. Building an orthogonal indexer for 10M points over 2**40 chunks takes 342 ms instead of 838 ms. Below 128 items per pass the plain stable sort is faster and is used instead, so the sort is stable for every input. Assisted-by: ClaudeCode:claude-fable-5-2 Co-Authored-By: Claude Fable 5.2 --- src/zarr/core/indexing.py | 29 ++++++++++++++++++----------- tests/test_indexing.py | 19 ++++++++++--------- 2 files changed, 28 insertions(+), 20 deletions(-) diff --git a/src/zarr/core/indexing.py b/src/zarr/core/indexing.py index d5d8217522..e4384f236f 100644 --- a/src/zarr/core/indexing.py +++ b/src/zarr/core/indexing.py @@ -762,24 +762,31 @@ def sorted_run_ends( return a[run_starts].astype(np.intp, copy=False), run_ends +# Below this many items per 16-bit pass, NumPy's stable sort of the full-width indices is +# faster than radix-sorting them. +_RADIX_SORT_MIN_ITEMS_PER_PASS = 128 + + def argsort_chunk_indices( chunk_indices: npt.NDArray[np.intp], nchunks: int ) -> npt.NDArray[np.intp]: """Return the indices that sort `chunk_indices`, each in `range(nchunks)`. NumPy sorts integers of at most 16 bits with a radix sort, several times faster than - its default sort, so up to `2**32` chunks the indices are sorted 16 bits at a time, - low half first. Each pass is stable, so items that share a chunk keep their order. - That order is an implementation detail: the value stored for a repeated index is - unspecified. + its default sort, so the indices are sorted 16 bits at a time, lowest bits first. + For a few indices the plain stable sort is faster and is used instead. Either way + the sort is stable, so items that share a chunk keep their order. That order is an + implementation detail: the value stored for a repeated index is unspecified. """ - if nchunks <= 2**16: - return np.argsort(chunk_indices.astype(np.uint16), kind="stable") - if nchunks <= 2**32: - low = np.argsort((chunk_indices & 0xFFFF).astype(np.uint16), kind="stable") - high = (chunk_indices[low] >> 16).astype(np.uint16) - return low[np.argsort(high, kind="stable")] - return np.argsort(chunk_indices) + # ceil(bits / 16) passes; chunk indices are at most 64 bits wide + n_passes = min(4, max(1, -((nchunks - 1).bit_length() // -16))) + if chunk_indices.size < _RADIX_SORT_MIN_ITEMS_PER_PASS * n_passes: + return np.argsort(chunk_indices, kind="stable") + order = np.argsort(chunk_indices.astype(np.uint16), kind="stable") + for shift in range(16, 16 * n_passes, 16): + digit = (chunk_indices[order] >> shift).astype(np.uint16) + order = order[np.argsort(digit, kind="stable")] + return order @dataclass(frozen=True) diff --git a/tests/test_indexing.py b/tests/test_indexing.py index 00f4957449..2e010cc134 100644 --- a/tests/test_indexing.py +++ b/tests/test_indexing.py @@ -566,18 +566,19 @@ def test_setitem_repeated_index( assert written[target] in value[targets == target] -@pytest.mark.parametrize("nchunks", [1, 2**8, 2**16, 2**16 + 1, 2**20, 2**32, 2**32 + 1, 2**40]) -def test_argsort_chunk_indices(nchunks: int) -> None: - """The sort groups items by chunk, and up to `2**32` chunks matches NumPy's stable sort.""" +@pytest.mark.parametrize("n_items", [100, 2000]) +@pytest.mark.parametrize( + "nchunks", [1, 2**8, 2**16, 2**16 + 1, 2**32, 2**32 + 1, 2**48, 2**48 + 1, 2**62, 2**70] +) +def test_argsort_chunk_indices(nchunks: int, n_items: int) -> None: + """The sort matches NumPy's stable sort for any number of chunks, and for few or many items.""" rng = np.random.default_rng(0) + highest = min(nchunks, 2**63) - 1 chunk_indices = np.concatenate( - [[0, nchunks - 1, nchunks - 1, 0], rng.integers(0, nchunks, 1000)] + [[0, highest, highest, 0], rng.integers(0, highest + 1, n_items - 4)] ).astype(np.intp) - order = argsort_chunk_indices(chunk_indices, nchunks) - assert_array_equal(np.sort(order), np.arange(chunk_indices.size)) - assert_array_equal(chunk_indices[order], np.sort(chunk_indices)) - if nchunks <= 2**32: - assert_array_equal(order, np.argsort(chunk_indices, kind="stable")) + expected = np.argsort(chunk_indices, kind="stable") + assert_array_equal(argsort_chunk_indices(chunk_indices, nchunks), expected) Index = list[int] | tuple[slice | int | list[int], ...]