Skip to content

Commit 96b76ee

Browse files
committed
Improve performance of format.py by 2.5x
We were spending a significant part of the time in `format.py` by running the `glob` queries for the files to exclude. Switch those to work based on the paths in the `files_to_format` list, improving performance of the script from ~5s to ~2s.
1 parent 02da1a1 commit 96b76ee

File tree

1 file changed

+18
-15
lines changed

1 file changed

+18
-15
lines changed

format.py

Lines changed: 18 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -86,21 +86,24 @@ def find_swiftformat(swift_format: str) -> Path:
8686

8787
def get_files_to_format() -> List[Path]:
8888
package_dir = Path(__file__).parent
89-
files_to_format = set(package_dir.glob('**/*.swift'))
90-
files_to_exclude = set()
91-
92-
# Don't format gyb_generated files
93-
files_to_exclude.update(package_dir.glob('**/gyb_generated/**/*.swift'))
94-
# Don't generate lit_tests files
95-
files_to_exclude.update(package_dir.glob('**/lit_tests/**/*.swift'))
96-
# Don't format generated files
97-
files_to_exclude.update(package_dir.glob('**/generated/**/*.swift'))
98-
# Don't format .build folder and content
99-
files_to_exclude.update(package_dir.glob('**/.build/**/*.swift'))
100-
# Don't format test input files
101-
files_to_exclude.update(package_dir.glob('**/Inputs/**/*.swift'))
102-
103-
return list(files_to_format.difference(files_to_exclude))
89+
files_to_format = package_dir.glob('**/*.swift')
90+
91+
def should_exclude(path: Path) -> bool:
92+
if 'gyb_generated' in path.parts:
93+
return True
94+
elif 'lit_tests' in path.parts:
95+
return True
96+
elif 'generated' in path.parts:
97+
return True
98+
elif '/build' in path.parts:
99+
return True
100+
elif 'Inputs' in path.parts:
101+
return True
102+
return False
103+
104+
files_to_format = [file for file in files_to_format if not should_exclude(file)]
105+
106+
return files_to_format
104107

105108

106109
def main() -> None:

0 commit comments

Comments
 (0)