Skip to content

Commit d877e5b

Browse files
committed
Use path + modification time as cache key to detect file changes
1 parent 47845a4 commit d877e5b

File tree

2 files changed

+11
-5
lines changed

2 files changed

+11
-5
lines changed

deepmd/env.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -50,7 +50,7 @@
5050
)
5151

5252
# Dynamic calculation of cache size
53-
_default_lru_cache_size = 888
53+
_default_lru_cache_size = 512
5454
LRU_CACHE_SIZE = _default_lru_cache_size
5555

5656
if platform.system() != "Windows":

deepmd/utils/data.py

Lines changed: 10 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -502,9 +502,13 @@ def _get_natoms_2(self, ntypes: int) -> tuple[int, np.ndarray]:
502502
return natoms, natoms_vec
503503

504504
def _get_memmap(self, path: DPPath) -> np.memmap:
505-
"""Get or create a memory-mapped object for a given npy file."""
506-
abs_path_str = str(Path(str(path)).absolute())
507-
return self._create_memmap(abs_path_str)
505+
"""Get or create a memory-mapped object for a given npy file.
506+
Uses file path and modification time as cache keys to detect file changes
507+
and invalidate cache when files are modified.
508+
"""
509+
abs_path = Path(str(path)).absolute()
510+
file_mtime = abs_path.stat().st_mtime
511+
return self._create_memmap(str(abs_path), str(file_mtime))
508512

509513
def _get_subdata(
510514
self, data: dict[str, Any], idx: Optional[np.ndarray] = None
@@ -962,14 +966,16 @@ def _check_mode(self, set_path: DPPath) -> bool:
962966

963967
@staticmethod
964968
@functools.lru_cache(maxsize=LRU_CACHE_SIZE)
965-
def _create_memmap(path_str: str) -> np.memmap:
969+
def _create_memmap(path_str: str, mtime_str: str) -> np.memmap:
966970
"""A cached helper function to create memmap objects.
967971
Using lru_cache to limit the number of open file handles.
968972
969973
Parameters
970974
----------
971975
path_str
972976
The file path as a string.
977+
mtime_str
978+
The modification time as a string, used for cache invalidation.
973979
"""
974980
with open(path_str, "rb") as f:
975981
version = np.lib.format.read_magic(f)

0 commit comments

Comments
 (0)