From eb8b23aafc7aed1db26be44b7efba9b4dfebe8e5 Mon Sep 17 00:00:00 2001 From: "codeflash-ai[bot]" <148906541+codeflash-ai[bot]@users.noreply.github.com> Date: Thu, 19 Feb 2026 11:06:54 +0000 Subject: [PATCH 1/2] Optimize _filter_new_declarations MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit The optimization achieves a **12% runtime improvement** (783μs → 698μs) by replacing the lambda function in the sorting key with `operator.attrgetter("start_line")` and eliminating an unnecessary intermediate variable. **Key optimization:** Using `attrgetter("start_line")` instead of `lambda d: d.start_line` reduces per-item function call overhead during sorting. In Python, lambda functions incur dispatch costs for each element, while `attrgetter` is implemented in C and provides faster attribute access. The line profiler shows the sorting operation dropped from 967μs (24.7% of runtime) to 756μs (22.5%), a reduction of ~22% in sorting time alone. **Why this works:** - `attrgetter` is a specialized built-in operator that bypasses Python's function call mechanism - Removing the `sorted_declarations` intermediate variable eliminates a list reference assignment and slightly reduces memory allocation overhead - The optimization directly iterates over `sorted()`, which is more idiomatic and marginally more efficient **Performance characteristics from tests:** - **Small inputs (< 10 items):** Minor slowdown of 1-15% due to the overhead of importing `attrgetter` outweighing benefits - **Large inputs (100-1000 items):** Significant speedup of 8-27%, with the best gains (27.7%) on the 1000-item test case where sorting overhead dominates - The optimization particularly excels when filtering many declarations with duplicate sources, as seen in tests with 100-500 items showing 6-26% improvements This is a textbook micro-optimization that pays dividends in hot paths processing many declarations, trading a negligible cost on trivial inputs for substantial gains on realistic workloads. --- codeflash/languages/javascript/code_replacer.py | 5 ++--- 1 file changed, 2 insertions(+), 3 deletions(-) diff --git a/codeflash/languages/javascript/code_replacer.py b/codeflash/languages/javascript/code_replacer.py index 83c96ec6a..01c58be74 100644 --- a/codeflash/languages/javascript/code_replacer.py +++ b/codeflash/languages/javascript/code_replacer.py @@ -5,6 +5,7 @@ from typing import TYPE_CHECKING from codeflash.cli_cmds.console import logger +from operator import attrgetter if TYPE_CHECKING: from pathlib import Path @@ -103,9 +104,7 @@ def _filter_new_declarations(optimized_declarations: list, existing_names: set[s seen_sources: set[str] = set() # Sort by line number to maintain order from optimized code - sorted_declarations = sorted(optimized_declarations, key=lambda d: d.start_line) - - for decl in sorted_declarations: + for decl in sorted(optimized_declarations, key=attrgetter("start_line")): if decl.name not in existing_names and decl.source_code not in seen_sources: new_declarations.append(decl) seen_sources.add(decl.source_code) From 0250d59ed27ef6885fc27ab33089c8a6500c4c2c Mon Sep 17 00:00:00 2001 From: "claude[bot]" <41898282+claude[bot]@users.noreply.github.com> Date: Thu, 19 Feb 2026 11:09:09 +0000 Subject: [PATCH 2/2] style: auto-fix linting issues --- codeflash/languages/javascript/code_replacer.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/codeflash/languages/javascript/code_replacer.py b/codeflash/languages/javascript/code_replacer.py index 01c58be74..a3dd09c28 100644 --- a/codeflash/languages/javascript/code_replacer.py +++ b/codeflash/languages/javascript/code_replacer.py @@ -2,10 +2,10 @@ from __future__ import annotations +from operator import attrgetter from typing import TYPE_CHECKING from codeflash.cli_cmds.console import logger -from operator import attrgetter if TYPE_CHECKING: from pathlib import Path