Commit 5eaadc2
authored
Optimize ensure_common_java_imports
Primary benefit — runtime improved from 64.3 ms to 10.7 ms (≈501% speedup). The optimized version was accepted because it materially reduces execution time, especially on the large and repeated-call workloads exercised in the tests.
What changed (specific optimizations)
- Substring pre-check before regex: the code first checks "if class_name not in code" using the fast C-level substring search, and only runs re.search(rf"\b{class_name}\b", ...) when the substring is present. This avoids dozens of expensive regex runs for names that aren’t even present.
- Batch import insertion: missing imports are collected into a list and inserted with a single _add_imports call that does one splitlines/join and one insertion of a block, instead of calling _add_import repeatedly.
- Minor local-variable rename (code = test_code) to avoid repeated attribute lookups and to make the flow clearer.
Why this yields the speedup
- Regex cost dominated the original function: the line profiler shows the re.search line consumed ~82% of the original function time. re.search with a \b word-boundary involves regex engine work and is substantially slower than a plain substring search.
- The substring test ("in") is implemented in C and is much cheaper; it quickly filters out most class names so the regex runs only for likely candidates. In the optimized profile the heavy regex line's relative cost dropped dramatically.
- Repeatedly calling _add_import caused repeated splitlines/join on the entire source for each added import (O(n) per insertion => O(k*n) when adding k imports). The new _add_imports builds the insertion block and performs a single split/join, giving roughly O(n + k) instead of O(k * n) behavior for that part.
- The profiler confirms these effects: total time for ensure_common_java_imports dropped from 0.105s to 0.027s; the number and cost of expensive operations (regex and split/join) fell accordingly.
Behavioral and compatibility notes
- Behavior is preserved: imports are still only added when needed, wildcard-package checks remain, and word-boundary checks are still performed (the regex is executed when necessary).
- No regressions in correctness were introduced by the changes; tests show identical behavior and faster runs.
- Memory and complexity trade-off: storing a small list of import statements is negligible; batching reduces overall CPU and memory churn.
When this helps most
- Large inputs or code with many references, and repeated calls (hot paths) benefit the most. The large-scale test shows a dramatic improvement (e.g., the 1000-iteration idempotent test went from ~31.5 ms to ~3.39 ms in one recorded case).
- Even small test cases became noticeably faster (microsecond-level improvements across the test suite), and empty inputs are sped up because cheap substring checks short-circuit further work.
Summary
- The optimization targets the two main costs: (1) many unnecessary regex searches and (2) repeated O(n) string recompositions when inserting imports. Replacing frequent regex invocations with a cheap substring pre-check and batching insertions cuts both CPU and memory work, producing the measured 501% runtime improvement without changing behavior.1 parent 15ade96 commit 5eaadc2
1 file changed
Lines changed: 65 additions & 5 deletions
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
| |||
1277 | 1277 | | |
1278 | 1278 | | |
1279 | 1279 | | |
| 1280 | + | |
| 1281 | + | |
| 1282 | + | |
1280 | 1283 | | |
1281 | | - | |
| 1284 | + | |
1282 | 1285 | | |
1283 | | - | |
| 1286 | + | |
1284 | 1287 | | |
1285 | 1288 | | |
1286 | | - | |
| 1289 | + | |
1287 | 1290 | | |
1288 | | - | |
1289 | | - | |
| 1291 | + | |
| 1292 | + | |
| 1293 | + | |
| 1294 | + | |
| 1295 | + | |
| 1296 | + | |
| 1297 | + | |
| 1298 | + | |
1290 | 1299 | | |
1291 | 1300 | | |
1292 | 1301 | | |
| |||
1384 | 1393 | | |
1385 | 1394 | | |
1386 | 1395 | | |
| 1396 | + | |
| 1397 | + | |
| 1398 | + | |
| 1399 | + | |
| 1400 | + | |
| 1401 | + | |
| 1402 | + | |
| 1403 | + | |
| 1404 | + | |
| 1405 | + | |
| 1406 | + | |
| 1407 | + | |
| 1408 | + | |
| 1409 | + | |
| 1410 | + | |
| 1411 | + | |
| 1412 | + | |
| 1413 | + | |
| 1414 | + | |
| 1415 | + | |
| 1416 | + | |
| 1417 | + | |
| 1418 | + | |
| 1419 | + | |
| 1420 | + | |
| 1421 | + | |
| 1422 | + | |
| 1423 | + | |
| 1424 | + | |
| 1425 | + | |
| 1426 | + | |
| 1427 | + | |
| 1428 | + | |
| 1429 | + | |
| 1430 | + | |
| 1431 | + | |
| 1432 | + | |
| 1433 | + | |
| 1434 | + | |
| 1435 | + | |
| 1436 | + | |
| 1437 | + | |
| 1438 | + | |
| 1439 | + | |
| 1440 | + | |
| 1441 | + | |
| 1442 | + | |
| 1443 | + | |
| 1444 | + | |
| 1445 | + | |
| 1446 | + | |
0 commit comments