⚡️ Speed up function _add_java_class_members by 48% in PR #1199 (omni-java)#1368
Open
codeflash-ai[bot] wants to merge 1 commit intoomni-javafrom
Open
⚡️ Speed up function _add_java_class_members by 48% in PR #1199 (omni-java)#1368codeflash-ai[bot] wants to merge 1 commit intoomni-javafrom
_add_java_class_members by 48% in PR #1199 (omni-java)#1368codeflash-ai[bot] wants to merge 1 commit intoomni-javafrom
Conversation
The optimized code achieves a **47% runtime improvement** (from 47.3ms to 32.0ms) by eliminating redundant parsing operations through two key optimizations: ## What Changed ### 1. Parse Result Caching in JavaAnalyzer Added a `_tree_cache` dictionary and `_get_cached_tree()` method that caches parsed tree-sitter trees by source content. This prevents reparsing identical source code when `find_methods()`, `find_classes()`, or `find_fields()` are called multiple times on the same source within a single analyzer instance. ### 2. Elimination of Repeated Reparsing in `_insert_class_members()` The original implementation reparsed the source after inserting fields to get updated byte positions for method insertion. The optimized version: - Computes both insertion points (fields and methods) from the **original** parse tree - Tracks a `delta` offset as bytes are inserted - Adjusts subsequent insertion points by the accumulated delta - Uses list-based string building (`field_parts`, `method_parts`) instead of repeated concatenation - Works entirely in bytes until the final decode, avoiding multiple encode/decode cycles ## Why It's Faster **Parse elimination is the key win**: Line profiler shows parsing (`self.parse()`) consumed 10.8-28.2% of time in various methods in the original. By caching parse results, the optimized code avoids ~60% of these expensive tree-sitter operations when the same source is analyzed multiple times (e.g., `_add_java_class_members` calls `find_classes()` on `original_source` twice and `optimized_code` up to three times). **Delta-based insertion tracking**: The original code's repeated reparsing after field insertion (`classes = analyzer.find_classes(result)` taking 14.2% and 28.2% of `_insert_class_members` time) is completely eliminated. The optimized version calculates both insertion points upfront and adjusts them arithmetically. **String operation efficiency**: Using list accumulation for building field/method text reduces repeated string concatenation overhead, though this is a minor contributor compared to parse elimination. ## Test Case Performance The optimization shows consistent 25-55% speedups across test cases: - **Basic operations**: 35-50% faster (e.g., `test_basic_adds_new_static_field_when_missing`: 38% faster) - **Large scale**: 30-108% faster (e.g., `test_large_scale_many_fields_and_methods`: 37.8% faster, `test_performance_with_large_original_source`: 108% faster due to eliminating repeated parsing of large ASTs) - **No performance regressions** on meaningful workloads (edge cases with invalid/empty inputs show negligible 1-5% variations due to measurement noise) ## Impact Context This optimization is valuable when Java source code analysis involves repeated queries on the same source content, particularly in code transformation pipelines where multiple analyses (classes, methods, fields) are performed on both original and optimized versions of the same code.
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 #1199
If you approve this dependent PR, these changes will be merged into the original PR branch
omni-java.📄 48% (0.48x) speedup for
_add_java_class_membersincodeflash/code_utils/code_replacer.py⏱️ Runtime :
47.3 milliseconds→32.0 milliseconds(best of82runs)📝 Explanation and details
The optimized code achieves a 47% runtime improvement (from 47.3ms to 32.0ms) by eliminating redundant parsing operations through two key optimizations:
What Changed
1. Parse Result Caching in JavaAnalyzer
Added a
_tree_cachedictionary and_get_cached_tree()method that caches parsed tree-sitter trees by source content. This prevents reparsing identical source code whenfind_methods(),find_classes(), orfind_fields()are called multiple times on the same source within a single analyzer instance.2. Elimination of Repeated Reparsing in
_insert_class_members()The original implementation reparsed the source after inserting fields to get updated byte positions for method insertion. The optimized version:
deltaoffset as bytes are insertedfield_parts,method_parts) instead of repeated concatenationWhy It's Faster
Parse elimination is the key win: Line profiler shows parsing (
self.parse()) consumed 10.8-28.2% of time in various methods in the original. By caching parse results, the optimized code avoids ~60% of these expensive tree-sitter operations when the same source is analyzed multiple times (e.g.,_add_java_class_memberscallsfind_classes()onoriginal_sourcetwice andoptimized_codeup to three times).Delta-based insertion tracking: The original code's repeated reparsing after field insertion (
classes = analyzer.find_classes(result)taking 14.2% and 28.2% of_insert_class_memberstime) is completely eliminated. The optimized version calculates both insertion points upfront and adjusts them arithmetically.String operation efficiency: Using list accumulation for building field/method text reduces repeated string concatenation overhead, though this is a minor contributor compared to parse elimination.
Test Case Performance
The optimization shows consistent 25-55% speedups across test cases:
test_basic_adds_new_static_field_when_missing: 38% faster)test_large_scale_many_fields_and_methods: 37.8% faster,test_performance_with_large_original_source: 108% faster due to eliminating repeated parsing of large ASTs)Impact Context
This optimization is valuable when Java source code analysis involves repeated queries on the same source content, particularly in code transformation pipelines where multiple analyses (classes, methods, fields) are performed on both original and optimized versions of the same code.
✅ Correctness verification report:
🌀 Click to see Generated Regression Tests
To edit these changes
git checkout codeflash/optimize-pr1199-2026-02-04T04.23.41and push.