⚡️ Speed up function _add_timing_instrumentation by 16% in PR #1580 (fix/java-direct-jvm-and-bugs)#1595
Merged
claude[bot] merged 2 commits intofix/java-direct-jvm-and-bugsfrom Feb 20, 2026
Conversation
This optimization achieves a **15% runtime improvement** (10.2ms → 8.81ms) by replacing recursive AST traversal with iterative stack-based traversal in two critical functions: `collect_test_methods` and `collect_target_calls`. ## Key Changes **1. Iterative AST Traversal (Primary Speedup)** - Replaced recursive tree walking with explicit stack-based iteration - In `collect_test_methods`: Changed from recursive calls to `while stack` loop with `stack.extend(reversed(current.children))` - In `collect_target_calls`: Similar transformation using explicit stack management - **Impact**: Line profiler shows `collect_test_methods` dropped from 24.2% to 3.8% of total runtime (81% reduction in that function) **2. Why This Works in Python** - Python function calls have significant overhead (frame creation, argument binding, scope setup) - Recursive traversal compounds this overhead across potentially deep AST trees - Iterative approach uses a simple list for the stack, avoiding repeated function call overhead - The `reversed()` call ensures children are processed in the same order as recursive traversal, preserving correctness **3. Performance Characteristics** Based on annotated tests: - **Large method bodies** (500+ lines): 23.8% faster - most benefit from reduced recursion overhead - **Many test methods** (100 methods): 9.2% faster - cumulative savings across many traversals - **Simple cases**: 2-5% faster - overhead reduction still measurable - **Empty/no-match cases**: Minor regression (8-9% slower) due to negligible baseline times (12-40μs) ## Impact on Workloads The function references show `_add_timing_instrumentation` is called from test instrumentation code. This optimization particularly benefits: - **Java projects with large test suites** containing many `@Test` methods - **Complex test methods** with deep AST structures and multiple method invocations - **Batch instrumentation operations** where the function is called repeatedly The iterative approach scales better than recursion as AST depth and method count increase, making it especially valuable for large Java codebases where instrumentation is applied across hundreds of test methods.
Contributor
PR Review SummaryPrek Checks✅ Passed — Auto-fixed 2 extra blank lines in Mypy✅ Passed — No type errors in Code Review✅ No critical issues found. The change converts
Test Coverage
Changed lines (729-753): 24/25 lines covered ✅
Overall project coverage: 79% (49,207 / 61,967 statements) Last updated: 2026-02-20 |
ae1c03d
into
fix/java-direct-jvm-and-bugs
23 of 30 checks passed
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
⚡️ This pull request contains optimizations for PR #1580
If you approve this dependent PR, these changes will be merged into the original PR branch
fix/java-direct-jvm-and-bugs.📄 16% (0.16x) speedup for
_add_timing_instrumentationincodeflash/languages/java/instrumentation.py⏱️ Runtime :
10.2 milliseconds→8.81 milliseconds(best of250runs)📝 Explanation and details
This optimization achieves a 15% runtime improvement (10.2ms → 8.81ms) by replacing recursive AST traversal with iterative stack-based traversal in two critical functions:
collect_test_methodsandcollect_target_calls.Key Changes
1. Iterative AST Traversal (Primary Speedup)
collect_test_methods: Changed from recursive calls towhile stackloop withstack.extend(reversed(current.children))collect_target_calls: Similar transformation using explicit stack managementcollect_test_methodsdropped from 24.2% to 3.8% of total runtime (81% reduction in that function)2. Why This Works in Python
reversed()call ensures children are processed in the same order as recursive traversal, preserving correctness3. Performance Characteristics
Based on annotated tests:
Impact on Workloads
The function references show
_add_timing_instrumentationis called from test instrumentation code. This optimization particularly benefits:@TestmethodsThe iterative approach scales better than recursion as AST depth and method count increase, making it especially valuable for large Java codebases where instrumentation is applied across hundreds of test methods.
✅ Correctness verification report:
🌀 Click to see Generated Regression Tests
To edit these changes
git checkout codeflash/optimize-pr1580-2026-02-20T09.26.51and push.