Commit 2742025
authored
Optimize extract_init_stub_from_class
The optimized code achieves a **70% runtime speedup** (from 7.02ms to 4.13ms) through three key improvements:
## 1. **Faster Class Discovery via Deque-Based BFS (Primary Speedup)**
The original code uses `ast.walk()` which recursively traverses the entire AST tree even after finding the target class. The line profiler shows this taking 20.5ms (71% of time).
The optimized version replaces this with an explicit BFS using `collections.deque`, which stops immediately upon finding the target class. The profiler shows this reduces traversal time to 9.95ms - **cutting the search overhead by >50%**.
This is especially impactful when:
- The target class appears early in the module (eliminates unnecessary traversal)
- The module contains many classes (test shows 7-10% faster on modules with 100-1000 classes)
- The function is called frequently (shown by the 108% speedup on 1000 repeated calls)
## 2. **Explicit Loops Replace Generator Overhead**
The original code uses `any()` with a generator expression and `min()` with a generator to check decorators and find minimum line numbers. These create function call and generator overhead.
The optimized version uses explicit `for` loops with early breaks:
- Decorator checking: Directly iterates and breaks on first match
- Min line number: Uses explicit comparison instead of `min()` generator
The profiler shows decorator processing time reduced from ~1.4ms to ~0.3ms, and min line calculation from 69μs to 28μs.
## 3. **Conditional Flag Pattern for Relevance Checking**
Instead of evaluating both conditions in a compound expression, the optimized version uses an `is_relevant` flag with early exits, reducing redundant checks.
## Impact on Workloads
Based on `function_references`, this function is called from:
- `enrich_testgen_context`: Used in test generation workflows where it may process many classes
- Benchmark tests: Indicates this is in a performance-critical path
The optimization particularly benefits:
- **Large codebases**: 89-90% faster on classes with 100+ methods or 50+ properties
- **Repeated calls**: 108% faster when called 1000 times in sequence
- **Early matches**: Up to 88% faster when target class is found quickly
- **Deep nesting**: 57% faster for nested classes
The annotated tests show consistent 50-108% speedups across most scenarios, with minimal gains (6-10%) only when processing very large files where string slicing dominates runtime.1 parent eedd73d commit 2742025
1 file changed
Lines changed: 25 additions & 8 deletions
Lines changed: 25 additions & 8 deletions
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
| |||
3 | 3 | | |
4 | 4 | | |
5 | 5 | | |
6 | | - | |
| 6 | + | |
7 | 7 | | |
8 | 8 | | |
9 | 9 | | |
| |||
746 | 746 | | |
747 | 747 | | |
748 | 748 | | |
749 | | - | |
| 749 | + | |
| 750 | + | |
| 751 | + | |
| 752 | + | |
750 | 753 | | |
751 | 754 | | |
752 | 755 | | |
| 756 | + | |
| 757 | + | |
753 | 758 | | |
754 | 759 | | |
755 | 760 | | |
756 | 761 | | |
757 | 762 | | |
758 | 763 | | |
759 | 764 | | |
760 | | - | |
761 | | - | |
762 | | - | |
763 | | - | |
764 | | - | |
| 765 | + | |
| 766 | + | |
| 767 | + | |
| 768 | + | |
| 769 | + | |
| 770 | + | |
| 771 | + | |
| 772 | + | |
| 773 | + | |
| 774 | + | |
| 775 | + | |
| 776 | + | |
765 | 777 | | |
766 | 778 | | |
767 | 779 | | |
| |||
771 | 783 | | |
772 | 784 | | |
773 | 785 | | |
774 | | - | |
| 786 | + | |
| 787 | + | |
| 788 | + | |
| 789 | + | |
| 790 | + | |
| 791 | + | |
775 | 792 | | |
776 | 793 | | |
777 | 794 | | |
| |||
0 commit comments