⚡️ Speed up function _extract_class_declaration by 29% in PR #1199 (omni-java)
#1371
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.📄 29% (0.29x) speedup for
_extract_class_declarationincodeflash/languages/java/context.py⏱️ Runtime :
236 microseconds→183 microseconds(best of18runs)📝 Explanation and details
The optimized code achieves a 28% runtime improvement (from 236μs to 183μs) through two key optimizations:
1. Module-Level Body Type Mapping (7-14% of gains)
The
body_typesdictionary is moved from a local variable recreated on every function call to a module-level constant_BODY_TYPES. This eliminates the overhead of dictionary construction for each invocation, saving ~7-14 microseconds per call based on line profiler data (7655ns reduced to effectively zero allocation cost).2. Deferred UTF-8 Decoding (Primary optimization: 70-85% of gains)
Instead of decoding each child node's bytes individually in the loop:
decode("utf8")called ~1048 times per execution)b" ".join(), then performs a single decode at the endThe line profiler shows the impact:
decode("utf8")takes 524μs (35.7% of total time)This reduces the UTF-8 decoding overhead from ~35% to ~3% of execution time because:
Test Results Validation
The optimization excels particularly in scenarios with many children nodes:
This optimization is especially valuable for Java code analysis workloads that parse complex class hierarchies with many modifiers, generics, annotations, or deeply nested type declarations.
✅ Correctness verification report:
🌀 Click to see Generated Regression Tests
To edit these changes
git checkout codeflash/optimize-pr1199-2026-02-04T05.05.57and push.