Skip to content

Avoid copying vectorized indexes #10316

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 6 commits into from
May 27, 2025
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
50 changes: 38 additions & 12 deletions asv_bench/benchmarks/indexing.py
Original file line number Diff line number Diff line change
Expand Up @@ -39,18 +39,30 @@
"2d-1scalar": xr.DataArray(randn(100, frac_nan=0.1), dims=["x"]),
}

vectorized_indexes = {
"1-1d": {"x": xr.DataArray(randint(0, nx, 400), dims="a")},
"2-1d": {
"x": xr.DataArray(randint(0, nx, 400), dims="a"),
"y": xr.DataArray(randint(0, ny, 400), dims="a"),
},
"3-2d": {
"x": xr.DataArray(randint(0, nx, 400).reshape(4, 100), dims=["a", "b"]),
"y": xr.DataArray(randint(0, ny, 400).reshape(4, 100), dims=["a", "b"]),
"t": xr.DataArray(randint(0, nt, 400).reshape(4, 100), dims=["a", "b"]),
},
}

def make_vectorized_indexes(n_index):
return {
"1-1d": {"x": xr.DataArray(randint(0, nx, n_index), dims="a")},
"2-1d": {
"x": xr.DataArray(randint(0, nx, n_index), dims="a"),
"y": xr.DataArray(randint(0, ny, n_index), dims="a"),
},
"3-2d": {
"x": xr.DataArray(
randint(0, nx, n_index).reshape(n_index // 100, 100), dims=["a", "b"]
),
"y": xr.DataArray(
randint(0, ny, n_index).reshape(n_index // 100, 100), dims=["a", "b"]
),
"t": xr.DataArray(
randint(0, nt, n_index).reshape(n_index // 100, 100), dims=["a", "b"]
),
},
}


vectorized_indexes = make_vectorized_indexes(400)
big_vectorized_indexes = make_vectorized_indexes(400_000)

vectorized_assignment_values = {
"1-1d": xr.DataArray(randn((400, ny)), dims=["a", "y"], coords={"a": randn(400)}),
Expand Down Expand Up @@ -101,6 +113,20 @@ def time_indexing_basic_ds_large(self, key):
self.ds_large.isel(**basic_indexes[key]).load()


class IndexingOnly(Base):
@parameterized(["key"], [list(basic_indexes.keys())])
def time_indexing_basic(self, key):
self.ds.isel(**basic_indexes[key])

@parameterized(["key"], [list(outer_indexes.keys())])
def time_indexing_outer(self, key):
self.ds.isel(**outer_indexes[key])

@parameterized(["key"], [list(big_vectorized_indexes.keys())])
def time_indexing_big_vectorized(self, key):
self.ds.isel(**big_vectorized_indexes[key])


class Assignment(Base):
@parameterized(["key"], [list(basic_indexes.keys())])
def time_assignment_basic(self, key):
Expand Down
8 changes: 8 additions & 0 deletions doc/whats-new.rst
Original file line number Diff line number Diff line change
Expand Up @@ -59,6 +59,14 @@ Bug fixes
and prevents round-tripping them as :py:class:`numpy.datetime64` values
(:pull:`10352`). By `Spencer Clark <https://github.com/spencerkclark>`_.

Performance
~~~~~~~~~~~
- Lazily indexed arrays now use less memory to store keys by avoiding copies
in :py:class:`~xarray.indexing.VectorizedIndexer` and :py:class:`~xarray.indexing.OuterIndexer`
(:issue:`10316`).
By `Jesse Rusak <https://github.com/jder>`_.
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

🎉



Documentation
~~~~~~~~~~~~~

Expand Down
4 changes: 2 additions & 2 deletions xarray/core/indexing.py
Original file line number Diff line number Diff line change
Expand Up @@ -444,7 +444,7 @@ def __init__(
f"invalid indexer array for {type(self).__name__}; must be scalar "
f"or have 1 dimension: {k!r}"
)
k = k.astype(np.int64) # type: ignore[union-attr]
k = duck_array_ops.astype(k, np.int64, copy=False)
else:
raise TypeError(
f"unexpected indexer type for {type(self).__name__}: {k!r}"
Expand Down Expand Up @@ -488,7 +488,7 @@ def __init__(self, key: tuple[slice | np.ndarray[Any, np.dtype[np.generic]], ...
"invalid indexer key: ndarray arguments "
f"have different numbers of dimensions: {ndims}"
)
k = k.astype(np.int64) # type: ignore[union-attr]
k = duck_array_ops.astype(k, np.int64, copy=False)
else:
raise TypeError(
f"unexpected indexer type for {type(self).__name__}: {k!r}"
Expand Down
Loading
pFad - Phonifier reborn

Pfad - The Proxy pFad of © 2024 Garber Painting. All rights reserved.

Note: This service is not intended for secure transactions such as banking, social media, email, or purchasing. Use at your own risk. We assume no liability whatsoever for broken pages.


Alternative Proxies:

Alternative Proxy

pFad Proxy

pFad v3 Proxy

pFad v4 Proxy