Commit 0916d12
authored
Optimize FunctionCallFinder._extract_source_code
The optimized code achieves a **295% speedup** through several key optimizations in the `_extract_source_code` method:
**1. Early Exit Optimization in Min Indent Finding**
The most significant improvement is adding an early exit when `min_indent` reaches 0:
```python
if min_indent == 0:
break # Early exit if we hit zero (can't get lower)
```
This eliminates unnecessary iterations when processing functions with no indentation, which is common for top-level functions.
**2. Caching Strip Operation**
Instead of calling `line.strip()` multiple times, the optimized version caches it:
```python
stripped = line.strip()
if stripped:
```
This reduces redundant string operations during the min indent calculation loop.
**3. Conditional List Comprehension with Fallbacks**
The optimized code replaces nested loops with list comprehensions but only when beneficial:
```python
if dedent_amount > 0:
result_lines = [line[dedent_amount:] if line.strip() and len(line) > dedent_amount else line
for line in func_lines]
else:
result_lines = list(func_lines)
```
When no processing is needed, it uses the faster `list(func_lines)` instead of applying transformations.
**Performance Impact by Test Case:**
- **Large functions** see the biggest gains (958% faster for 1000-line function, 870% for functions with many blank lines)
- **Small functions** show moderate improvements (16-56% faster)
- **Unicode content** benefits significantly (405% faster) due to reduced string operations
- **Class methods** see smaller gains since they have more complex indentation logic
The optimizations are particularly effective for **large codebases** and **functions with minimal indentation** (top-level functions), which are common in real-world code analysis scenarios.1 parent a3402f5 commit 0916d12
1 file changed
+20
-17
lines changed| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
| |||
994 | 994 | | |
995 | 995 | | |
996 | 996 | | |
997 | | - | |
| 997 | + | |
998 | 998 | | |
999 | 999 | | |
1000 | | - | |
| 1000 | + | |
| 1001 | + | |
1001 | 1002 | | |
1002 | | - | |
| 1003 | + | |
| 1004 | + | |
| 1005 | + | |
| 1006 | + | |
1003 | 1007 | | |
1004 | | - | |
| 1008 | + | |
1005 | 1009 | | |
1006 | 1010 | | |
1007 | 1011 | | |
1008 | | - | |
1009 | | - | |
1010 | | - | |
1011 | | - | |
1012 | | - | |
1013 | | - | |
| 1012 | + | |
| 1013 | + | |
| 1014 | + | |
| 1015 | + | |
| 1016 | + | |
| 1017 | + | |
| 1018 | + | |
| 1019 | + | |
| 1020 | + | |
| 1021 | + | |
| 1022 | + | |
1014 | 1023 | | |
1015 | | - | |
1016 | | - | |
1017 | | - | |
1018 | | - | |
1019 | | - | |
1020 | | - | |
1021 | | - | |
| 1024 | + | |
1022 | 1025 | | |
1023 | 1026 | | |
1024 | 1027 | | |
| |||
0 commit comments