Commit 1502b4d
authored
Optimize StandaloneCallTransformer._parse_bracket_standalone_call
The optimized code achieves a **147% speedup** (2.47x faster) by fundamentally changing how the `_find_balanced_parens` method searches through JavaScript code to find matching parentheses.
**Key Optimization: Regex-Based Scanning**
The original code iterates through **every single character** in the string (20,283 iterations) to find quotes and parentheses. The optimized version uses a precompiled regex pattern (`self._special_re = re.compile(r'["\'`()]')`) to **jump directly between special characters**, reducing iterations from 20,283 to just 1,268 - a **94% reduction in loop iterations**.
**Why This Is Faster**
1. **Compiled regex scanning**: The regex engine (implemented in C) can skip over irrelevant characters much faster than Python's character-by-character iteration
2. **Fewer comparisons**: Instead of checking `if char in quotes` on every character, the regex only returns when it finds a relevant character
3. **Reduced overhead**: The optimized loop executes 16x fewer times, eliminating 19,000+ Python bytecode interpretation cycles
**Line Profiler Evidence**
The `_find_balanced_parens` function shows dramatic improvement:
- **Original**: 18.4ms total (20,321 loop iterations @ 182.7ns per hit on the while condition)
- **Optimized**: 3.2ms total (1,305 loop iterations @ 194.2ns per hit)
- **5.7x faster** for this function alone
The while loop body itself drops from consuming 98.3% of function time (checking every character) to the regex search consuming just 22.7% (jumping between special characters).
**Trade-offs**
The regex approach adds small overhead for each `match.group()` and `match.start()` call (9.3% + 7.5% of optimized time), but this is vastly outweighed by eliminating 19,000 character checks.
**Impact**
Since `_parse_bracket_standalone_call` spends 98.6% of its time in `_find_balanced_parens`, this optimization cascades up the call stack. This makes the code particularly beneficial when parsing JavaScript files with deeply nested function calls or large argument lists where the parenthesis-matching logic is heavily exercised.1 parent a3ca8ee commit 1502b4d
1 file changed
Lines changed: 14 additions & 7 deletions
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
| |||
179 | 179 | | |
180 | 180 | | |
181 | 181 | | |
| 182 | + | |
| 183 | + | |
| 184 | + | |
182 | 185 | | |
183 | 186 | | |
184 | 187 | | |
| |||
380 | 383 | | |
381 | 384 | | |
382 | 385 | | |
| 386 | + | |
| 387 | + | |
383 | 388 | | |
384 | | - | |
| 389 | + | |
| 390 | + | |
| 391 | + | |
| 392 | + | |
| 393 | + | |
| 394 | + | |
385 | 395 | | |
386 | | - | |
387 | | - | |
388 | 396 | | |
389 | | - | |
| 397 | + | |
390 | 398 | | |
391 | 399 | | |
392 | 400 | | |
| |||
399 | 407 | | |
400 | 408 | | |
401 | 409 | | |
402 | | - | |
403 | | - | |
| 410 | + | |
| 411 | + | |
404 | 412 | | |
405 | 413 | | |
406 | 414 | | |
407 | 415 | | |
408 | | - | |
409 | 416 | | |
410 | 417 | | |
411 | 418 | | |
| |||
0 commit comments