Commit 4f8ff2d
authored
Optimize _extract_class_body_context
The optimized code achieves an **11% runtime improvement** (93.6μs → 83.6μs) through two key changes:
**1. Caching `child.type` in a local variable**
```python
child_type = child.type # Cache the attribute access
if child_type in ("{", "}", ";", ","):
```
In the loop over `body_node.children`, `child.type` was accessed 3-4 times per iteration. By storing it once in `child_type`, we eliminate repeated attribute lookups on the Node object, which are more expensive than local variable access in Python.
**2. Replacing `append("".join(...))` with `extend(...)`**
Original:
```python
field_lines = lines[javadoc_start : end_line + 1]
field_parts.append("".join(field_lines)) # Join then append
```
Optimized:
```python
field_parts.extend(lines[javadoc_start : end_line + 1]) # Directly extend
```
This eliminates intermediate string concatenations inside the loop. Instead of creating a joined string for each field/constructor and appending it to the list, we extend the list with the raw line slices. The final `"".join(field_parts)` at the end performs one single join operation over all accumulated lines, which is significantly more efficient than multiple joins.
**Performance impact by test case:**
- **Large-scale test** (200 fields): 16.5% faster (71.6μs → 61.5μs) - the extend optimization scales particularly well with many fields
- **Multiple mixed fields/constructors**: 4.86% faster - benefits from both optimizations
- **Basic single field tests**: slight variation (some 0.5-5% slower) - the overhead of the extra local variable assignment is negligible for single-element cases but the optimization still maintains correctness
The optimization is most effective when processing Java files with many field declarations or constructors, which is common in real-world codebases. The deferred string joining pattern is a classic Python performance technique that reduces memory allocations and intermediate object creation.1 parent 41b08a9 commit 4f8ff2d
1 file changed
Lines changed: 11 additions & 11 deletions
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
| |||
413 | 413 | | |
414 | 414 | | |
415 | 415 | | |
| 416 | + | |
| 417 | + | |
416 | 418 | | |
417 | | - | |
| 419 | + | |
418 | 420 | | |
419 | 421 | | |
420 | 422 | | |
421 | 423 | | |
422 | | - | |
| 424 | + | |
423 | 425 | | |
424 | 426 | | |
425 | 427 | | |
426 | 428 | | |
427 | | - | |
| 429 | + | |
428 | 430 | | |
429 | 431 | | |
430 | 432 | | |
| |||
436 | 438 | | |
437 | 439 | | |
438 | 440 | | |
439 | | - | |
440 | | - | |
| 441 | + | |
441 | 442 | | |
442 | 443 | | |
443 | | - | |
| 444 | + | |
444 | 445 | | |
445 | 446 | | |
446 | | - | |
447 | | - | |
| 447 | + | |
448 | 448 | | |
449 | 449 | | |
450 | | - | |
| 450 | + | |
451 | 451 | | |
452 | 452 | | |
453 | 453 | | |
| |||
459 | 459 | | |
460 | 460 | | |
461 | 461 | | |
462 | | - | |
463 | | - | |
| 462 | + | |
| 463 | + | |
464 | 464 | | |
465 | 465 | | |
466 | 466 | | |
| |||
0 commit comments