Repository navigation
Improve performance of StringUtils.replaceEach. - #7494
Merged
Merged
Conversation
## Motivation Improves the performance of `StringUtils.replaceEach` (used in query parameter, path segement and XML escaping), making it linear. ## Changes 1. Remember the next match position of each search string and search for it again only after a replacement has moved past the remembered match. 2. Copy unmatched text between replacements with one bulk append instead of one character at a time. 3. Return the text unchanged when the search list or the replacement list is null. 4. Add a changelog entry for the bugfix. ## Compatibility 1. There is no known compatibility risk. The public API is unchanged and the output is the same as before for every input, including overlapping matches, ties, empty search strings and null replacements. ## Testing New unit tests cover the documented examples, null and empty arguments, the length mismatch exception, overlapping and tied matches, and matches consumed by an earlier replacement. One test runs 20,000 random small inputs through the new code and a copy of the old algorithm and checks that the outputs are equal. Another test replaces 100,000 `&` characters followed by a `<` and checks the result. I ran these tests against the old code, and the large-input timing checks failed there.
ianbotsf
approved these changes
Oct 28, 2026
|
This pull request has been closed and the conversation has been locked. Comments on closed PRs are hard for our team to see. If you need more assistance, please open a new issue that references this one. |
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 subscribe to this conversation on GitHub.
Already have an account?
Sign in.
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.
Motivation
Improves the performance of
StringUtils.replaceEach(used in query parameter, path segement and XML escaping), making it linear.Changes
Compatibility
Testing
New unit tests cover the documented examples, null and empty arguments, the length mismatch exception, overlapping and tied matches, and matches consumed by an earlier replacement. One test runs 20,000 random small inputs through the new code and a copy of the old algorithm and checks that the outputs are equal. Another test replaces 100,000
&characters followed by a<and checks the result. I ran these tests against the old code, and the large-input timing checks failed there.License