Skip to content
Draft
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
19 changes: 18 additions & 1 deletion xarray/core/dataset.py
Original file line number Diff line number Diff line change
Expand Up @@ -316,6 +316,17 @@ def _get_breaks_cached(
return None


_STR_CACHE = dict()


def cached_str(chunks: tuple[int, ...]) -> str:
if id(chunks) not in _STR_CACHE:
value = str(chunks)
_STR_CACHE[id(chunks)] = value

return _STR_CACHE[id(chunks)]


def _maybe_chunk(
name: Hashable,
var: Variable,
Expand Down Expand Up @@ -344,7 +355,13 @@ def _maybe_chunk(
# by providing chunks as an input to tokenize.
# subtle bugs result otherwise. see GH3350
# we use str() for speed, and use the name for the final array name on the next line
token2 = tokenize(token if token else var._data, str(chunks))
token2 = tokenize(
token if token else var._data,
[
cached_str(_)
for _ in itertools.chain(chunks.keys(), chunks.values())
],
)
name2 = f"{name_prefix}{name}-{token2}"

from_array_kwargs = utils.consolidate_dask_from_array_kwargs(
Expand Down
7 changes: 6 additions & 1 deletion xarray/namedarray/daskmanager.py
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@

try:
from dask.array import Array as DaskArray

except ImportError:
DaskArray = np.ndarray[Any, Any]

Expand Down Expand Up @@ -52,7 +53,11 @@ def normalize_chunks(
previous_chunks: _NormalizedChunks | None = None,
) -> Any:
"""Called by open_dataset"""
from dask.array.core import normalize_chunks
# FIXME: switch to explicit dask version
try:
from dask.array.api import normalize_chunks_cached as normalize_chunks
except ImportError:
from dask.array.core import normalize_chunks

return normalize_chunks(
chunks,
Expand Down