String: optimize gsub and tr for ascii char and single byte string #4978
+36
−1
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.
Fixes #4586
The main performance benefit is that when replacing a single byte char with another single byte char the new string bytesize is known to be the same as the current one, so we can just allocate that. The general
gsub
implementation uses aString::Builder
with an initial approximate capacity.Here I also check for single byte strings and invoke the same method (different overload) with Char, which should be more efficient in general.
I used this as a benchmark:
Old output:
New output:
(the last two entries are just there to see that those checks don't ruin the performance when the optimization can't be applied)