Skip to content

Commit

Permalink
Add lru_cache to named_array.utils.module_available and core.utils.mo…
Browse files Browse the repository at this point in the history
…dule_available (#8717)

A significant time in
xarray.backends.common.py:AbstractWriteableDataStore.set_variables is
spent on common.py:is_dask_collection as it checks for the presence
of the module dask.

This time becomes significant in the case of many small files.
  • Loading branch information
eivindjahren authored Feb 7, 2024
1 parent f33a632 commit 0f7a034
Show file tree
Hide file tree
Showing 2 changed files with 3 additions and 0 deletions.
1 change: 1 addition & 0 deletions xarray/core/utils.py
Original file line number Diff line number Diff line change
Expand Up @@ -1159,6 +1159,7 @@ def contains_only_chunked_or_numpy(obj) -> bool:
)


@functools.lru_cache
def module_available(module: str, minversion: str | None = None) -> bool:
"""Checks whether a module is installed without importing it.
Expand Down
2 changes: 2 additions & 0 deletions xarray/namedarray/utils.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
import sys
import warnings
from collections.abc import Hashable, Iterable, Iterator, Mapping
from functools import lru_cache
from typing import TYPE_CHECKING, Any, TypeVar, cast

import numpy as np
Expand Down Expand Up @@ -32,6 +33,7 @@
T = TypeVar("T")


@lru_cache
def module_available(module: str) -> bool:
"""Checks whether a module is installed without importing it.
Expand Down

0 comments on commit 0f7a034

Please sign in to comment.