Skip to content

⚡️ Speed up function is_pr_draft by 121% in PR #384 (trace-and-optimize) #499

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Closed
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
30 changes: 20 additions & 10 deletions codeflash/code_utils/env_utils.py
Original file line number Diff line number Diff line change
Expand Up @@ -125,14 +125,24 @@ def is_LSP_enabled() -> bool:

def is_pr_draft() -> bool:
"""Check if the PR is draft. in the github action context."""
event_path = os.getenv("GITHUB_EVENT_PATH")
pr_number = get_pr_number()
if pr_number is not None and event_path:
event_data = get_event_data(event_path)
if event_data:
try:
return bool(event_data.get("pull_request", {}).get("draft", False))
except Exception as e:
logger.warning(f"Error checking if PR is draft: {e}")
return False


@lru_cache(maxsize=1)
def get_event_data(event_path: str) -> Optional[dict]:
try:
event_path = os.getenv("GITHUB_EVENT_PATH")
pr_number = get_pr_number()
if pr_number is not None and event_path:
with Path(event_path).open() as f:
event_data = json.load(f)
return bool(event_data["pull_request"]["draft"])
return False # noqa
except Exception as e:
logger.warning(f"Error checking if PR is draft: {e}")
return False
with open(event_path, "rb") as f:
# Read as bytes, decode and parse to reduce intermediate allocations
data = f.read()
return json.loads(data)
except Exception:
return None
Loading