Skip to content

Commit e504c87

Browse files
split & apply
1 parent 4385b8e commit e504c87

File tree

2 files changed

+25
-3
lines changed

2 files changed

+25
-3
lines changed

codeflash/models/models.py

Lines changed: 18 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -139,8 +139,11 @@ class CodeString(BaseModel):
139139
file_path: Optional[Path] = None
140140

141141

142+
SPLITTER_MARKER = "# codeflash-splitter__"
143+
144+
142145
def get_code_block_splitter(file_path: Path) -> str:
143-
return f"# codeflash-splitter__{file_path}"
146+
return f"{SPLITTER_MARKER}{file_path}"
144147

145148

146149
class CodeStringsMarkdown(BaseModel):
@@ -166,6 +169,20 @@ def markdown(self) -> str:
166169
]
167170
)
168171

172+
@staticmethod
173+
def from_str_with_markers(code_with_markers: str) -> list[CodeString]:
174+
pattern = rf"{SPLITTER_MARKER}([^\n]+)\n"
175+
matches = list(re.finditer(pattern, code_with_markers))
176+
177+
results = []
178+
for i, match in enumerate(matches):
179+
start = match.end()
180+
end = matches[i + 1].start() if i + 1 < len(matches) else len(code_with_markers)
181+
file_path = match.group(1).strip()
182+
code = code_with_markers[start:end].lstrip("\n")
183+
results.append(CodeString(file_path=file_path, code=code))
184+
return results
185+
169186

170187
class CodeOptimizationContext(BaseModel):
171188
testgen_context_code: str = ""

codeflash/optimization/function_optimizer.py

Lines changed: 7 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -62,6 +62,7 @@
6262
from codeflash.models.models import (
6363
BestOptimization,
6464
CodeOptimizationContext,
65+
CodeStringsMarkdown,
6566
GeneratedTests,
6667
GeneratedTestsList,
6768
OptimizationSet,
@@ -93,7 +94,6 @@
9394
from codeflash.either import Result
9495
from codeflash.models.models import (
9596
BenchmarkKey,
96-
CodeStringsMarkdown,
9797
CoverageData,
9898
FunctionCalledInTest,
9999
FunctionSource,
@@ -621,13 +621,18 @@ def replace_function_and_helpers_with_optimized_code(
621621
read_writable_functions_by_file_path[self.function_to_optimize.file_path].add(
622622
self.function_to_optimize.qualified_name
623623
)
624+
code_strings = CodeStringsMarkdown.from_str_with_markers(optimized_code)
625+
optimized_code_dict = {code_string.file_path: code_string.code for code_string in code_strings}
626+
logger.debug(f"Optimized code: {optimized_code_dict}")
624627
for helper_function in code_context.helper_functions:
625628
if helper_function.jedi_definition.type != "class":
626629
read_writable_functions_by_file_path[helper_function.file_path].add(helper_function.qualified_name)
627630
for module_abspath, qualified_names in read_writable_functions_by_file_path.items():
631+
relative_module_path = module_abspath.relative_to(self.project_root)
632+
logger.debug(f"applying optimized code to: {relative_module_path}")
628633
did_update |= replace_function_definitions_in_module(
629634
function_names=list(qualified_names),
630-
optimized_code=optimized_code,
635+
optimized_code=optimized_code_dict.get(relative_module_path),
631636
module_abspath=module_abspath,
632637
preexisting_objects=code_context.preexisting_objects,
633638
project_root_path=self.project_root,

0 commit comments

Comments
 (0)