⚡️ Speed up function _add_global_declarations_for_language by 103% in PR #1199 (omni-java)#1284
Closed
codeflash-ai[bot] wants to merge 1 commit intoomni-javafrom
Closed
Conversation
The optimized code achieves a **102% speedup** (from 409ms to 202ms) by eliminating redundant tree-sitter parsing operations when inserting multiple declarations. **Key optimization:** In the original code, after inserting each new declaration, the entire source was re-parsed via `analyzer.find_module_level_declarations(result)` to update line numbers. With many declarations (e.g., 100+ in test scenarios), this caused quadratic behavior—each insertion triggered a full parse of increasingly larger source code. The optimization introduces `_insert_declaration_after_dependencies_fast()`, which returns not just the modified source but also metadata about the insertion: the insertion line and number of lines added. Instead of re-parsing, the code now updates the `existing_decl_end_lines` dictionary incrementally by: 1. Shifting end lines of declarations appearing after the insertion point 2. Recording the newly inserted declaration's end line directly This transforms O(n²) parse operations into O(n) dictionary updates, where n is the number of declarations. **Performance gains by test category:** - **Dependency chains** (100 declarations): 1326% faster (37.2ms → 2.61ms) - **Independent declarations** (100 items): 88.3% faster (61.3ms → 32.6ms) - **Wide dependency graphs** (100 items): 1291% faster (42.2ms → 3.03ms) - **Simple cases** (1-3 declarations): 15-25% faster The optimization is most impactful when inserting many declarations with dependencies—precisely the scenario where re-parsing becomes expensive. For codebases with optimized code introducing numerous helper constants or utility declarations, this eliminates a major performance bottleneck while maintaining identical correctness.
Collaborator
|
Closing stale bot PR. |
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.📄 103% (1.03x) speedup for
_add_global_declarations_for_languageincodeflash/code_utils/code_replacer.py⏱️ Runtime :
409 milliseconds→202 milliseconds(best of20runs)📝 Explanation and details
The optimized code achieves a 102% speedup (from 409ms to 202ms) by eliminating redundant tree-sitter parsing operations when inserting multiple declarations.
Key optimization: In the original code, after inserting each new declaration, the entire source was re-parsed via
analyzer.find_module_level_declarations(result)to update line numbers. With many declarations (e.g., 100+ in test scenarios), this caused quadratic behavior—each insertion triggered a full parse of increasingly larger source code.The optimization introduces
_insert_declaration_after_dependencies_fast(), which returns not just the modified source but also metadata about the insertion: the insertion line and number of lines added. Instead of re-parsing, the code now updates theexisting_decl_end_linesdictionary incrementally by:This transforms O(n²) parse operations into O(n) dictionary updates, where n is the number of declarations.
Performance gains by test category:
The optimization is most impactful when inserting many declarations with dependencies—precisely the scenario where re-parsing becomes expensive. For codebases with optimized code introducing numerous helper constants or utility declarations, this eliminates a major performance bottleneck while maintaining identical correctness.
✅ Correctness verification report:
🌀 Click to see Generated Regression Tests
To edit these changes
git checkout codeflash/optimize-pr1199-2026-02-03T03.34.45and push.