⚡️ Speed up method StringUtils.aspectjNameToPattern by 25%#9
Open
codeflash-ai[bot] wants to merge 1 commit intomainfrom
Open
⚡️ Speed up method StringUtils.aspectjNameToPattern by 25%#9codeflash-ai[bot] wants to merge 1 commit intomainfrom
StringUtils.aspectjNameToPattern by 25%#9codeflash-ai[bot] wants to merge 1 commit intomainfrom
Conversation
The optimized code caches `name.charAt(i + 1)` in a local variable `next` at the start of each iteration instead of calling it twice inside the conditional, and pre-sizes the `StringBuilder` to `Math.max(16, length * 2)` to accommodate common expansions (e.g. '.' → "[.$]", '*' → "[^.]*") without triggering internal resizing. Profiler data shows the hot loop (72.6% of original runtime on `name.length()` and iteration logic) benefits from eliminating repeated bounds checks and reducing allocation overhead, achieving a 24% runtime improvement (775 µs → 623 µs). The optimization introduces a negligible per-iteration cost for the `next` assignment but this is more than offset by removing redundant method calls.
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.
📄 25% (0.25x) speedup for
StringUtils.aspectjNameToPatterninrewrite-core/src/main/java/org/openrewrite/internal/StringUtils.java⏱️ Runtime :
775 microseconds→623 microseconds(best of139runs)📝 Explanation and details
The optimized code caches
name.charAt(i + 1)in a local variablenextat the start of each iteration instead of calling it twice inside the conditional, and pre-sizes theStringBuildertoMath.max(16, length * 2)to accommodate common expansions (e.g. '.' → "[.$]", '' → "[^.]") without triggering internal resizing. Profiler data shows the hot loop (72.6% of original runtime onname.length()and iteration logic) benefits from eliminating repeated bounds checks and reducing allocation overhead, achieving a 24% runtime improvement (775 µs → 623 µs). The optimization introduces a negligible per-iteration cost for thenextassignment but this is more than offset by removing redundant method calls.✅ Correctness verification report:
🌀 Click to see Generated Regression Tests
To edit these changes
git checkout codeflash/optimize-StringUtils.aspectjNameToPattern-mnn9wbydand push.