Commit 105a428
authored
Optimize transform_java_assertions
This optimization achieves a **71% runtime improvement** through three key changes that reduce repeated work and CPU overhead:
## What Changed
1. **Module-level regex compilation**: The assignment-detection regex (`_ASSIGN_RE`) is now compiled once at module import time instead of being recompiled for every `JavaAssertTransformer` instance. In the original code, line profiler shows `re.compile()` consuming **78.5% of `__init__` time** (671μs per call × 42 calls). The optimized version reduces this to **47.1%** (157μs per call), saving ~520μs total across all instances.
2. **Lazy analyzer initialization**: The `JavaAnalyzer` is now created on-demand in the `transform()` method only when needed, rather than eagerly in `__init__`. This eliminates unnecessary analyzer creation when instances don't end up calling `transform()`. The optimized code shows the lazy check taking only 13.7μs versus the eager initialization cost.
3. **O(n²) → O(n) nested assertion detection**: The original code used a nested loop to filter nested assertions, comparing every assertion against every other assertion (1.28M comparisons for 1,884 assertions, consuming **75.5% of transform() time**). The optimized version uses a single-pass algorithm with a running `max_end` tracker, reducing this to just 1,884 comparisons (~0.3% of transform time).
4. **Linear string building**: The original code applied replacements in reverse order using repeated string slicing (`result[:start] + replacement + result[end:]`), which created intermediate string copies. The optimized version builds a list of string parts in a single forward pass and joins them once, eliminating redundant memory allocations.
## Why It's Faster
- **Reduced redundant work**: Compiling the same regex pattern 42 times was pure overhead - the pattern never changes between instances.
- **Algorithmic improvement**: The nested loop performed O(n²) comparisons where O(n) sufficed. With typical test files having hundreds of assertions, this quadratic behavior was the primary bottleneck (consuming 75.5% of runtime).
- **Memory efficiency**: Building strings incrementally via slicing creates n intermediate copies for n replacements. The parts-list approach allocates once and assembles once.
## Impact on Workloads
The function references show `transform_java_assertions()` is called extensively in test transformation workflows. The optimization particularly benefits:
- **Large test files**: The `test_large_source_file` case (500 assertions) improved by **53.1%** (41.9ms → 27.4ms)
- **Very large files**: The `test_1000_line_source` case (1000 assertions) improved by **115%** (115ms → 53.7ms)
- **Many repeated calls**: The `test_many_assertions` case (100 assertions) improved by **10.4%** (5.88ms → 5.32ms)
Since test files often contain dozens to hundreds of assertion statements, and the function is called once per test transformation, these improvements compound significantly in CI/CD pipelines processing entire test suites.
The optimization is most effective for test files with many assertions, where the O(n²) nested detection becomes the dominant bottleneck.1 parent a979c45 commit 105a428
1 file changed
Lines changed: 30 additions & 14 deletions
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
| |||
28 | 28 | | |
29 | 29 | | |
30 | 30 | | |
| 31 | + | |
| 32 | + | |
31 | 33 | | |
32 | 34 | | |
33 | 35 | | |
| |||
206 | 208 | | |
207 | 209 | | |
208 | 210 | | |
| 211 | + | |
| 212 | + | |
| 213 | + | |
| 214 | + | |
| 215 | + | |
| 216 | + | |
209 | 217 | | |
210 | 218 | | |
211 | 219 | | |
| |||
220 | 228 | | |
221 | 229 | | |
222 | 230 | | |
223 | | - | |
224 | | - | |
225 | | - | |
226 | | - | |
227 | | - | |
228 | | - | |
229 | | - | |
230 | | - | |
231 | | - | |
| 231 | + | |
| 232 | + | |
| 233 | + | |
| 234 | + | |
| 235 | + | |
| 236 | + | |
| 237 | + | |
| 238 | + | |
| 239 | + | |
| 240 | + | |
232 | 241 | | |
233 | 242 | | |
234 | 243 | | |
235 | 244 | | |
236 | 245 | | |
237 | 246 | | |
238 | 247 | | |
239 | | - | |
240 | | - | |
241 | | - | |
242 | | - | |
| 248 | + | |
| 249 | + | |
| 250 | + | |
| 251 | + | |
| 252 | + | |
| 253 | + | |
| 254 | + | |
| 255 | + | |
| 256 | + | |
| 257 | + | |
| 258 | + | |
243 | 259 | | |
244 | | - | |
| 260 | + | |
245 | 261 | | |
246 | 262 | | |
247 | 263 | | |
| |||
0 commit comments