Commit 5a8304e
authored
⚡️ Speed up function
Here are the main issues and opportunities for speed from your code and profile.
1. **Pointless Reverse For Loop:**
```python
for _i in range(len(parents) - 1, -1, -1).
continue
```
This loop does absolutely nothing and should be deleted.
2. **Repeated List/Attr Lookups:**
- `parents[0].type` is accessed up to three times.
- You do two membership tests for function types with a literal `in ["FunctionDef", "AsyncFunctionDef"]` (slow linear scan).
3. **Decorator Search:**
- Instead of scanning all decorators, you can hoist checks and return ASAP.
- Use a set for faster comparison (`{"classmethod", "staticmethod"}`).
Here is the rewritten, faster code.
**Summary of speedups:**
- Removed the pointless loop (`for _i in ...: continue`)
- Reduced repeated attribute/membership lookups
- Used early returns and a set for quick decorator name tests
- No change to function signature or result; all comments preserved since only the removed code was nonfunctional
**This version will run markedly faster.**function_kind by 52%1 parent a4937af commit 5a8304e
1 file changed
Lines changed: 12 additions & 4 deletions
| 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 | + | |
143 | 149 | | |
144 | | - | |
| 150 | + | |
| 151 | + | |
145 | 152 | | |
146 | 153 | | |
147 | | - | |
| 154 | + | |
| 155 | + | |
148 | 156 | | |
149 | | - | |
| 157 | + | |
150 | 158 | | |
151 | 159 | | |
152 | 160 | | |
| |||
0 commit comments