⚡️ Speed up method InitDecorator.visit_Module by 12% in PR #1860 (fix/attrs-init-instrumentation)#1866
Conversation
The optimization pre-parses the `codeflash_capture` import statement once in `__init__` and stores it in `self._import_stmt`, eliminating the repeated `ast.parse` call inside `visit_Module`. Line profiler confirms the original code spent ~186 µs (1% of runtime) parsing the import on every module visit (11 hits × 16.9 µs each), which is now reduced to a one-time ~8 µs insertion cost. This reduces total `visit_Module` time by ~2.6% (17.87 ms → 17.41 ms) with no correctness trade-offs, preserving all AST structure and behavior across diverse test scenarios including large modules with 100+ classes.
|
Claude finished @codeflash-ai[bot]'s task in 19s —— View job PR Review SummaryTasks
Prek Checks✅ Code ReviewThe optimization is correct and safe. What it does: Moves Why it's safe: Minor note: The comment on line 135 says "to avoid repeated parsing in visit_Module" — but since each instance only calls No bugs, no security issues, no breaking API changes. Duplicate DetectionNo duplicates detected. The Test CoveragePR reports 100% coverage from 40 generated regression tests. CI checks ( Last updated: 2026-03-18T10:32Z |
46016bd
into
fix/attrs-init-instrumentation
⚡️ This pull request contains optimizations for PR #1860
If you approve this dependent PR, these changes will be merged into the original PR branch
fix/attrs-init-instrumentation.📄 12% (0.12x) speedup for
InitDecorator.visit_Moduleincodeflash/languages/python/instrument_codeflash_capture.py⏱️ Runtime :
376 microseconds→336 microseconds(best of137runs)📝 Explanation and details
The optimization pre-parses the
codeflash_captureimport statement once in__init__and stores it inself._import_stmt, eliminating the repeatedast.parsecall insidevisit_Module. Line profiler confirms the original code spent ~186 µs (1% of runtime) parsing the import on every module visit (11 hits × 16.9 µs each), which is now reduced to a one-time ~8 µs insertion cost. This reduces totalvisit_Moduletime by ~2.6% (17.87 ms → 17.41 ms) with no correctness trade-offs, preserving all AST structure and behavior across diverse test scenarios including large modules with 100+ classes.✅ Correctness verification report:
🌀 Click to see Generated Regression Tests
To edit these changes
git checkout codeflash/optimize-pr1860-2026-03-18T10.30.36and push.