Skip to content
Open
Show file tree
Hide file tree
Changes from 3 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
6 changes: 6 additions & 0 deletions doc/whats-new.rst
Original file line number Diff line number Diff line change
Expand Up @@ -42,6 +42,12 @@ Documentation
Internal Changes
~~~~~~~~~~~~~~~~

- Speed up :py:meth:`Dataset.load` and other ``.load()`` paths on datasets
with many variables by short-circuiting ``is_chunked_array`` for
``numpy.ndarray`` inputs and removing a duplicate ``is_duck_array``
call inside the function. Roughly 1.5× faster ``isel().load()`` on a
400-scalar-var dataset (0.52 ms → 0.34 ms); no effect on chunked paths.


.. _whats-new.2026.04.0:

Expand Down
17 changes: 15 additions & 2 deletions xarray/namedarray/pycompat.py
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,11 @@
from packaging.version import Version

from xarray.core.utils import is_scalar
from xarray.namedarray.utils import is_duck_array, is_duck_dask_array
from xarray.namedarray.utils import (
is_dask_collection,
is_duck_array,
is_duck_dask_array,
)

integer_types = (int, np.integer)

Expand Down Expand Up @@ -89,7 +93,16 @@ def mod_version(mod: ModType) -> Version:


def is_chunked_array(x: duckarray[Any, Any]) -> bool:
return is_duck_dask_array(x) or (is_duck_array(x) and hasattr(x, "chunks"))
# Fast path: ndarrays (and ndarray subclasses without a `chunks` attribute,
# e.g. MaskedArray) are the dominant non-dask case and skip the duck-array
# protocol work entirely. Subclasses that *do* expose `chunks` — test fakes
# like DummyChunkedArray, or third-party chunked-array impls — fall through
# to the full check below.
if isinstance(x, np.ndarray) and not hasattr(x, "chunks"):
return False
if not is_duck_array(x):
return False
return is_dask_collection(x) or hasattr(x, "chunks")


def is_0d_dask_array(x: duckarray[Any, Any]) -> bool:
Expand Down
Loading