-
Notifications
You must be signed in to change notification settings - Fork 22
mirror the args and test_cfg paths for worktree #792
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Merged
Merged
Changes from 4 commits
Commits
Show all changes
6 commits
Select commit
Hold shift + click to select a range
10b89fa
mirror the args in worktree
mohammedahmed18 85a8477
re-naming
mohammedahmed18 f400432
keep path type
mohammedahmed18 70d648f
tests
mohammedahmed18 7882edc
also nested tests dir
mohammedahmed18 6ed7a6c
keep tests dir
mohammedahmed18 File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
8 changes: 8 additions & 0 deletions
8
code_to_optimize/code_directories/nested_module_root/pyproject.toml
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| 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 = "tests" | ||
| test-framework = "pytest" | ||
| ignore-paths = [] | ||
| disable-telemetry = true | ||
| formatter-cmds = ["disabled"] |
10 changes: 10 additions & 0 deletions
10
code_to_optimize/code_directories/nested_module_root/src/app/main.py
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| 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 |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| 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 | ||
| assert optimizer.args.module_root == worktree_dir / "src" / "app" | ||
| assert optimizer.args.tests_root == worktree_dir / "tests" | ||
| assert optimizer.args.file == worktree_dir / "src" / "app" / "main.py" | ||
|
|
||
| assert optimizer.test_cfg.tests_root == worktree_dir / "tests" | ||
| assert optimizer.test_cfg.project_root_path == worktree_dir / "src" # same as project_root | ||
| assert optimizer.test_cfg.tests_project_rootdir == worktree_dir # 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 |
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
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_pathincodeflash/optimization/optimizer.py⏱️ Runtime :
14.2 milliseconds→10.1 milliseconds(best of90runs)📝 Explanation and details
The optimization replaces
Path.relative_to()with direct manipulation of path components using.parts, resulting in a 40% speedup.Key optimizations:
Eliminated expensive
relative_to()method: The original code callspath.relative_to(src_root), which internally creates a new Path object and performs complex validation logic. The optimized version directly accesses.partstuples and uses simple tuple slicing.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.Efficient path reconstruction: Uses
joinpath(*relative_parts)with tuple unpacking rather than the/operator on Path objects, reducing intermediate Path object allocations.Performance characteristics:
relative_to()'s overheadThe optimization maintains identical behavior including proper ValueError handling for invalid paths, making it a drop-in performance improvement.
✅ Correctness verification report:
🌀 Generated Regression Tests and Runtime
To test or edit this optimization locally
git merge codeflash/optimize-pr792-2025-10-04T20.20.00