Skip to content

Commit 6efb179

Browse files
⚡️ Speed up function get_diff_lines_count by 12% in PR #274 (skip-formatting-for-large-diffs)
Here’s a more optimized version of your program. **Optimization rationale:** - Eliminated the nested function, reducing overhead. - Avoided creating an intermediate list, directly counting matching lines. - Used string indexing instead of `startswith()` for single-char checks for slightly less overhead. - Preserved all relevant comments (there were none to preserve).
1 parent ce15022 commit 6efb179

File tree

1 file changed

+19
-10
lines changed

1 file changed

+19
-10
lines changed

codeflash/code_utils/formatter.py

Lines changed: 19 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -4,13 +4,15 @@
44
import shlex
55
import subprocess
66
from typing import TYPE_CHECKING, Optional
7+
78
import isort
89

910
from codeflash.cli_cmds.console import console, logger
1011

1112
if TYPE_CHECKING:
1213
from pathlib import Path
1314

15+
1416
def get_diff_output(cmd: list[str]) -> Optional[str]:
1517
try:
1618
result = subprocess.run(cmd, capture_output=True, text=True, check=True)
@@ -29,25 +31,32 @@ def get_diff_output(cmd: list[str]) -> Optional[str]:
2931
def get_diff_lines_output_by_black(filepath: str) -> Optional[str]:
3032
try:
3133
import black # type: ignore
32-
return get_diff_output(['black', '--diff', filepath])
34+
35+
return get_diff_output(["black", "--diff", filepath])
3336
except ImportError:
3437
return None
3538

39+
3640
def get_diff_lines_output_by_ruff(filepath: str) -> Optional[str]:
3741
try:
3842
import ruff # type: ignore
39-
return get_diff_output(['ruff', 'format', '--diff', filepath])
43+
44+
return get_diff_output(["ruff", "format", "--diff", filepath])
4045
except ImportError:
4146
print("can't import ruff")
4247
return None
4348

4449

4550
def get_diff_lines_count(diff_output: str) -> int:
46-
lines = diff_output.split('\n')
47-
def is_diff_line(line: str) -> bool:
48-
return line.startswith(('+', '-')) and not line.startswith(('+++', '---'))
49-
diff_lines = [line for line in lines if is_diff_line(line)]
50-
return len(diff_lines)
51+
count = 0
52+
for line in diff_output.split("\n"):
53+
# Check only the minimal needed prefixes for diff lines
54+
if line:
55+
first = line[0]
56+
if (first == "+" or first == "-") and not (line.startswith("+++") or line.startswith("---")):
57+
count += 1
58+
return count
59+
5160

5261
def is_safe_to_format(filepath: str, max_diff_lines: int = 100) -> bool:
5362
diff_changes_stdout = None
@@ -60,15 +69,15 @@ def is_safe_to_format(filepath: str, max_diff_lines: int = 100) -> bool:
6069
if diff_changes_stdout is None:
6170
logger.warning("Both ruff, black formatters not found, skipping formatting diff check.")
6271
return False
63-
72+
6473
diff_lines_count = get_diff_lines_count(diff_changes_stdout)
65-
74+
6675
if diff_lines_count > max_diff_lines:
6776
logger.debug(f"Skipping {filepath}: {diff_lines_count} lines would change (max: {max_diff_lines})")
6877
return False
6978

7079
return True
71-
80+
7281

7382
def format_code(formatter_cmds: list[str], path: Path, print_status: bool = True) -> str: # noqa
7483
# TODO: Only allow a particular whitelist of formatters here to prevent arbitrary code execution

0 commit comments

Comments
 (0)