⚡️ Speed up function _get_parent_type_name by 12% in PR #1199 (omni-java)
#1249
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.📄 12% (0.12x) speedup for
_get_parent_type_nameincodeflash/languages/java/context.py⏱️ Runtime :
151 microseconds→135 microseconds(best of250runs)📝 Explanation and details
The optimization achieves an 11% runtime improvement by eliminating repeated tuple construction during membership testing.
Key Change:
The original code uses
parent.type in ("ClassDef", "InterfaceDef", "EnumDef")which creates a new tuple on every iteration of the parent loop. The optimized version replaces this with a module-level constant_PARENT_TYPE_NAMES = frozenset(("ClassDef", "InterfaceDef", "EnumDef")).Why This Is Faster:
frozensetuses hash-based O(1) lookups vs. tuple's O(n) linear scan, though with only 3 elements this difference is minimal.Performance Profile:
Test Results Show:
The line profiler confirms the hot spot is the membership test line (20.9% of total time), making this micro-optimization worthwhile for a function that may be called frequently during code analysis workflows.
✅ Correctness verification report:
🌀 Click to see Generated Regression Tests
To edit these changes
git checkout codeflash/optimize-pr1199-2026-02-02T00.11.48and push.