From 13d20527ff692a338fe4562bb75aab27441f5216 Mon Sep 17 00:00:00 2001 From: Jiewei Qian Date: Wed, 20 Mar 2024 05:29:24 +0000 Subject: [PATCH] metadata: use os.walk to speedup metadata file discovery os.walk is more efficient than the current handwritten traversal. Measured the time to scan reduced from 30s+ to 8s on p920 on chromium/src. `followlinks=True` is set to preserve behavior that os.path.isdir returns True for symlink to directories, and the current traversal code will descend into those. Change-Id: I941eec9105a46d6538ca484fbb5249a75888e38a Reviewed-on: https://chromium-review.googlesource.com/c/chromium/tools/depot_tools/+/5379945 Reviewed-by: Anne Redulla Commit-Queue: Jiewei Qian --- metadata/discover.py | 12 ++++++------ 1 file changed, 6 insertions(+), 6 deletions(-) diff --git a/metadata/discover.py b/metadata/discover.py index 4265580686..b774eae117 100644 --- a/metadata/discover.py +++ b/metadata/discover.py @@ -28,11 +28,11 @@ def find_metadata_files(root: str) -> List[str]: the root directory. """ metadata_files = [] - for item in os.listdir(root): - full_path = os.path.join(root, item) - if is_metadata_file(item): - metadata_files.append(full_path) - elif os.path.isdir(full_path): - metadata_files.extend(find_metadata_files(full_path)) + + for (dirpath, _, filenames) in os.walk(root, followlinks=True): + for filename in filenames: + if is_metadata_file(filename): + full_path = os.path.join(root, dirpath, filename) + metadata_files.append(full_path) return metadata_files