⚡️ Speed up function _add_timing_instrumentation by 21% in PR #1390 (feat/kryo-serialization)
#1406
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 #1390
If you approve this dependent PR, these changes will be merged into the original PR branch
feat/kryo-serialization.📄 21% (0.21x) speedup for
_add_timing_instrumentationincodeflash/languages/java/instrumentation.py⏱️ Runtime :
3.17 milliseconds→2.61 milliseconds(best of245runs)📝 Explanation and details
This optimization achieves a 21% runtime improvement by eliminating expensive character-by-character iteration when counting braces in Java method bodies.
Key Performance Changes:
Replaced per-character loop with
str.count()calls: The original code iterated through every character in each line to count opening and closing braces, which consumed 35% of total runtime (18% + 17% from line profiler). The optimized version uses Python's built-instr.count("{")andstr.count("}")methods, which are implemented in C and run significantly faster.Direct appending eliminates intermediate list: The original code collected method body lines in a
body_lineslist, then later iterated through it again to append lines with indentation to the result. The optimized version directly appends each line toresultas it's processed, removing both the intermediate storage and the second iteration.Cached list length and
lstrip()calls: Storingn = len(lines)upfront avoids repeated length calculations in tight loops (1,071 iterations), and storinglstripped = line.lstrip()avoids redundant string operations.Why This Matters:
Based on the function references,
_add_timing_instrumentationis called from test instrumentation workflows that process Java test classes. The test results show consistent 10-30% speedups across various scenarios:The optimization is particularly effective for:
Since this function processes every
@Testmethod in Java source files during instrumentation, reducing its runtime by 21% provides compounding benefits in workflows that instrument multiple test classes or run in tight optimization loops.✅ Correctness verification report:
🌀 Click to see Generated Regression Tests
To edit these changes
git checkout codeflash/optimize-pr1390-2026-02-06T19.01.20and push.