⚡️ Speed up function project_root_from_module_root by 509% in PR #790 (fix/use-git-root-as-project-root-in-worktree)
#791
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.
⚡️ This pull request contains optimizations for PR #790
If you approve this dependent PR, these changes will be merged into the original PR branch
fix/use-git-root-as-project-root-in-worktree.📄 509% (5.09x) speedup for
project_root_from_module_rootincodeflash/cli_cmds/cli.py⏱️ Runtime :
23.9 milliseconds→3.93 milliseconds(best of239runs)📝 Explanation and details
The optimization replaces the expensive
module_root.parent.resolve()call with a more efficient path handling approach that avoids unnecessary filesystem operations.Key Changes:
return module_root.parent.resolve()into two operations:parent = module_root.parent- gets the parent pathreturn parent if parent.is_absolute() else parent.absolute()- conditionally applies path resolutionWhy This Is Faster:
The original code called
resolve()on every parent path, which performs expensive filesystem operations to resolve symlinks and canonicalize paths. The profiler shows this line consumed 84.8% of the total runtime (74.4ms out of 87.8ms).The optimized version uses
is_absolute()(a fast string check) to determine if the path is already absolute, and only callsabsolute()when needed. Unlikeresolve(),absolute()doesn't hit the filesystem to resolve symlinks or verify path existence - it simply converts relative paths to absolute ones through string manipulation.Performance Impact:
pyproject_file_path.parent != module_root✅ Correctness verification report:
🌀 Generated Regression Tests and Runtime
To edit these changes
git checkout codeflash/optimize-pr790-2025-10-02T11.22.07and push.