Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
[tool.codeflash]
# All paths are relative to this pyproject.toml's directory.
module-root = "src/app"
tests-root = "src/tests"
test-framework = "pytest"
ignore-paths = []
disable-telemetry = true
formatter-cmds = ["disabled"]
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
def sorter(arr):
print("codeflash stdout: Sorting list")
for i in range(len(arr)):
for j in range(len(arr) - 1):
if arr[j] > arr[j + 1]:
temp = arr[j]
arr[j] = arr[j + 1]
arr[j + 1] = temp
print(f"result: {arr}")
return arr
Empty file.
9 changes: 3 additions & 6 deletions codeflash/cli_cmds/cli.py
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,6 @@
from codeflash.code_utils import env_utils
from codeflash.code_utils.code_utils import exit_with_message
from codeflash.code_utils.config_parser import parse_config_file
from codeflash.code_utils.git_utils import git_root_dir
from codeflash.lsp.helpers import is_LSP_enabled
from codeflash.version import __version__ as version

Expand Down Expand Up @@ -223,20 +222,18 @@ def process_pyproject_config(args: Namespace) -> Namespace:
args.module_root = Path(args.module_root).resolve()
# If module-root is "." then all imports are relatives to it.
# in this case, the ".." becomes outside project scope, causing issues with un-importable paths
args.project_root = project_root_from_module_root(args.module_root, pyproject_file_path, args.worktree)
args.project_root = project_root_from_module_root(args.module_root, pyproject_file_path)
args.tests_root = Path(args.tests_root).resolve()
if args.benchmarks_root:
args.benchmarks_root = Path(args.benchmarks_root).resolve()
args.test_project_root = project_root_from_module_root(args.tests_root, pyproject_file_path, args.worktree)
args.test_project_root = project_root_from_module_root(args.tests_root, pyproject_file_path)
if is_LSP_enabled():
args.all = None
return args
return handle_optimize_all_arg_parsing(args)


def project_root_from_module_root(module_root: Path, pyproject_file_path: Path, in_worktree: bool = False) -> Path: # noqa: FBT001, FBT002
if in_worktree:
return git_root_dir()
def project_root_from_module_root(module_root: Path, pyproject_file_path: Path) -> Path:
if pyproject_file_path.parent == module_root:
return module_root
return module_root.parent.resolve()
Expand Down
7 changes: 1 addition & 6 deletions codeflash/lsp/beta.py
Original file line number Diff line number Diff line change
Expand Up @@ -115,24 +115,19 @@ def get_optimizable_functions(
server: CodeflashLanguageServer, params: OptimizableFunctionsParams
) -> dict[str, list[str]]:
file_path = Path(uris.to_fs_path(params.textDocument.uri))
server.show_message_log(f"Getting optimizable functions for: {file_path}", "Info")
if not server.optimizer:
return {"status": "error", "message": "optimizer not initialized"}

server.optimizer.args.file = file_path
server.optimizer.args.function = None # Always get ALL functions, not just one
server.optimizer.args.previous_checkpoint_functions = False

server.show_message_log(f"Calling get_optimizable_functions for {server.optimizer.args.file}...", "Info")
optimizable_funcs, _, _ = server.optimizer.get_optimizable_functions()

path_to_qualified_names = {}
for functions in optimizable_funcs.values():
path_to_qualified_names[file_path] = [func.qualified_name for func in functions]

server.show_message_log(
f"Found {len(path_to_qualified_names)} files with functions: {path_to_qualified_names}", "Info"
)
return path_to_qualified_names


Expand Down Expand Up @@ -177,7 +172,7 @@ def init_project(server: CodeflashLanguageServer, params: ValidateProjectParams)
else:
return {"status": "error", "message": "No pyproject.toml found in workspace."}

# since we are using worktrees, optimization diffs are generated with respect to the root of the repo, also the args.project_root is set to the root of the repo when creating a worktree
# since we are using worktrees, optimization diffs are generated with respect to the root of the repo.
root = str(git_root_dir())

