Skip to content

Commit 4b9498b

Browse files
committed
perf: use rglob("type.raw") to find systems instead of find * and then find type.raw. 10x accelarate
1 parent c659e9d commit 4b9498b

File tree

2 files changed

+19
-1
lines changed

2 files changed

+19
-1
lines changed

deepmd/common.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -202,7 +202,7 @@ def expand_sys_str(root_dir: Union[str, Path]) -> list[str]:
202202
list of string pointing to system directories
203203
"""
204204
root_dir = DPPath(root_dir)
205-
matches = [str(d) for d in root_dir.rglob("*") if (d / "type.raw").is_file()]
205+
matches = [str(p.parent) for p in root_dir.rglob("type.raw") if p.is_file()]
206206
if (root_dir / "type.raw").is_file():
207207
matches.append(str(root_dir))
208208
return matches

deepmd/utils/path.py

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -157,6 +157,11 @@ def mkdir(self, parents: bool = False, exist_ok: bool = False) -> None:
157157
If true, no error will be raised if the target directory already exists.
158158
"""
159159

160+
@property
161+
@abstractmethod
162+
def parent(self) -> "DPPath":
163+
"""Return the parent path."""
164+
160165

161166
class DPOSPath(DPPath):
162167
"""The OS path class to data system (DeepmdData) for real directories.
@@ -267,6 +272,11 @@ def name(self) -> str:
267272
"""Name of the path."""
268273
return self.path.name
269274

275+
@property
276+
def parent(self) -> "DPPath":
277+
"""Return the parent path."""
278+
return type(self)(self.path.parent, mode=self.mode)
279+
270280
def mkdir(self, parents: bool = False, exist_ok: bool = False) -> None:
271281
"""Make directory.
272282
@@ -469,6 +479,14 @@ def name(self) -> str:
469479
"""Name of the path."""
470480
return self._name.split("/")[-1]
471481

482+
@property
483+
def parent(self) -> "DPPath":
484+
"""Return the parent path."""
485+
parent_name = "/".join(self._name.split("/")[:-1])
486+
if not parent_name:
487+
parent_name = "/"
488+
return type(self)(f"{self.root_path}#{parent_name}", mode=self.mode)
489+
472490
def mkdir(self, parents: bool = False, exist_ok: bool = False) -> None:
473491
"""Make directory.
474492

0 commit comments

Comments
 (0)