⚡️ Speed up method StringUtils.formatUriForPropertiesFile by 265%#54
Open
codeflash-ai[bot] wants to merge 1 commit intomainfrom
Open
Conversation
The optimized code pre-compiles the regex pattern `(?<!\\)://` into a static final field `URI_PROTOCOL_PATTERN` instead of recompiling it on every call to `replaceAll()`. This eliminates per-invocation regex compilation overhead, reducing function runtime from 6.92ms to 1.90ms (264% speedup). Line profiler data shows per-hit cost remains ~10ms but the optimization cuts total time by ~20ms across all hits. Two null-handling test cases regressed by 8-19% (adding ~1-2μs), likely due to slightly different exception-throwing paths when using `Matcher.replaceAll()` versus `String.replaceAll()`, but this is negligible compared to the overall runtime improvement.
HeshamHM28
approved these changes
Apr 1, 2026
Owner
HeshamHM28
left a comment
There was a problem hiding this comment.
Optimization Verification Report
Method: StringUtils.formatUriForPropertiesFile
Tests: PASS (StringUtilsTest)
Benchmark Result: 48.1% speedup (1,049ms → 544ms per 500K iterations)
Verification
- All tests pass, significant speedup confirmed
- Pre-compiling the regex
(?<!\\\\)://into a staticPatternfield avoids repeatedPattern.compile()on every call String.replaceAll()compiles the pattern each time — this is a well-known performance antipattern- The pre-compiled pattern is thread-safe and reusable
Verdict: APPROVED — Classic and significant optimization. Nearly 2x faster.
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.
📄 265% (2.65x) speedup for
StringUtils.formatUriForPropertiesFileinrewrite-core/src/main/java/org/openrewrite/internal/StringUtils.java⏱️ Runtime :
6.92 milliseconds→1.90 milliseconds(best of109runs)📝 Explanation and details
The optimized code pre-compiles the regex pattern
(?<!\\)://into a static final fieldURI_PROTOCOL_PATTERNinstead of recompiling it on every call toreplaceAll(). This eliminates per-invocation regex compilation overhead, reducing function runtime from 6.92ms to 1.90ms (264% speedup). Line profiler data shows per-hit cost remains ~10ms but the optimization cuts total time by ~20ms across all hits. Two null-handling test cases regressed by 8-19% (adding ~1-2μs), likely due to slightly different exception-throwing paths when usingMatcher.replaceAll()versusString.replaceAll(), but this is negligible compared to the overall runtime improvement.✅ Correctness verification report:
🌀 Click to see Generated Regression Tests
To edit these changes
git checkout codeflash/optimize-StringUtils.formatUriForPropertiesFile-mmphe0mqand push.