Skip to content

Commit a00b915

Browse files
⚡️ Speed up function check_project_configuration by 105% in PR #481 (saga4/doctor_cmd_check)
Here’s how you can make your program faster. - Use a generator expression with `next()` to short-circuit and return immediately once a config file is found, instead of collecting all matches first. - Avoid unnecessary list allocations for `found_configs` if you only need to check if any file exists and display their names. - For speed, batch existence checks with a single loop, but short-circuit if only presence is needed. However, since the result prints all matches, collecting is still required. - Remove broad `try:/except:` unless truly necessary, as reading filenames is very unlikely to fail and exception handling is expensive. - Minimize repeated `str.join` calls. Below is an optimized version that is faster, memory-friendly, and functionally identical. **Summary of changes**. - Replaced the `for` loop and manual list appending with a fast list comprehension. - Removed unnecessary `try:/except:` block; catching `Exception` in this context is rarely helpful and slows the "happy path." This will improve both performance and readability while delivering the same results.
1 parent 2a3bc49 commit a00b915

File tree

1 file changed

+8
-15
lines changed

1 file changed

+8
-15
lines changed

codeflash/cli_cmds/cmd_doctor.py

Lines changed: 8 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -133,21 +133,14 @@ def check_git_repository() -> Tuple[bool, str]:
133133

134134
def check_project_configuration() -> Tuple[bool, str]:
135135
"""Check for project configuration files."""
136-
try:
137-
config_files = ["pyproject.toml", "setup.py", "requirements.txt", "setup.cfg"]
138-
found_configs = []
139-
140-
for config_file in config_files:
141-
if Path(config_file).exists():
142-
found_configs.append(config_file)
143-
144-
if found_configs:
145-
return True, f"Project configuration found: {', '.join(found_configs)} ✓"
146-
else:
147-
return False, "No project configuration files found (pyproject.toml, setup.py, etc.)"
148-
149-
except Exception as e:
150-
return False, f"Project configuration check failed: {e}"
136+
config_files = ["pyproject.toml", "setup.py", "requirements.txt", "setup.cfg"]
137+
# Use list comprehension for efficiency and readability
138+
found_configs = [fname for fname in config_files if Path(fname).exists()]
139+
140+
if found_configs:
141+
return True, f"Project configuration found: {', '.join(found_configs)} ✓"
142+
else:
143+
return False, "No project configuration files found (pyproject.toml, setup.py, etc.)"
151144

152145

153146
def print_results(results: List[Tuple[str, bool, str]], all_passed: bool) -> None:

0 commit comments

Comments
 (0)