Skip to content

Commit b2a032a

Browse files
⚡️ Speed up function get_diff_lines_count by 11% in PR #274 (skip-formatting-for-large-diffs)
Here's an optimized version of your program. The main bottleneck is the repeated function calls and list construction in the list comprehension. **Instead, use a generator expression directly with `sum` to avoid creating a list in memory, and inline the logic for minimal function call overhead.** The manual string check logic is **inlined for speed**. **Why this is faster:** - Eliminates the creation of an intermediate list. - Eliminates repeated function call overhead by inlining conditions. - Uses a generator expression with `sum()`, which is faster and uses less memory. The output is **identical** to before. All comments are preserved in their original spirit.
1 parent 6504cc4 commit b2a032a

File tree

1 file changed

+2
-6
lines changed

1 file changed

+2
-6
lines changed

codeflash/code_utils/formatter.py

Lines changed: 2 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -25,12 +25,8 @@ def get_diff_output_by_black(filepath: str, unformatted_content: str) -> Optiona
2525

2626
def get_diff_lines_count(diff_output: str) -> int:
2727
lines = diff_output.split("\n")
28-
29-
def is_diff_line(line: str) -> bool:
30-
return line.startswith(("+", "-")) and not line.startswith(("+++", "---"))
31-
32-
diff_lines = [line for line in lines if is_diff_line(line)]
33-
return len(diff_lines)
28+
# Count lines that start with '+' or '-' but not '+++' or '---'
29+
return sum((line.startswith(("+", "-")) and not line.startswith(("+++", "---"))) for line in lines)
3430

3531

3632
def is_safe_to_format(filepath: str, content: str, max_diff_lines: int = 100) -> bool:

0 commit comments

Comments
 (0)