Commit d49ee67
authored
Optimize AsyncCallInstrumenter._call_in_positions
The optimization achieves a 33% speedup by **eliminating redundant attribute lookups** in the `node_in_call_position` function.
**Key changes:**
- **Cached attribute access**: Instead of repeatedly accessing `node.lineno`, `node.col_offset`, `node.end_lineno`, and `node.end_col_offset` within the loop, these values are now extracted once into local variables (`node_lineno`, `node_col_offset`, etc.) at the beginning of the function.
- **Reduced getattr overhead**: The optimization uses `getattr()` with default values for optional attributes (`end_lineno`, `end_col_offset`) instead of repeated `hasattr()` checks.
- **Similar treatment for position attributes**: `pos.line_no` and `pos.col_no` are cached as `pos_line_no` and `pos_col_no` to avoid repeated attribute lookups within the inner loop.
**Why this improves performance:**
In Python, attribute access has overhead due to the dynamic nature of object attribute resolution. When the same attributes are accessed multiple times in a loop (especially nested loops), this overhead compounds. By caching these values in local variables, the optimization reduces the number of attribute lookups from O(n) per attribute to O(1), where n is the number of positions being checked.
**Test case performance patterns:**
- **Large-scale tests show the biggest gains** (50-60% faster) because they iterate through many positions, maximizing the benefit of reduced attribute lookups
- **Basic tests with few positions show moderate gains** (5-17% faster) as the optimization overhead is minimal
- **Edge cases with missing attributes show mixed results** since the `getattr()` approach handles these cases differently but equivalently1 parent 2c97b92 commit d49ee67
1 file changed
+18
-15
lines changed| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
| |||
20 | 20 | | |
21 | 21 | | |
22 | 22 | | |
| 23 | + | |
| 24 | + | |
| 25 | + | |
| 26 | + | |
23 | 27 | | |
24 | | - | |
25 | | - | |
26 | | - | |
27 | | - | |
28 | | - | |
29 | | - | |
30 | | - | |
31 | | - | |
32 | | - | |
33 | | - | |
34 | | - | |
35 | | - | |
36 | | - | |
37 | | - | |
38 | | - | |
| 28 | + | |
| 29 | + | |
| 30 | + | |
| 31 | + | |
| 32 | + | |
| 33 | + | |
| 34 | + | |
| 35 | + | |
| 36 | + | |
| 37 | + | |
| 38 | + | |
| 39 | + | |
| 40 | + | |
| 41 | + | |
39 | 42 | | |
40 | 43 | | |
41 | 44 | | |
| |||
0 commit comments