Commit 5331f8b
authored
Here is an optimized version of your program. The main hotspots in your code are.
- Disk IO with reading/parsing the event file (unavoidable but can be slightly optimized).
- Using `Path(event_path).open()` is slower than using `open(event_path, ...)`.
- `@lru_cache` introduces 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.
- The use of lots of chained `.get` with 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:**
- Replaced `@lru_cache` with a lightweight module-level cache for `get_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.
- Used plain `open()` instead of the slower `Path(event_path).open()`.
- Reduced nested `.get(..., {})` lookups to a single step for faster logic.
- Kept exception handling to prevent failure if the file is missing/corrupt.
- No external behavior was changed: all function names/signatures/return values are identical.
- Preserved all important comments as requested.
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.
1 parent 23b2bdc commit 5331f8b
1 file changed
+18
-5
lines changed| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
| |||
12 | 12 | | |
13 | 13 | | |
14 | 14 | | |
| 15 | + | |
| 16 | + | |
15 | 17 | | |
16 | 18 | | |
17 | 19 | | |
| |||
100 | 102 | | |
101 | 103 | | |
102 | 104 | | |
103 | | - | |
104 | 105 | | |
| 106 | + | |
| 107 | + | |
| 108 | + | |
105 | 109 | | |
106 | 110 | | |
107 | | - | |
108 | | - | |
109 | | - | |
| 111 | + | |
| 112 | + | |
| 113 | + | |
| 114 | + | |
| 115 | + | |
| 116 | + | |
| 117 | + | |
| 118 | + | |
110 | 119 | | |
111 | 120 | | |
112 | 121 | | |
| |||
128 | 137 | | |
129 | 138 | | |
130 | 139 | | |
131 | | - | |
| 140 | + | |
| 141 | + | |
| 142 | + | |
| 143 | + | |
| 144 | + | |
0 commit comments