Skip to content

Commit 0f7a034

Browse files
authored
Add lru_cache to named_array.utils.module_available and core.utils.module_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.
1 parent f33a632 commit 0f7a034

File tree

2 files changed

+3
-0
lines changed

2 files changed

+3
-0
lines changed

xarray/core/utils.py

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1159,6 +1159,7 @@ def contains_only_chunked_or_numpy(obj) -> bool:
11591159
)
11601160

11611161

1162+
@functools.lru_cache
11621163
def module_available(module: str, minversion: str | None = None) -> bool:
11631164
"""Checks whether a module is installed without importing it.
11641165

xarray/namedarray/utils.py

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@
33
import sys
44
import warnings
55
from collections.abc import Hashable, Iterable, Iterator, Mapping
6+
from functools import lru_cache
67
from typing import TYPE_CHECKING, Any, TypeVar, cast
78

89
import numpy as np
@@ -32,6 +33,7 @@
3233
T = TypeVar("T")
3334

3435

36+
@lru_cache
3537
def module_available(module: str) -> bool:
3638
"""Checks whether a module is installed without importing it.
3739

0 commit comments

Comments
 (0)