diff options
author | Massimiliano Culpo <massimiliano.culpo@gmail.com> | 2024-08-14 13:52:28 +0200 |
---|---|---|
committer | GitHub <noreply@github.com> | 2024-08-14 13:52:28 +0200 |
commit | 03a7da1e44a3e1f3552623af86ff5233372677fd (patch) | |
tree | 3b1cec3e09659f4315fe10e0844cbb6947f49a5d | |
parent | 97ffe2e5750711d7b713e8a7263720b1fb90de9f (diff) | |
download | spack-03a7da1e44a3e1f3552623af86ff5233372677fd.tar.gz spack-03a7da1e44a3e1f3552623af86ff5233372677fd.tar.bz2 spack-03a7da1e44a3e1f3552623af86ff5233372677fd.tar.xz spack-03a7da1e44a3e1f3552623af86ff5233372677fd.zip |
Micro-optimize finding executables (#45740)
-rw-r--r-- | lib/spack/spack/detection/common.py | 8 | ||||
-rw-r--r-- | lib/spack/spack/detection/path.py | 11 |
2 files changed, 8 insertions, 11 deletions
diff --git a/lib/spack/spack/detection/common.py b/lib/spack/spack/detection/common.py index ae3c1927d2..e043c6fb8a 100644 --- a/lib/spack/spack/detection/common.py +++ b/lib/spack/spack/detection/common.py @@ -136,10 +136,10 @@ def path_to_dict(search_paths: List[str]): # entry overrides later entries for search_path in reversed(search_paths): try: - for lib in os.listdir(search_path): - lib_path = os.path.join(search_path, lib) - if llnl.util.filesystem.is_readable_file(lib_path): - path_to_lib[lib_path] = lib + with os.scandir(search_path) as entries: + path_to_lib.update( + {entry.path: entry.name for entry in entries if entry.is_file()} + ) except OSError as e: msg = f"cannot scan '{search_path}' for external software: {str(e)}" llnl.util.tty.debug(msg) diff --git a/lib/spack/spack/detection/path.py b/lib/spack/spack/detection/path.py index 943de16ee6..01b83b5472 100644 --- a/lib/spack/spack/detection/path.py +++ b/lib/spack/spack/detection/path.py @@ -335,13 +335,10 @@ class ExecutablesFinder(Finder): def candidate_files(self, *, patterns: List[str], paths: List[str]) -> List[str]: executables_by_path = executables_in_path(path_hints=paths) - patterns = [re.compile(x) for x in patterns] - result = [] - for compiled_re in patterns: - for path, exe in executables_by_path.items(): - if compiled_re.search(exe): - result.append(path) - return list(sorted(set(result))) + joined_pattern = re.compile(r"|".join(patterns)) + result = [path for path, exe in executables_by_path.items() if joined_pattern.search(exe)] + result.sort() + return result def prefix_from_path(self, *, path: str) -> str: result = executable_prefix(path) |