Commit 4c4feef
authored
Optimize CommentMapper.visit_FunctionDef
The optimized code achieves an 11% speedup through several targeted micro-optimizations that reduce attribute lookups and function call overhead in the hot path:
**Key Optimizations:**
1. **Local Variable Caching**: Frequently accessed attributes (`self.context_stack`, `self.original_runtimes`, etc.) are stored in local variables at function start, eliminating repeated `self.` lookups in tight loops.
2. **Method Reference Caching**: `self.get_comment` is cached as `get_comment`, and `context_stack.append`/`pop` are stored locally, reducing method lookup overhead in the main processing loop.
3. **Type Tuple Pre-computation**: The commonly used type tuples for `isinstance` checks are stored in local variables (`_stmt_types`, `_node_stmt_assign`), avoiding tuple creation on every iteration.
4. **Optimized Node Collection**: The inefficient pattern of creating a list then extending it (`nodes_to_check = [...]; nodes_to_check.extend(...)`) is replaced with conditional unpacking (`[compound_line_node, *body_attr]` or `[compound_line_node]`), reducing list operations.
5. **f-string Usage**: String concatenations for `inv_id` and `match_key` are converted to f-strings, which are faster than concatenation operations.
**Performance Characteristics:**
- Best gains on **large-scale test cases** (24.9-32.5% faster) with many nested blocks or statements, where the micro-optimizations compound
- Minimal overhead on **simple cases** (0.6-7.9% variance), showing the optimizations don't hurt baseline performance
- Most effective when processing complex ASTs with deep nesting, as seen in the `test_large_many_nested_blocks` (24.9% faster) and `test_large_sparse_runtime_keys` (32.5% faster) cases
The optimizations target the innermost loops where attribute lookups and object creation happen most frequently, making them particularly effective for batch AST processing workflows.1 parent 40c4108 commit 4c4feef
1 file changed
+46
-20
lines changed| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
| |||
60 | 60 | | |
61 | 61 | | |
62 | 62 | | |
63 | | - | |
64 | | - | |
65 | | - | |
66 | | - | |
| 63 | + | |
| 64 | + | |
| 65 | + | |
| 66 | + | |
| 67 | + | |
| 68 | + | |
| 69 | + | |
| 70 | + | |
| 71 | + | |
| 72 | + | |
| 73 | + | |
| 74 | + | |
| 75 | + | |
| 76 | + | |
| 77 | + | |
| 78 | + | |
| 79 | + | |
| 80 | + | |
| 81 | + | |
| 82 | + | |
| 83 | + | |
| 84 | + | |
| 85 | + | |
67 | 86 | | |
68 | | - | |
69 | | - | |
70 | | - | |
| 87 | + | |
| 88 | + | |
| 89 | + | |
| 90 | + | |
71 | 91 | | |
72 | | - | |
73 | | - | |
74 | | - | |
| 92 | + | |
| 93 | + | |
| 94 | + | |
| 95 | + | |
| 96 | + | |
| 97 | + | |
| 98 | + | |
| 99 | + | |
75 | 100 | | |
76 | | - | |
77 | | - | |
78 | | - | |
79 | | - | |
80 | | - | |
| 101 | + | |
| 102 | + | |
| 103 | + | |
| 104 | + | |
| 105 | + | |
| 106 | + | |
81 | 107 | | |
82 | 108 | | |
83 | | - | |
84 | | - | |
85 | | - | |
86 | | - | |
| 109 | + | |
| 110 | + | |
| 111 | + | |
| 112 | + | |
87 | 113 | | |
88 | | - | |
| 114 | + | |
89 | 115 | | |
90 | 116 | | |
91 | 117 | | |
| |||
0 commit comments