⚡️ Speed up function _compute_wrapped_segment by 44% in PR #1561 (add/support_react)#1606
Conversation
This optimization achieves a **43% runtime improvement** (from 1.20ms to 835μs) through two key changes that eliminate Python overhead:
## Primary Optimization: Explicit Loop in `_contains_jsx`
The original used `any(_contains_jsx(child) for child in node.children)`, which creates a generator object on every call. The optimized version replaces this with an explicit `for` loop that can short-circuit immediately upon finding JSX:
```python
for child in node.children:
if _contains_jsx(child):
return True
return False
```
This eliminates generator allocation overhead and enables early termination. The line profiler shows the impact: the original's generator expression took **1.04μs** (73.1% of function time), while the optimized explicit loop takes only **0.39μs** total for iteration and checks (40.6% + 0.7%). This is especially effective for the common case where JSX is found early in the children list.
## Secondary Optimization: Loop Consolidation in `_compute_wrapped_segment`
The original code iterated over `return_node.children` twice - once to find JSX and again to check for parenthesized expressions. The optimized version merges these into a single pass by checking `child.type == "parenthesized_expression"` immediately when JSX is found, before breaking from the loop.
This eliminates the redundant iteration that processed ~1395 children in the second loop (shown in the original profiler). The optimization is particularly effective for test cases with many children - the large performance test shows **104% speedup** (117μs → 57.7μs) and the parenthesized test with 200+ children shows **106% speedup** (77.1μs → 37.4μs).
## Runtime Impact by Test Case
- Simple JSX: 6% faster (baseline benefit)
- Self-closing JSX with whitespace: 36.5% faster (generator elimination effect)
- Parenthesized expression: 58% faster (both optimizations)
- Nested JSX detection: 55.7% faster (recursive generator elimination)
- Large workloads (300+ children): 104-106% faster (loop consolidation dominates)
The optimizations are most impactful when processing complex React component trees with many children or deeply nested JSX structures, which are common in real-world React codebases.
PR Review SummaryPrek Checks
Code ReviewNo critical issues found. The optimization is correct:
No security vulnerabilities, breaking API changes, or test issues introduced by this PR. The 8 test failures in Test CoverageThis PR modifies only
Key observations:
Last updated: 2026-02-20 |
⚡️ This pull request contains optimizations for PR #1561
If you approve this dependent PR, these changes will be merged into the original PR branch
add/support_react.📄 44% (0.44x) speedup for
_compute_wrapped_segmentincodeflash/languages/javascript/frameworks/react/profiler.py⏱️ Runtime :
1.20 milliseconds→835 microseconds(best of14runs)📝 Explanation and details
This optimization achieves a 43% runtime improvement (from 1.20ms to 835μs) through two key changes that eliminate Python overhead:
Primary Optimization: Explicit Loop in
_contains_jsxThe original used
any(_contains_jsx(child) for child in node.children), which creates a generator object on every call. The optimized version replaces this with an explicitforloop that can short-circuit immediately upon finding JSX:This eliminates generator allocation overhead and enables early termination. The line profiler shows the impact: the original's generator expression took 1.04μs (73.1% of function time), while the optimized explicit loop takes only 0.39μs total for iteration and checks (40.6% + 0.7%). This is especially effective for the common case where JSX is found early in the children list.
Secondary Optimization: Loop Consolidation in
_compute_wrapped_segmentThe original code iterated over
return_node.childrentwice - once to find JSX and again to check for parenthesized expressions. The optimized version merges these into a single pass by checkingchild.type == "parenthesized_expression"immediately when JSX is found, before breaking from the loop.This eliminates the redundant iteration that processed ~1395 children in the second loop (shown in the original profiler). The optimization is particularly effective for test cases with many children - the large performance test shows 104% speedup (117μs → 57.7μs) and the parenthesized test with 200+ children shows 106% speedup (77.1μs → 37.4μs).
Runtime Impact by Test Case
The optimizations are most impactful when processing complex React component trees with many children or deeply nested JSX structures, which are common in real-world React codebases.
✅ Correctness verification report:
🌀 Click to see Generated Regression Tests
To edit these changes
git checkout codeflash/optimize-pr1561-2026-02-20T13.20.28and push.