⚡️ Speed up function _prompt_custom_directory by 365% in PR #1199 (omni-java)
#1237
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 #1199
If you approve this dependent PR, these changes will be merged into the original PR branch
omni-java.📄 365% (3.65x) speedup for
_prompt_custom_directoryincodeflash/cli_cmds/init_java.py⏱️ Runtime :
101 milliseconds→21.7 milliseconds(best of54runs)📝 Explanation and details
The optimized code achieves a 364% speedup (101ms → 21.7ms) through two targeted optimizations:
1. Theme Instance Caching (Primary Optimization)
The original code recreated a
CodeflashTheme()instance on every call to_get_theme(). Line profiler shows this function was called 360 times, spending 88.2% of execution time (133.4ms) just instantiating theme objects.The optimization introduces a module-level cache (
_cached_theme) that stores the theme after first creation. This reduces_get_theme()total time from 151.2ms to 17.4ms (88.5% reduction), as subsequent calls simply return the cached instance instead of re-instantiating.Test Impact: Basic path validation tests show 900-1000% speedup (265μs → 25μs), stress tests with 50-100 invalid retries show 281-300% speedup (16ms → 4.2ms), and deeply nested paths improve by 300-500%. The caching is most effective in scenarios with multiple validation attempts, which is common when users enter incorrect paths.
2. Faster Absolute Path Check
The original code used
Path(path).is_absolute()which instantiates aPathobject just to check if a path is absolute. Line profiler shows this consumed 55.4% ofvalidate_relative_directory_path()time (4.28ms).The optimization replaces this with
os.path.isabs(path), a lightweight string-based check that avoids object instantiation. This reduces the absolute path check from 4.28ms to 0.68ms (84% reduction).Why This Works
os.path.isabs()is functionally equivalent toPath().is_absolute()for validation purposes but avoids the overhead of pathlib's object modelCodeflashThemeinitialization likely involves terminal capability detection and color setup, making it expensive to repeatContext Considerations
While function references aren't available, the fact that
_get_theme()was called 360 times in the test suite suggests this function is in a validation loop where users may enter multiple invalid paths before succeeding. The optimization particularly benefits workflows with:The test results confirm this: single-path tests show 9-10x speedup, while tests with 50-100 retries show 3-4x speedup, demonstrating scalability benefits for both quick validations and extended retry scenarios.
✅ Correctness verification report:
🌀 Click to see Generated Regression Tests
To edit these changes
git checkout codeflash/optimize-pr1199-2026-02-01T21.28.53and push.