⚡️ Speed up function is_pr_draft
by 121% in PR #384 (trace-and-optimize
)
#499
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 #384
If you approve this dependent PR, these changes will be merged into the original PR branch
trace-and-optimize
.📄 121% (1.21x) speedup for
is_pr_draft
incodeflash/code_utils/env_utils.py
⏱️ Runtime :
4.98 milliseconds
→2.25 milliseconds
(best of94
runs)📝 Explanation and details
Here's an optimized version of your Python program, focused on runtime and memory.
Key changes:
Avoids reading the event file or parsing JSON if not needed.
Reads the file as binary and parses with
json.loads()
for slightly faster IO.References the
"draft"
property directly using.get()
to avoid possibleKeyError
.Reduces scope of data loaded from JSON for less memory usage.
Caches the result of parsing the event file for repeated calls within the same process.
The inner try/except is kept close to only catching the specific case.
Results for each event_path file are cached in memory.
Exception handling and comments are preserved where their context is changed.
I/O and JSON parsing is only done if both env vars are set and PR number exists.
✅ Correctness verification report:
🌀 Generated Regression Tests and Runtime
To edit these changes
git checkout codeflash/optimize-pr384-2025-07-03T05.58.51
and push.