Skip to content

Conversation

@aseembits93
Copy link
Contributor

@aseembits93 aseembits93 commented Aug 14, 2025

User description

wip


PR Type

Enhancement


Description

  • Refactor candidate loop for async profiling and refinement

  • Introduce line_profiler_done and refinement_done flags

  • Add waiting logic for profiling and refinements

  • Update optimization log message for context


Diagram Walkthrough

flowchart LR
  A["Start processing candidates"] --> B["Process next candidate"]
  B --> C{"candidates empty?"}
  C -- "no" --> B
  C -- "yes & profiling not done" --> D["Wait for line profiler"]
  D --> E["Add profiling results"]
  E --> B
  C -- "profiling done & refinement not done" --> F["Wait for refinements"]
  F --> G["Add refinement results"]
  G --> B
  C -- "profiling & refinement done" --> H["Exit loop"]
Loading

File Walkthrough

Relevant files
Enhancement
aiservice.py
Improve optimization log detail                                                   

codeflash/api/aiservice.py

  • Enhanced log in optimize method
  • Included line profiler context in message
+1/-1     
function_optimizer.py
Refactor candidate processing loop                                             

codeflash/optimization/function_optimizer.py

  • Refactored processing into while True loop
  • Added line_profiler_done and refinement_done flags
  • Added wait logic for async profiling and refinements
  • Removed duplicate post-loop candidate handling
+45/-46 

@github-actions
Copy link

PR Reviewer Guide 🔍

Here are some key observations to aid the review process:

⏱️ Estimated effort to review: 4 🔵🔵🔵🔵⚪
🧪 No relevant tests
🔒 No security concerns identified
⚡ Recommended focus areas for review

Deadlock Risk

Waiting indefinitely on futures without a timeout can hang the candidate loop if a future never completes.

concurrent.futures.wait([future_line_profile_results])
line_profile_results = future_line_profile_results.result()
Loop Logic Error

Breaking out of the while True loop may skip candidate processing and reference an undefined candidate in subsequent code.

if line_profiler_done and refinement_done:
    logger.debug("everything done, exiting")
    break

@github-actions
Copy link

PR Code Suggestions ✨

Explore these optional code suggestions:

CategorySuggestion                                                                                                                                    Impact
Possible issue
Check for None before waiting on future

Guard against a missing or already-consumed future_line_profile_results to prevent
passing None into wait and avoid a runtime error. If the future is None, log a
warning and mark profiling as done.

codeflash/optimization/function_optimizer.py [413-423]

 if not line_profiler_done:
     logger.debug("all candidates processed, await candidates from line profiler")
-    concurrent.futures.wait([future_line_profile_results])
-    line_profile_results = future_line_profile_results.result()
-    candidates.extend(line_profile_results)
-    original_len += len(line_profile_results)
-    logger.info(
-        f"Added results from line profiler to candidates, total candidates now: {original_len}"
-    )
-    line_profiler_done = True
+    if future_line_profile_results is None:
+        logger.warning("No line profiler task available; marking done.")
+        line_profiler_done = True
+    else:
+        concurrent.futures.wait([future_line_profile_results])
+        line_profile_results = future_line_profile_results.result()
+        candidates.extend(line_profile_results)
+        original_len += len(line_profile_results)
+        logger.info(
+            f"Added results from line profiler to candidates, total candidates now: {original_len}"
+        )
+        # release reference
+        future_line_profile_results = None
+        line_profiler_done = True
     continue
Suggestion importance[1-10]: 6

__

Why: Guarding on future_line_profile_results prevents a possible None being passed to wait, which is a useful safety check in this async loop.

Low
Skip wait on empty refinement list

Ensure future_all_refinements is non-empty before calling wait to avoid instantly
returning or misbehavior when there are no refinement tasks. Log and mark refinement
as done if the list is empty.

codeflash/optimization/function_optimizer.py [424-437]

 if line_profiler_done and not refinement_done:
-    concurrent.futures.wait(future_all_refinements)
-    refinement_response = []
-    for future_refinement in future_all_refinements:
-        possible_refinement = future_refinement.result()
-        if len(possible_refinement) > 0:  # if the api returns a valid response
-            refinement_response.append(possible_refinement[0])
-    candidates.extend(refinement_response)
-    original_len += len(refinement_response)
-    logger.info(
-        f"Added {len(refinement_response)} candidates from refinement, total candidates now: {original_len}"
-    )
+    if future_all_refinements:
+        concurrent.futures.wait(future_all_refinements)
+        refinement_response = []
+        for future_refinement in future_all_refinements:
+            possible_refinement = future_refinement.result()
+            if possible_refinement:
+                refinement_response.append(possible_refinement[0])
+        candidates.extend(refinement_response)
+        original_len += len(refinement_response)
+        logger.info(
+            f"Added {len(refinement_response)} candidates from refinement, total candidates now: {original_len}"
+        )
+    else:
+        logger.debug("No refinement tasks to await; marking done.")
     refinement_done = True
     continue
Suggestion importance[1-10]: 5

__

Why: Checking future_all_refinements avoids unnecessary wait calls and ensures the loop proceeds correctly when there are no refinement tasks.

Low

@aseembits93 aseembits93 closed this pull request by merging all changes into main in 51c5e5a Aug 15, 2025
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants