⚡️ Speed up method StringUtils.isNumeric by 390%#8
Open
codeflash-ai[bot] wants to merge 1 commit intomainfrom
Open
⚡️ Speed up method StringUtils.isNumeric by 390%#8codeflash-ai[bot] wants to merge 1 commit intomainfrom
StringUtils.isNumeric by 390%#8codeflash-ai[bot] wants to merge 1 commit intomainfrom
Conversation
The optimization added a fast-path that checks `c >= '0' && c <= '9'` before calling `Character.isDigit(c)`, allowing ASCII digits (the overwhelmingly common case) to bypass the expensive Unicode classification logic. Line profiler data shows the original spent 50.6% of time in `Character.isDigit`, while the optimized version distributes that cost across cheaper comparison checks (25.2% each for `charAt` and the ASCII range test), reducing per-call overhead. Unicode digits (Arabic-Indic, fullwidth) still fall through to `Character.isDigit`, preserving correctness. The 389% speedup comes from avoiding Unicode table lookups for typical inputs.
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.
📄 390% (3.90x) speedup for
StringUtils.isNumericinrewrite-core/src/main/java/org/openrewrite/internal/StringUtils.java⏱️ Runtime :
2.26 milliseconds→461 microseconds(best of129runs)📝 Explanation and details
The optimization added a fast-path that checks
c >= '0' && c <= '9'before callingCharacter.isDigit(c), allowing ASCII digits (the overwhelmingly common case) to bypass the expensive Unicode classification logic. Line profiler data shows the original spent 50.6% of time inCharacter.isDigit, while the optimized version distributes that cost across cheaper comparison checks (25.2% each forcharAtand the ASCII range test), reducing per-call overhead. Unicode digits (Arabic-Indic, fullwidth) still fall through toCharacter.isDigit, preserving correctness. The 389% speedup comes from avoiding Unicode table lookups for typical inputs.✅ Correctness verification report:
🌀 Click to see Generated Regression Tests
To edit these changes
git checkout codeflash/optimize-StringUtils.isNumeric-mnn9kkuzand push.