⚡️ Speed up function get_path_for_workflow_download_directory by 119%
#155
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.
📄 119% (1.19x) speedup for
get_path_for_workflow_download_directoryinskyvern/forge/sdk/api/files.py⏱️ Runtime :
1.76 milliseconds→802 microseconds(best of100runs)📝 Explanation and details
The optimization adds a directory existence check using
os.path.isdir()before callingos.makedirs(), providing a 119% speedup by eliminating unnecessary system calls.What was optimized:
if not os.path.isdir(download_dir):guard clause beforeos.makedirs(download_dir, exist_ok=True)os.makedirs()call when the directory already existsWhy this is faster:
os.makedirs()involves system calls to check directory existence, create directories, and handle permissions even withexist_ok=Trueos.path.isdir()is a lighter-weight filesystem check that only verifies existence without creation overheados.makedirs()took 88.7% of execution time (3.76ms) in the original vs only 2.5% (38μs) in the optimized version when directories already existPerformance characteristics:
isdir()checkexecute_step()in a workflow execution loop, making this optimization particularly valuable for repeated workflow runs that reuse the same download directoriesTest results show consistent improvements:
✅ Correctness verification report:
🌀 Generated Regression Tests and Runtime
To edit these changes
git checkout codeflash/optimize-get_path_for_workflow_download_directory-mjaqbqaxand push.