⚡️ Speed up function command_exists by 16%
#107
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.
📄 16% (0.16x) speedup for
command_existsinskyvern/cli/database.py⏱️ Runtime :
11.0 milliseconds→9.49 milliseconds(best of216runs)📝 Explanation and details
The optimized code achieves a 16% speedup by inlining the core logic of
shutil.which()and eliminating its overhead. Here's what was optimized:Key Performance Improvements:
Eliminated
shutil.which()overhead: The original code delegates entirely toshutil.which(), which has additional internal complexity for cross-platform compatibility and error handling that isn't needed here.Efficient PATH processing: The optimized version splits the PATH environment variable only once and reuses the list, rather than potentially re-parsing it internally within
shutil.which().Early termination optimizations:
Reduced function call overhead: Direct OS operations (
os.path.join,os.path.isfile,os.access) instead of going throughshutil.which()'s abstraction layers.Impact on Workloads:
Based on the function references,
command_exists()is called in critical infrastructure setup paths:psqlandpg_isreadyavailabilityThe test results show particularly strong gains for edge cases like empty strings (102% faster) and custom PATH scenarios (53-172% faster), suggesting the optimization is especially effective when PATH is limited or commands don't exist.
Best Performance Cases:
This optimization is valuable since
command_exists()is used in setup and validation workflows where it may be called repeatedly during system initialization.✅ Correctness verification report:
🌀 Generated Regression Tests and Runtime
To edit these changes
git checkout codeflash/optimize-command_exists-mir38qt3and push.