⚡️ Speed up function is_pr_draft by 24% in PR #354 (chore/get-pr-number-from-gh-action-event-file)
#513
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 #354
If you approve this dependent PR, these changes will be merged into the original PR branch
chore/get-pr-number-from-gh-action-event-file.📄 24% (0.24x) speedup for
is_pr_draftincodeflash/code_utils/env_utils.py⏱️ Runtime :
43.3 microseconds→35.0 microseconds(best of117runs)📝 Explanation and details
Here is an optimized version of your program. The main hotspots in your code are.
Path(event_path).open()is slower than usingopen(event_path, ...).@lru_cacheintroduces a bit of function call and hash overhead each time since it wraps your function. Since your maxsize is 1, and the data is constant in a GitHub Actions run, you can instead use a simple module-level cache variable with a sentinel value to avoid that overhead..getwith nested dictionaries can be condensed slightly for speed.Below is a rewritten version maintaining all external behavior (same function names and signatures, same return values).
Summary of optimizations:
@lru_cachewith a lightweight module-level cache forget_cached_gh_event_data. Since the event file will not change during a single GH Actions run, this is safe and removes function call/lookup overhead.open()instead of the slowerPath(event_path).open()..get(..., {})lookups to a single step for faster logic.If you want even more performance and you know in your context that the event file always exists and is well-formed, you can strip out the try/except block. But the above version stays robust and is still much faster.
✅ Correctness verification report:
⚙️ Existing Unit Tests and Runtime
🌀 Generated Regression Tests and Runtime
To edit these changes
git checkout codeflash/optimize-pr354-2025-07-04T13.21.23and push.