⚡️ Speed up method JavaImportResolver._is_external_library by 148% in PR #1199 (omni-java)
#1372
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.📄 148% (1.48x) speedup for
JavaImportResolver._is_external_libraryincodeflash/languages/java/import_resolver.py⏱️ Runtime :
584 microseconds→236 microseconds(best of177runs)📝 Explanation and details
The optimized code achieves a 147% speedup (from 584μs to 236μs) by introducing two key optimizations:
1. Caching Previously-Seen Results
The optimization adds
self._external_library_cache: dict[str, bool]to memoize results of previous_is_external_librarycalls. This is particularly effective because:2. Set-Based Membership Tests Instead of Linear String Scanning
The original code used
for prefix in self.COMMON_EXTERNAL_PREFIXESwithstartswith()checks, performing linear iteration and string concatenation (prefix + ".") on every call. The optimization:COMMON_EXTERNAL_PREFIXEStofrozensetfor O(1) membership tests"org"→"org.apache"→"org.apache.commons") and checks each against the setprefix + "."operation consumed 61.7% of total time)Performance Characteristics by Test Case:
The optimization is especially valuable in real-world scenarios where:
The trade-off is minimal: slightly increased memory usage for the cache (proportional to unique imports seen) and marginal first-call overhead for deeply nested external packages, both negligible compared to the massive gains in repeated-check scenarios.
✅ Correctness verification report:
🌀 Click to see Generated Regression Tests
To edit these changes
git checkout codeflash/optimize-pr1199-2026-02-04T05.47.50and push.