Skip to content

Commit f29e33a

Browse files
author
Codeflash Bot
committed
Merge remote-tracking branch 'origin/main' into inspect-signature-issue
2 parents bab2752 + 1f6cf3f commit f29e33a

File tree

16 files changed

+460
-33
lines changed

16 files changed

+460
-33
lines changed

.github/workflows/unit-tests.yaml

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -28,8 +28,8 @@ jobs:
2828
- name: install dependencies
2929
run: uv sync
3030

31-
- name: Install test-only dependencies (Python 3.13)
32-
if: matrix.python-version == '3.13'
31+
- name: Install test-only dependencies (Python 3.9 and 3.13)
32+
if: matrix.python-version == '3.9' || matrix.python-version == '3.13'
3333
run: uv sync --group tests
3434

3535
- name: Unit tests

code_to_optimize/tests/pytest/test_topological_sort.py

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,7 @@ def test_topological_sort():
1010
g.addEdge(2, 3)
1111
g.addEdge(3, 1)
1212

13-
assert g.topologicalSort() == [5, 4, 2, 3, 1, 0]
13+
assert g.topologicalSort()[0] == [5, 4, 2, 3, 1, 0]
1414

1515

1616
def test_topological_sort_2():
@@ -20,15 +20,15 @@ def test_topological_sort_2():
2020
for j in range(i + 1, 10):
2121
g.addEdge(i, j)
2222

23-
assert g.topologicalSort() == [0, 1, 2, 3, 4, 5, 6, 7, 8, 9]
23+
assert g.topologicalSort()[0] == [0, 1, 2, 3, 4, 5, 6, 7, 8, 9]
2424

2525
g = Graph(10)
2626

2727
for i in range(10):
2828
for j in range(i + 1, 10):
2929
g.addEdge(i, j)
3030

31-
assert g.topologicalSort() == [0, 1, 2, 3, 4, 5, 6, 7, 8, 9]
31+
assert g.topologicalSort()[0] == [0, 1, 2, 3, 4, 5, 6, 7, 8, 9]
3232

3333

3434
def test_topological_sort_3():
@@ -38,4 +38,4 @@ def test_topological_sort_3():
3838
for j in range(i + 1, 1000):
3939
g.addEdge(j, i)
4040

41-
assert g.topologicalSort() == list(reversed(range(1000)))
41+
assert g.topologicalSort()[0] == list(reversed(range(1000)))

codeflash/api/aiservice.py

Lines changed: 14 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -255,6 +255,8 @@ def optimize_python_code_refinement(self, request: list[AIServiceRefinerRequest]
255255
"optimized_code_runtime": opt.optimized_code_runtime,
256256
"speedup": opt.speedup,
257257
"trace_id": opt.trace_id,
258+
"function_references": opt.function_references,
259+
"python_version": platform.python_version(),
258260
}
259261
for opt in request
260262
]
@@ -308,6 +310,7 @@ def get_new_explanation( # noqa: D417
308310
original_throughput: str | None = None,
309311
optimized_throughput: str | None = None,
310312
throughput_improvement: str | None = None,
313+
function_references: str | None = None,
311314
) -> str:
312315
"""Optimize the given python code for performance by making a request to the Django endpoint.
313316
@@ -327,6 +330,7 @@ def get_new_explanation( # noqa: D417
327330
- original_throughput: str | None - throughput for the baseline code (operations per second)
328331
- optimized_throughput: str | None - throughput for the optimized code (operations per second)
329332
- throughput_improvement: str | None - throughput improvement percentage
333+
- function_references: str | None - where the function is called in the codebase
330334
331335
Returns
332336
-------
@@ -349,6 +353,7 @@ def get_new_explanation( # noqa: D417
349353
"original_throughput": original_throughput,
350354
"optimized_throughput": optimized_throughput,
351355
"throughput_improvement": throughput_improvement,
356+
"function_references": function_references,
352357
}
353358
logger.info("loading|Generating explanation")
354359
console.rule()
@@ -373,7 +378,12 @@ def get_new_explanation( # noqa: D417
373378
return ""
374379

375380
def generate_ranking( # noqa: D417
376-
self, trace_id: str, diffs: list[str], optimization_ids: list[str], speedups: list[float]
381+
self,
382+
trace_id: str,
383+
diffs: list[str],
384+
optimization_ids: list[str],
385+
speedups: list[float],
386+
function_references: str | None = None,
377387
) -> list[int] | None:
378388
"""Optimize the given python code for performance by making a request to the Django endpoint.
379389
@@ -382,6 +392,7 @@ def generate_ranking( # noqa: D417
382392
- trace_id : unique uuid of function
383393
- diffs : list of unified diff strings of opt candidates
384394
- speedups : list of speedups of opt candidates
395+
- function_references : where the function is called in the codebase
385396
386397
Returns
387398
-------
@@ -394,6 +405,7 @@ def generate_ranking( # noqa: D417
394405
"speedups": speedups,
395406
"optimization_ids": optimization_ids,
396407
"python_version": platform.python_version(),
408+
"function_references": function_references,
397409
}
398410
logger.info("loading|Generating ranking")
399411
console.rule()
@@ -594,6 +606,7 @@ def get_optimization_review(
594606
"optimized_runtime": humanize_runtime(explanation.best_runtime_ns),
595607
"original_runtime": humanize_runtime(explanation.original_runtime_ns),
596608
"calling_fn_details": calling_fn_details,
609+
"python_version": platform.python_version(),
597610
}
598611
console.rule()
599612
try:

codeflash/api/cfapi.py

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,3 @@
1-
"""Module for interacting with the Codeflash API."""
2-
31
from __future__ import annotations
42

53
import json

codeflash/cli_cmds/cmd_init.py

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -264,7 +264,7 @@ def get_valid_subdirs(current_dir: Optional[Path] = None) -> list[str]:
264264
]
265265

