[php-src] PR #23661: ext/standard: Optimize str_pad() using doubling copies
| From: | LamentXU123 | Date: | Fri, 11 Sep 2026 09:45:45 +0000 |
| Subject: | [php-src] PR #23661: ext/standard: Optimize str_pad() using doubling copies | ||
| Groups: | php.git-pulls | ||
| Request: | Send a blank email to git-pulls+get-38440@lists.php.net to get a copy of this message | ||
Pull Request: https://github.com/php/php-src/pull/23661
Author: LamentXU123
I was reading standard code recently. The implementation of
str_pad here is quite old
that we make a loop to pad the strings 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.