Skip to content
New issue

Have a question about this project? # for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “#”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? # to your account

parallelize formatter to speed up things #2632

Merged
merged 1 commit into from
Aug 30, 2024
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
35 changes: 30 additions & 5 deletions tools/cross/format.py
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@
import shutil
import subprocess
from argparse import ArgumentParser, Namespace
from concurrent.futures import ThreadPoolExecutor, as_completed
from dataclasses import dataclass
from pathlib import Path
from sys import exit
Expand Down Expand Up @@ -203,13 +204,23 @@ class FormatConfig:
]


def format(config: FormatConfig, files: list[Path], check: bool) -> bool:
def format(config: FormatConfig, files: list[Path], check: bool) -> tuple[bool, str]:
matching_files = filter_files_by_globs(files, Path(config.directory), config.globs)

if not matching_files:
return True
return (
True,
f"No matching files for {config.directory} ({', '.join(config.globs)})",
)

return config.formatter(matching_files, check)
result = config.formatter(matching_files, check)
message = (
f"{len(matching_files)} files in {config.directory} ({', '.join(config.globs)})"
)
return (
result,
f"{'Checked' if check else 'Formatted'} {message}",
)


def main() -> None:
Expand All @@ -222,8 +233,22 @@ def main() -> None:

all_ok = True

for config in FORMATTERS:
all_ok &= format(config, files, options.check)
with ThreadPoolExecutor() as executor:
future_to_config = {
executor.submit(format, config, files, options.check): config
for config in FORMATTERS
}
for future in as_completed(future_to_config):
config = future_to_config[future]
try:
result, message = future.result()
all_ok &= result
logging.info(message)
except Exception:
logging.exception(
f"Formatter for {config.directory} generated an exception"
)
all_ok = False

if not all_ok:
logging.error(
Expand Down
Loading