Commit 34f5d31
authored
⚡️ Speed up function
Here is an optimized version of your function, making the following **performance improvements**.
- **Avoids repeatedly creating lists and checking list membership** (`parents[0].type in ["FunctionDef", "AsyncFunctionDef"]`). This is more efficiently done via a set: `{"FunctionDef", "AsyncFunctionDef"}`.
- **Reduces attribute lookups and loop overhead** inside the decorator check, using local variables, early return, and prepares the valid decorator names as a set for speedy comparison.
- **Reduces repeated attribute lookups** (`parents[0]`), storing it in a local variable for speed.
- **Avoids unnecessary `isinstance()` checks/loops** by pre-filtering.
- Keeps both **return type and functionality** the same as before.
### Changes Made
- Changed `["FunctionDef", "AsyncFunctionDef"]` to `{"FunctionDef", "AsyncFunctionDef"}` for O(1) lookup.
- Reduced multiple attribute lookups by storing `parents[0]` in `parent_type`.
- Combined the `if` and `elif` for decorator names for faster path to result (early return).
- The logic and return values are identical.
This should result in reduced runtime, particularly for the high-frequency paths in your line profiling results.function_kind by 11%1 parent a4937af commit 34f5d31
1 file changed
+15
-4
lines changed| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
| |||
7 | 7 | | |
8 | 8 | | |
9 | 9 | | |
| 10 | + | |
| 11 | + | |
10 | 12 | | |
11 | 13 | | |
12 | 14 | | |
| |||
139 | 141 | | |
140 | 142 | | |
141 | 143 | | |
142 | | - | |
| 144 | + | |
| 145 | + | |
| 146 | + | |
| 147 | + | |
| 148 | + | |
| 149 | + | |
143 | 150 | | |
144 | | - | |
| 151 | + | |
| 152 | + | |
| 153 | + | |
145 | 154 | | |
146 | 155 | | |
147 | | - | |
| 156 | + | |
| 157 | + | |
148 | 158 | | |
149 | | - | |
| 159 | + | |
150 | 160 | | |
151 | 161 | | |
| 162 | + | |
152 | 163 | | |
153 | 164 | | |
154 | 165 | | |
| |||
0 commit comments