if getattr(params, "skip_validation", False):
Expand Down
69 changes: 40 additions & 29 deletions codeflash/optimization/optimizer.py
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@
from codeflash.code_utils import env_utils
from codeflash.code_utils.code_utils import cleanup_paths, get_run_tmp_file
from codeflash.code_utils.env_utils import get_pr_number, is_pr_draft
from codeflash.code_utils.git_utils import check_running_in_git_repo
from codeflash.code_utils.git_utils import check_running_in_git_repo, git_root_dir
from codeflash.code_utils.git_worktree_utils import (
create_detached_worktree,
create_diff_patch_from_worktree,
Expand Down Expand Up @@ -442,39 +442,50 @@ def worktree_mode(self) -> None:
logger.warning("Failed to create worktree. Skipping optimization.")
return
self.current_worktree = worktree_dir
self.mutate_args_for_worktree_mode(worktree_dir)
self.mirror_paths_for_worktree_mode(worktree_dir)
# make sure the tests dir is created in the worktree, this can happen if the original tests dir is empty
Path(self.args.tests_root).mkdir(parents=True, exist_ok=True)

def mutate_args_for_worktree_mode(self, worktree_dir: Path) -> None:
saved_args = copy.deepcopy(self.args)
saved_test_cfg = copy.deepcopy(self.test_cfg)
self.original_args_and_test_cfg = (saved_args, saved_test_cfg)

project_root = self.args.project_root
module_root = self.args.module_root
relative_module_root = module_root.relative_to(project_root)
relative_optimized_file = self.args.file.relative_to(project_root) if self.args.file else None
relative_tests_root = self.test_cfg.tests_root.relative_to(project_root)
relative_benchmarks_root = (
self.args.benchmarks_root.relative_to(project_root) if self.args.benchmarks_root else None
def mirror_paths_for_worktree_mode(self, worktree_dir: Path) -> None:
original_args = copy.deepcopy(self.args)
original_test_cfg = copy.deepcopy(self.test_cfg)
self.original_args_and_test_cfg = (original_args, original_test_cfg)

original_git_root = git_root_dir()

# mirror project_root
self.args.project_root = mirror_path(self.args.project_root, original_git_root, worktree_dir)
self.test_cfg.project_root_path = mirror_path(self.test_cfg.project_root_path, original_git_root, worktree_dir)

# mirror module_root
self.args.module_root = mirror_path(self.args.module_root, original_git_root, worktree_dir)

# mirror target file
if self.args.file:
self.args.file = mirror_path(self.args.file, original_git_root, worktree_dir)

# mirror tests root
self.args.tests_root = mirror_path(self.args.tests_root, original_git_root, worktree_dir)
self.test_cfg.tests_root = mirror_path(self.test_cfg.tests_root, original_git_root, worktree_dir)

# mirror tests project root
self.args.test_project_root = mirror_path(self.args.test_project_root, original_git_root, worktree_dir)
self.test_cfg.tests_project_rootdir = mirror_path(
self.test_cfg.tests_project_rootdir, original_git_root, worktree_dir
)

self.args.module_root = worktree_dir / relative_module_root
self.args.project_root = worktree_dir
self.args.test_project_root = worktree_dir
self.args.tests_root = worktree_dir / relative_tests_root
if relative_benchmarks_root:
self.args.benchmarks_root = worktree_dir / relative_benchmarks_root

self.test_cfg.project_root_path = worktree_dir
self.test_cfg.tests_project_rootdir = worktree_dir
self.test_cfg.tests_root = worktree_dir / relative_tests_root
if relative_benchmarks_root:
self.test_cfg.benchmark_tests_root = worktree_dir / relative_benchmarks_root

if relative_optimized_file is not None:
self.args.file = worktree_dir / relative_optimized_file
# mirror benchmarks root paths
if self.args.benchmarks_root:
self.args.benchmarks_root = mirror_path(self.args.benchmarks_root, original_git_root, worktree_dir)
if self.test_cfg.benchmark_tests_root:
self.test_cfg.benchmark_tests_root = mirror_path(
self.test_cfg.benchmark_tests_root, original_git_root, worktree_dir
)


def mirror_path(path: Path, src_root: Path, dest_root: Path) -> Path:
relative_path = path.relative_to(src_root)
return dest_root / relative_path
Comment on lines +487 to +488
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

⚡️Codeflash found 41% (0.41x) speedup for mirror_path in codeflash/optimization/optimizer.py

⏱️ Runtime : 14.2 milliseconds 10.1 milliseconds (best of 90 runs)

📝 Explanation and details

The optimization replaces Path.relative_to() with direct manipulation of path components using .parts, resulting in a 40% speedup.

Key optimizations:

  1. Eliminated expensive relative_to() method: The original code calls path.relative_to(src_root), which internally creates a new Path object and performs complex validation logic. The optimized version directly accesses .parts tuples and uses simple tuple slicing.

  2. Direct tuple comparison: Instead of Path's internal prefix checking, the code compares path_parts[:len(src_parts)] != src_parts - a fast tuple slice comparison that avoids object creation.

  3. Efficient path reconstruction: Uses joinpath(*relative_parts) with tuple unpacking rather than the / operator on Path objects, reducing intermediate Path object allocations.

Performance characteristics:

  • Best gains (40-98% faster): Cases with simple path structures, root mirroring, and paths with many components benefit most from avoiding relative_to()'s overhead
  • Slight slowdowns (6-29% slower): Very deeply nested paths (100+ levels) or complex relative paths where tuple operations become less efficient than Path's optimized internal handling
  • Consistent improvements: Most real-world use cases see 10-50% speedups, especially beneficial for batch operations processing thousands of files

The optimization maintains identical behavior including proper ValueError handling for invalid paths, making it a drop-in performance improvement.

Correctness verification report:

Test Status
⚙️ Existing Unit Tests 🔘 None Found
🌀 Generated Regression Tests 4037 Passed
⏪ Replay Tests 🔘 None Found
🔎 Concolic Coverage Tests 🔘 None Found
📊 Tests Coverage 100.0%
🌀 Generated Regression Tests and Runtime
from __future__ import annotations

from pathlib import Path

# imports
import pytest  # used for our unit tests
from codeflash.optimization.optimizer import mirror_path

# unit tests

# ----------------------
# 1. Basic Test Cases
# ----------------------

def test_basic_file_mirroring():
    # Simple file mirroring
    src_root = Path("/home/user/source")
    dest_root = Path("/mnt/backup/dest")
    file_path = src_root / "docs/readme.txt"
    codeflash_output = mirror_path(file_path, src_root, dest_root); mirrored = codeflash_output # 8.16μs -> 7.16μs (13.9% faster)

def test_basic_directory_mirroring():
    # Directory mirroring
    src_root = Path("/data/src")
    dest_root = Path("/data/dest")
    dir_path = src_root / "images"
    codeflash_output = mirror_path(dir_path, src_root, dest_root); mirrored = codeflash_output # 7.43μs -> 5.65μs (31.5% faster)

def test_basic_root_mirroring():
    # Mirroring the root itself
    src_root = Path("/srcroot")
    dest_root = Path("/destroot")
    codeflash_output = mirror_path(src_root, src_root, dest_root); mirrored = codeflash_output # 6.97μs -> 4.90μs (42.1% faster)

def test_basic_nested_mirroring():
    # Nested file path
    src_root = Path("/a/b")
    dest_root = Path("/x/y")
    file_path = src_root / "c/d/e.txt"
    codeflash_output = mirror_path(file_path, src_root, dest_root); mirrored = codeflash_output # 7.81μs -> 7.13μs (9.51% faster)

# ----------------------
# 2. Edge Test Cases
# ----------------------

def test_path_not_under_src_root_raises():
    # Path not under src_root should raise ValueError
    src_root = Path("/home/user/source")
    dest_root = Path("/mnt/backup/dest")
    file_path = Path("/home/user/otherdir/file.txt")
    with pytest.raises(ValueError):
        mirror_path(file_path, src_root, dest_root) # 12.4μs -> 11.5μs (8.24% faster)

def test_src_root_is_relative():
    # Relative src_root
    src_root = Path("relative/src")
    dest_root = Path("relative/dest")
    file_path = src_root / "file.txt"
    codeflash_output = mirror_path(file_path, src_root, dest_root); mirrored = codeflash_output # 5.86μs -> 5.40μs (8.45% faster)

def test_dest_root_is_relative():
    # Relative dest_root
    src_root = Path("/absolute/src")
    dest_root = Path("relative/dest")
    file_path = src_root / "file.txt"
    codeflash_output = mirror_path(file_path, src_root, dest_root); mirrored = codeflash_output # 6.90μs -> 5.29μs (30.5% faster)

def test_path_is_relative():
    # Relative path
    src_root = Path("src")
    dest_root = Path("dest")
    file_path = src_root / "subdir/file.txt"
    codeflash_output = mirror_path(file_path, src_root, dest_root); mirrored = codeflash_output # 5.75μs -> 6.16μs (6.55% slower)

def test_src_root_trailing_slash():
    # src_root with trailing slash
    src_root = Path("/foo/bar/")
    dest_root = Path("/baz/qux")
    file_path = Path("/foo/bar/file.txt")
    codeflash_output = mirror_path(file_path, src_root, dest_root); mirrored = codeflash_output # 7.53μs -> 5.77μs (30.4% faster)

def test_path_is_symlink():
    # Path is a symlink (just as a string, not checking filesystem)
    src_root = Path("/src")
    dest_root = Path("/dest")
    file_path = src_root / "link"
    codeflash_output = mirror_path(file_path, src_root, dest_root); mirrored = codeflash_output # 6.63μs -> 5.12μs (29.4% faster)

def test_src_root_is_dot():
    # src_root is "."
    src_root = Path(".")
    dest_root = Path("/backup")
    file_path = src_root / "foo/bar.txt"
    codeflash_output = mirror_path(file_path, src_root, dest_root); mirrored = codeflash_output # 5.16μs -> 5.97μs (13.6% slower)

def test_path_equals_src_root():
    # path == src_root
    src_root = Path("/abc/def")
    dest_root = Path("/xyz/uvw")
    codeflash_output = mirror_path(src_root, src_root, dest_root); mirrored = codeflash_output # 7.09μs -> 4.51μs (57.0% faster)

def test_path_is_empty_relative_to_src_root():
    # path is src_root, so relative path is empty
    src_root = Path("/a")
    dest_root = Path("/b")
    codeflash_output = mirror_path(src_root, src_root, dest_root); mirrored = codeflash_output # 6.66μs -> 4.42μs (50.8% faster)

def test_path_is_dot_relative_to_src_root():
    # path is "./", src_root is "."
    src_root = Path(".")
    dest_root = Path("/backup")
    file_path = Path(".")
    codeflash_output = mirror_path(file_path, src_root, dest_root); mirrored = codeflash_output # 4.39μs -> 4.76μs (7.76% slower)

def test_path_is_subdir_of_src_root():
    # path is an immediate subdir of src_root
    src_root = Path("/data")
    dest_root = Path("/mirror")
    file_path = src_root / "subdir"
    codeflash_output = mirror_path(file_path, src_root, dest_root); mirrored = codeflash_output # 6.28μs -> 5.26μs (19.4% faster)

def test_path_has_multiple_slashes():
    # path with multiple slashes
    src_root = Path("/foo//bar")
    dest_root = Path("/baz")
    file_path = Path("/foo/bar//file.txt")
    codeflash_output = mirror_path(file_path, src_root, dest_root); mirrored = codeflash_output # 7.30μs -> 5.82μs (25.5% faster)

def test_path_is_not_subpath_of_src_root():
    # path is not a subpath of src_root (should raise)
    src_root = Path("/foo/bar")
    dest_root = Path("/baz")
    file_path = Path("/foo/bar2/file.txt")
    with pytest.raises(ValueError):
        mirror_path(file_path, src_root, dest_root) # 12.1μs -> 11.4μs (6.06% faster)

def test_path_is_empty_string():
    # path is empty string (should raise)
    src_root = Path("/foo")
    dest_root = Path("/bar")
    file_path = Path("")
    with pytest.raises(ValueError):
        mirror_path(file_path, src_root, dest_root) # 10.3μs -> 9.93μs (4.16% faster)

def test_src_root_is_empty_string():
    # src_root is empty string
    src_root = Path("")
    dest_root = Path("/bar")
    file_path = Path("foo.txt")
    codeflash_output = mirror_path(file_path, src_root, dest_root); mirrored = codeflash_output # 5.47μs -> 5.80μs (5.54% slower)

def test_dest_root_is_empty_string():
    # dest_root is empty string
    src_root = Path("/foo")
    dest_root = Path("")
    file_path = src_root / "bar.txt"
    codeflash_output = mirror_path(file_path, src_root, dest_root); mirrored = codeflash_output # 6.94μs -> 5.64μs (22.9% faster)

# ----------------------
# 3. Large Scale Test Cases
# ----------------------

def test_large_number_of_files():
    # Test mirroring for a large number of files
    src_root = Path("/src")
    dest_root = Path("/dest")
    for i in range(1000):
        file_path = src_root / f"folder_{i}/file_{i}.txt"
        codeflash_output = mirror_path(file_path, src_root, dest_root); mirrored = codeflash_output # 3.72ms -> 2.62ms (42.1% faster)

def test_deeply_nested_path():
    # Test with a deeply nested path
    src_root = Path("/deep")
    dest_root = Path("/mirror")
    nested = "a/" * 100 + "file.txt"
    file_path = src_root / nested
    codeflash_output = mirror_path(file_path, src_root, dest_root); mirrored = codeflash_output # 30.1μs -> 39.3μs (23.3% slower)

def test_large_src_and_dest_roots():
    # Large src_root and dest_root
    src_root = Path("/" + "/".join([f"src{i}" for i in range(20)]))
    dest_root = Path("/" + "/".join([f"dest{i}" for i in range(20)]))
    file_path = src_root / "file.txt"
    codeflash_output = mirror_path(file_path, src_root, dest_root); mirrored = codeflash_output # 11.2μs -> 5.66μs (97.9% faster)

def test_large_file_name():
    # File with a very long name
    src_root = Path("/src")
    dest_root = Path("/dest")
    long_name = "a" * 255 + ".txt"
    file_path = src_root / long_name
    codeflash_output = mirror_path(file_path, src_root, dest_root); mirrored = codeflash_output # 6.11μs -> 4.96μs (23.2% faster)

def test_large_number_of_subdirs():
    # Path with many subdirectories
    src_root = Path("/src")
    dest_root = Path("/dest")
    subdirs = "/".join([f"subdir{i}" for i in range(50)])
    file_path = src_root / subdirs / "file.txt"
    codeflash_output = mirror_path(file_path, src_root, dest_root); mirrored = codeflash_output # 17.3μs -> 21.9μs (20.9% slower)
# codeflash_output is used to check that the output of the original code is the same as that of the optimized code.
#------------------------------------------------
from __future__ import annotations

from pathlib import Path

# imports
import pytest  # used for our unit tests
from codeflash.optimization.optimizer import mirror_path

# unit tests

# ----------------
# BASIC TEST CASES
# ----------------

def test_basic_file_mirroring():
    # Simple file mirroring
    src = Path("/src_root/dir/file.txt")
    src_root = Path("/src_root")
    dest_root = Path("/dest_root")
    expected = Path("/dest_root/dir/file.txt")
    codeflash_output = mirror_path(src, src_root, dest_root) # 6.61μs -> 6.16μs (7.30% faster)

def test_basic_directory_mirroring():
    # Simple directory mirroring
    src = Path("/src_root/dir/subdir")
    src_root = Path("/src_root")
    dest_root = Path("/dest_root")
    expected = Path("/dest_root/dir/subdir")
    codeflash_output = mirror_path(src, src_root, dest_root) # 7.17μs -> 6.46μs (11.0% faster)

def test_basic_root_mirroring():
    # Mirroring the root itself
    src = Path("/src_root")
    src_root = Path("/src_root")
    dest_root = Path("/dest_root")
    expected = Path("/dest_root")
    codeflash_output = mirror_path(src, src_root, dest_root) # 6.42μs -> 5.09μs (26.2% faster)

def test_basic_nested_file_mirroring():
    # Mirroring a deeply nested file
    src = Path("/src_root/a/b/c/d/e.txt")
    src_root = Path("/src_root")
    dest_root = Path("/dest_root")
    expected = Path("/dest_root/a/b/c/d/e.txt")
    codeflash_output = mirror_path(src, src_root, dest_root) # 8.15μs -> 7.86μs (3.64% faster)

# ----------------
# EDGE TEST CASES
# ----------------


def test_path_not_under_src_root():
    # path is not under src_root (should raise ValueError)
    src = Path("/other_root/file.txt")
    src_root = Path("/src_root")
    dest_root = Path("/dest_root")
    with pytest.raises(ValueError):
        mirror_path(src, src_root, dest_root) # 11.9μs -> 11.6μs (2.48% faster)

def test_src_and_dest_are_same():
    # src_root and dest_root are the same
    src = Path("/root/dir/file.txt")
    src_root = Path("/root")
    dest_root = Path("/root")
    expected = Path("/root/dir/file.txt")
    codeflash_output = mirror_path(src, src_root, dest_root) # 8.34μs -> 7.06μs (18.1% faster)

def test_relative_paths():
    # Using relative paths
    src = Path("src_root/dir/file.txt")
    src_root = Path("src_root")
    dest_root = Path("dest_root")
    expected = Path("dest_root/dir/file.txt")
    codeflash_output = mirror_path(src, src_root, dest_root) # 6.54μs -> 6.29μs (4.04% faster)

def test_dot_paths():
    # Using '.' and '..' in paths
    src = Path("/src_root/dir/../dir2/file.txt")
    src_root = Path("/src_root")
    dest_root = Path("/dest_root")
    # Pathlib resolves '..' automatically
    expected = Path("/dest_root/dir2/file.txt")
    codeflash_output = mirror_path(src.resolve(), src_root, dest_root) # 8.02μs -> 6.59μs (21.5% faster)

def test_trailing_slash():
    # src_root and dest_root with trailing slashes
    src = Path("/src_root/dir/file.txt")
    src_root = Path("/src_root/")
    dest_root = Path("/dest_root/")
    expected = Path("/dest_root/dir/file.txt")
    codeflash_output = mirror_path(src, src_root, dest_root) # 7.50μs -> 6.32μs (18.8% faster)

def test_empty_relative_path():
    # Path is exactly src_root, so relative_path is empty
    src = Path("/src_root")
    src_root = Path("/src_root")
    dest_root = Path("/dest_root")
    expected = Path("/dest_root")
    codeflash_output = mirror_path(src, src_root, dest_root) # 6.77μs -> 5.24μs (29.3% faster)


def test_case_sensitivity():
    # Case sensitivity (should fail on case mismatch)
    src = Path("/SRC_ROOT/dir/file.txt")
    src_root = Path("/src_root")
    dest_root = Path("/dest_root")
    with pytest.raises(ValueError):
        mirror_path(src, src_root, dest_root) # 12.5μs -> 11.8μs (6.17% faster)

def test_symlink_path():
    # Symlinked path (should work as long as path is under src_root)
    src = Path("/src_root/dir/link_to_file.txt")
    src_root = Path("/src_root")
    dest_root = Path("/dest_root")
    expected = Path("/dest_root/dir/link_to_file.txt")
    codeflash_output = mirror_path(src, src_root, dest_root) # 8.68μs -> 7.58μs (14.6% faster)

# ----------------------
# LARGE SCALE TEST CASES
# ----------------------

def test_large_number_of_files():
    # Large number of files in a flat directory
    src_root = Path("/src_root")
    dest_root = Path("/dest_root")
    for i in range(1000):
        src = src_root / f"file_{i}.txt"
        expected = dest_root / f"file_{i}.txt"
        codeflash_output = mirror_path(src, src_root, dest_root) # 3.51ms -> 2.28ms (53.7% faster)

def test_large_nested_structure():
    # Large nested directory structure
    src_root = Path("/src_root")
    dest_root = Path("/dest_root")
    # Create a path with 1000 nested directories
    nested_dirs = [f"dir_{i}" for i in range(1000)]
    src = src_root.joinpath(*nested_dirs, "file.txt")
    expected = dest_root.joinpath(*nested_dirs, "file.txt")
    codeflash_output = mirror_path(src, src_root, dest_root) # 211μs -> 299μs (29.4% slower)

def test_large_relative_paths():
    # Large number of relative paths
    src_root = Path("src_root")
    dest_root = Path("dest_root")
    for i in range(1000):
        src = Path("src_root") / f"file_{i}.txt"
        expected = Path("dest_root") / f"file_{i}.txt"
        codeflash_output = mirror_path(src, src_root, dest_root) # 2.92ms -> 2.30ms (27.3% faster)

def test_large_mixed_paths():
    # Mix of absolute and relative paths
    src_root = Path("/src_root")
    dest_root = Path("dest_root")
    for i in range(1000):
        src = Path("/src_root") / f"file_{i}.txt"
        expected = Path("dest_root") / f"file_{i}.txt"
        codeflash_output = mirror_path(src, src_root, dest_root) # 3.51ms -> 2.31ms (51.9% faster)

To test or edit this optimization locally git merge codeflash/optimize-pr792-2025-10-04T20.20.00

Suggested change
relative_path = path.relative_to(src_root)
return dest_root / relative_path
path_parts = path.parts
src_parts = src_root.parts
if path_parts[: len(src_parts)] != src_parts:
raise ValueError(f"{path!r} does not start with {src_root!r}")
relative_parts = path_parts[len(src_parts) :]
return dest_root.joinpath(*relative_parts)



def run_with_args(args: Namespace) -> None:
Expand Down
65 changes: 65 additions & 0 deletions tests/test_worktree.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,65 @@
from argparse import Namespace
from pathlib import Path

import pytest
from codeflash.cli_cmds.cli import process_pyproject_config
from codeflash.optimization.optimizer import Optimizer


def test_mirror_paths_for_worktree_mode(monkeypatch: pytest.MonkeyPatch):
repo_root = Path(__file__).resolve().parent.parent
project_root = repo_root / "code_to_optimize" / "code_directories" / "nested_module_root"

monkeypatch.setattr("codeflash.optimization.optimizer.git_root_dir", lambda: project_root)

args = Namespace()
args.benchmark = False
args.benchmarks_root = None

args.config_file = project_root / "pyproject.toml"
args.file = project_root / "src" / "app" / "main.py"
args.worktree = True

new_args = process_pyproject_config(args)

optimizer = Optimizer(new_args)

worktree_dir = repo_root / "worktree"
optimizer.mirror_paths_for_worktree_mode(worktree_dir)

assert optimizer.args.project_root == worktree_dir / "src"
assert optimizer.args.test_project_root == worktree_dir / "src"
assert optimizer.args.module_root == worktree_dir / "src" / "app"
assert optimizer.args.tests_root == worktree_dir / "src" / "tests"
assert optimizer.args.file == worktree_dir / "src" / "app" / "main.py"

assert optimizer.test_cfg.tests_root == worktree_dir / "src" / "tests"
assert optimizer.test_cfg.project_root_path == worktree_dir / "src" # same as project_root
assert optimizer.test_cfg.tests_project_rootdir == worktree_dir / "src" # same as test_project_root

# test on our repo
monkeypatch.setattr("codeflash.optimization.optimizer.git_root_dir", lambda: repo_root)
args = Namespace()
args.benchmark = False
args.benchmarks_root = None

args.config_file = repo_root / "pyproject.toml"
args.file = repo_root / "codeflash/optimization/optimizer.py"
args.worktree = True

new_args = process_pyproject_config(args)

optimizer = Optimizer(new_args)

worktree_dir = repo_root / "worktree"
optimizer.mirror_paths_for_worktree_mode(worktree_dir)

assert optimizer.args.project_root == worktree_dir
assert optimizer.args.test_project_root == worktree_dir
assert optimizer.args.module_root == worktree_dir / "codeflash"
assert optimizer.args.tests_root == worktree_dir / "tests"
assert optimizer.args.file == worktree_dir / "codeflash/optimization/optimizer.py"

assert optimizer.test_cfg.tests_root == worktree_dir / "tests"
assert optimizer.test_cfg.project_root_path == worktree_dir # same as project_root
assert optimizer.test_cfg.tests_project_rootdir == worktree_dir # same as test_project_root
Loading