⚡️ Speed up method Optimizer.get_optimizable_functions by 14% in PR #1869 (fix/trigger_cc_on_multiple_commits)#1870
Conversation
The remapping branch previously called `mirror_path` once per file and `dataclasses.replace` once per function, with each iteration incurring a repeated `func.file_path.resolve()` cost inside `mirror_path`. The optimized code resolves `original_git_root` and `current_worktree` exactly once before the loop, then computes `relative_path` directly via `file_path.resolve().relative_to(original_root_resolved)` and constructs `new_path` with simple path arithmetic (`worktree_resolved / relative_path`). Because all functions in a given `funcs` list share the same `file_path`, the list comprehension now does `dataclasses.replace(func, file_path=new_path)` with a pre‐computed `new_path`, eliminating redundant resolutions inside each function's replacement. Line profiler shows the `mirror_path` call dropped from 226 µs to 93 µs (with an additional 69 µs + 41 µs spent on the single upfront resolves), yielding a net 13% speedup. No regressions in correctness; all tests passed with timing deltas under 8%.
|
Claude finished @codeflash-ai[bot]'s task in 2m 3s —— View job PR Review Summary
Prek Checks
Code ReviewBug Fixed: Duplicate comment block ✅ The optimization PR accidentally duplicated the existing comment block (lines 136–144 of Optimization correctness: The core change inlines # Before (per-iteration resolve inside mirror_path):
new_path = mirror_path(Path(file_path), original_git_root, self.current_worktree)
remapped[new_path] = [dataclasses.replace(func, file_path=mirror_path(func.file_path, ...)) for func in funcs]
# After (resolve once, reuse):
original_root_resolved = original_git_root.resolve()
worktree_resolved = self.current_worktree.resolve()
relative_path = file_path.resolve().relative_to(original_root_resolved)
new_path = worktree_resolved / relative_path
remapped[new_path] = [dataclasses.replace(func, file_path=new_path) for func in funcs]This is semantically equivalent to Duplicate DetectionNo duplicates detected. The inlined path-computation logic replaces calls to Test Coverage55 generated regression tests passed (100% coverage per PR report). No existing unit tests to compare against for this method. Fix committed: Removed duplicate comment block → |
|
Closing: CI unit tests are failing on the base branch (tests/test_git_utils.py::TestGetGitDiffMultiLanguage::test_java_diff_ignored_when_language_is_python and test_mixed_lang_diff_filters_by_current_language). These failures appear to be pre-existing on the fix/trigger_cc_on_multiple_commits branch and cannot be auto-fixed. |
⚡️ This pull request contains optimizations for PR #1869
If you approve this dependent PR, these changes will be merged into the original PR branch
fix/trigger_cc_on_multiple_commits.📄 14% (0.14x) speedup for
Optimizer.get_optimizable_functionsincodeflash/optimization/optimizer.py⏱️ Runtime :
491 microseconds→431 microseconds(best of250runs)📝 Explanation and details
The remapping branch previously called
mirror_pathonce per file anddataclasses.replaceonce per function, with each iteration incurring a repeatedfunc.file_path.resolve()cost insidemirror_path. The optimized code resolvesoriginal_git_rootandcurrent_worktreeexactly once before the loop, then computesrelative_pathdirectly viafile_path.resolve().relative_to(original_root_resolved)and constructsnew_pathwith simple path arithmetic (worktree_resolved / relative_path). Because all functions in a givenfuncslist share the samefile_path, the list comprehension now doesdataclasses.replace(func, file_path=new_path)with a pre‐computednew_path, eliminating redundant resolutions inside each function's replacement. Line profiler shows themirror_pathcall dropped from 226 µs to 93 µs (with an additional 69 µs + 41 µs spent on the single upfront resolves), yielding a net 13% speedup. No regressions in correctness; all tests passed with timing deltas under 8%.✅ Correctness verification report:
🌀 Click to see Generated Regression Tests
To edit these changes
git checkout codeflash/optimize-pr1869-2026-03-18T20.01.48and push.