ext/standard: Optimize str_pad() using doubling copies - #23661
Merged
Merged
Conversation
devnexen
approved these changes
Sep 12, 2026
devnexen
left a comment
Member
There was a problem hiding this comment.
I think it s correct. nice follow-up for you is applying the same sort of optimisation in mbstring (mb_str_pad)
Member
Author
|
Yeah here are some benchmark results if anyone is curious (str_pad):
|
LamentXU123
added a commit
that referenced
this pull request
Sep 12, 2026
Follow-up #23661. Use the same optimization on mb_str_pad. Co-authored-by: David CARLIER <devnexen@gmail.com>
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.
I was reading standard code recently. The implementation of
str_padhere is quite old that we make a loop to pad the strings. The loop goes on if the remaining padding length is smaller than padded-string's length. In modern implementations like OpenJDK for example, we use a doubling algo for this: https://github.com/openjdk/jdk/blob/jdk-21%2B35/src/java.base/share/classes/java/lang/String.java#L4682 This is way more faster than the original one. Let's say we want to pad "ab" for 1 MiB. With the original one we need ~52,000 times of memory copying but with this implementation we only need 20 times.