⚡️ Speed up function check_formatter_installed by 1,676% in PR #853 (fix/formatter-reporting-and-early-exit)
#854
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 #853
If you approve this dependent PR, these changes will be merged into the original PR branch
fix/formatter-reporting-and-early-exit.📄 1,676% (16.76x) speedup for
check_formatter_installedincodeflash/code_utils/env_utils.py⏱️ Runtime :
1.75 seconds→98.4 milliseconds(best of43runs)📝 Explanation and details
The optimization achieves a 1676% speedup by introducing a smart early detection mechanism for formatter availability that avoids expensive disk I/O operations.
Key Optimization - Fast Formatter Detection:
The critical change is in
check_formatter_installed()where instead of always running the full formatter process on a temporary file (which involves disk writes, subprocess execution, and file formatting), the code now first tries quick version checks (--version,-V,-v) that most formatters support. This lightweight subprocess call requires no file I/O and immediately confirms if the executable works.Performance Impact:
format_code()which creates temp files, writes to disk, and runs the full formatter - taking 96.5% of execution timeSecondary Optimization - Efficient Line Counting:
Replaced
len(original_code.split("\n"))withoriginal_code.count('\n') + 1, avoiding unnecessary string splitting and list allocation for large files.Test Case Performance:
The optimization is particularly effective for scenarios involving:
python,echocommands)The fallback mechanism ensures backward compatibility while the version check provides immediate validation for the vast majority of real-world formatter tools.
✅ Correctness verification report:
🌀 Generated Regression Tests and Runtime
To edit these changes
git checkout codeflash/optimize-pr853-2025-10-24T21.19.34and push.