266266

267-
def get_suggestions(section: str) -> tuple(list[str], Optional[str]):
267+
def get_suggestions(section: str) -> tuple[list[str], Optional[str]]:
268268
valid_subdirs = get_valid_subdirs()
269269
if section == CommonSections.module_root:
270270
return [d for d in valid_subdirs if d != "tests"], None
@@ -391,7 +391,7 @@ def collect_setup_info() -> CLISetupInfo:
391391
tests_root_answer = tests_answers["tests_root"]
392392

393393
if tests_root_answer == create_for_me_option:
394-
tests_root = Path(curdir) / default_tests_subdir
394+
tests_root = Path(curdir) / (default_tests_subdir or "tests")
395395
tests_root.mkdir()
396396
click.echo(f"✅ Created directory {tests_root}{os.path.sep}{LF}")
397397
elif tests_root_answer == custom_dir_option:

codeflash/code_utils/code_extractor.py

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
from __future__ import annotations
22

33
import ast
4+
import time
45
from dataclasses import dataclass
56
from itertools import chain
67
from pathlib import Path
@@ -1138,6 +1139,7 @@ def find_specific_function_in_file(
11381139
def get_fn_references_jedi(
11391140
source_code: str, file_path: Path, project_root: Path, target_function: str, target_class: str | None
11401141
) -> list[Path]:
1142+
start_time = time.perf_counter()
11411143
function_position: CodePosition = find_specific_function_in_file(
11421144
source_code, file_path, target_function, target_class
11431145
)
@@ -1146,6 +1148,8 @@ def get_fn_references_jedi(
11461148
# Get references to the function
11471149
references = script.get_references(line=function_position.line_no, column=function_position.col_no)
11481150
# Collect unique file paths where references are found
1151+
end_time = time.perf_counter()
1152+
logger.debug(f"Jedi for function references ran in {end_time - start_time:.2f} seconds")
11491153
reference_files = set()
11501154
for ref in references:
11511155
if ref.module_path:
@@ -1163,6 +1167,7 @@ def get_fn_references_jedi(
11631167
def get_opt_review_metrics(
11641168
source_code: str, file_path: Path, qualified_name: str, project_root: Path, tests_root: Path
11651169
) -> str:
1170+
start_time = time.perf_counter()
11661171
try:
11671172
qualified_name_split = qualified_name.rsplit(".", maxsplit=1)
11681173
if len(qualified_name_split) == 1:
@@ -1176,4 +1181,6 @@ def get_opt_review_metrics(
11761181
except Exception as e:
11771182
calling_fns_details = ""
11781183
logger.debug(f"Investigate {e}")
1184+
end_time = time.perf_counter()
1185+
logger.debug(f"Got function references in {end_time - start_time:.2f} seconds")
11791186
return calling_fns_details

codeflash/discovery/discover_unit_tests.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -284,7 +284,7 @@ def visit_Assign(self, node: ast.Assign) -> None:
284284
stack = [node]
285285
append = stack.append
286286
pop = stack.pop
287-
found_flag = self.found_any_target_function
287+
# found_flag = self.found_any_target_function
288288
while stack:
289289
current_node = pop()
290290
if self.found_any_target_function:

codeflash/models/models.py

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -44,6 +44,7 @@ class AIServiceRefinerRequest:
4444
trace_id: str
4545
original_line_profiler_results: str
4646
optimized_line_profiler_results: str
47+
function_references: str | None = None
4748

4849

4950
# If the method spam is in the class Ham, which is at the top level of the module eggs in the package foo, the fully

0 commit comments

Comments
 (0)