You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
Optimize CommentMapper.visit_FunctionDef for PR #719
The optimized code achieves a 13% speedup through several targeted micro-optimizations that reduce overhead in the hot loops:
**Key optimizations applied:**
1. **Hoisted loop-invariant computations**: Moved `isinstance` tuple constants (`compound_types`, `valid_types`) and frequently accessed attributes (`get_comment`, `orig_rt`, `opt_rt`) outside the loops to avoid repeated lookups.
2. **Precomputed key prefix**: Instead of repeatedly concatenating `test_qualified_name + "#" + str(self.abs_path)` inside loops, this is computed once as `key_prefix` and reused with f-string formatting.
3. **Optimized `getattr` usage**: Replaced the costly `getattr(compound_line_node, "body", [])` pattern with a single `getattr(..., None)` call, then conditionally building the `nodes_to_check` list using unpacking (`*compound_line_node_body`) when a body exists.
4. **Reduced function call overhead**: Cached the `get_comment` method reference and called it once per `match_key`, reusing the same comment for all nodes that share the same key, rather than calling it for each individual node.
5. **String formatting optimization**: Replaced string concatenation with f-string formatting for better performance.
**Performance characteristics by test case:**
- **Large-scale tests** show the best improvements (10-79% faster), particularly `test_large_deeply_nested` (78.8% faster) where the inner loop optimizations have maximum impact
- **Basic cases** show modest gains (1-4% faster) as there's less loop iteration overhead to optimize
- **Edge cases** with minimal computation show negligible or slightly negative impact due to the upfront setup cost of hoisted variables
The optimizations are most effective for functions with complex nested structures (for/while/if blocks) and many runtime entries, where the reduced per-iteration overhead compounds significantly.
0 commit comments