⚡️ Speed up function speedup_critic by 23% in PR #620 (pre-commit-update)
#621
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 #620
If you approve this dependent PR, these changes will be merged into the original PR branch
pre-commit-update.📄 23% (0.23x) speedup for
speedup_criticincodeflash/result/critic.py⏱️ Runtime :
1.27 milliseconds→1.03 milliseconds(best of153runs)📝 Explanation and details
The optimization introduces a cached GitHub Actions detection mechanism that eliminates repeated expensive environment lookups in the hot path.
Key optimization:
_in_github_actions_mode()function with@lru_cache(maxsize=1)that caches the result ofenv_utils.get_pr_number()bool(env_utils.get_pr_number())call insidespeedup_criticwith a call to the cached functionWhy this is faster:
The original code called
env_utils.get_pr_number()on every invocation ofspeedup_critic. Whileget_pr_number()itself is cached, it still involves function call overhead and the boolean conversion. The line profiler shows this operation took 5.69ms (28.8% of total time) in the original vs only 1.09ms (7.7%) in the optimized version.Performance characteristics:
speedup_criticis called many times (as shown in the large-scale tests with 500+ candidates), where the 22% speedup compounds significantlyThe optimization maintains identical behavior while reducing redundant work through strategic caching of the GitHub Actions detection logic.
✅ Correctness verification report:
⚙️ Existing Unit Tests and Runtime
🌀 Generated Regression Tests and Runtime
To edit these changes
git checkout codeflash/optimize-pr620-2025-08-06T04.15.09and push.