Skip to content

ext/standard: Avoid redundant searches in str_replace() - #25851

Open
LamentXU123 wants to merge 1 commit into
php:masterfrom
LamentXU123:opt1128
Open

LamentXU123 wants to merge 1 commit into
php:masterfrom
LamentXU123:opt1128

Conversation

@LamentXU123

@LamentXU123 LamentXU123 commented Nov 27, 2026 •

Copy link
Copy Markdown
Member

When the search string is longer than one byte and its replacement has a different length, str_replace() counts matches before allocating the result.

Two things:

  • We can reuse the first match from that pass
  • We can stop searching once all counted matches have been copied.

Err, so now when the search string occurs once, the copy pass performs no substring searches.

<?php
$half = str_repeat('a', 2044);
$subject = $half . '{{name}}' . $half;

$start = hrtime(true);
for ($i = 0; $i < 1_000_000; ++$i) {
    $result = str_replace('{{name}}', 'Alice', $subject);
}
printf("%.1f ms\n", (hrtime(true) - $start) / 1e6);

Before this PR the call average use 139.0 ms, after this it use only 96.1 ms roughly 30.86% increasement. Also this can cause some reducsion in memory consumption as well.

I will look at the performance PR later on but not now. Today I spent all my open source time on this stuff. It is really very interesting to do these optimizations :) I am happy to see such improvement on such a critical function.

@github-actions

This comment was marked as outdated.

@github-actions

Copy link
Copy Markdown

AWS x86_64 (c6id.metal)

Attribute Value
Environment aws
Instance type c6id.metal
Architecture x86_64
CPU Intel(R) Xeon(R) Platinum 8375C CPU @ 2.90GHz, 64 cores @ 2900 MHz
CPU settings disabled deeper C-states, disabled turbo boost, disabled hyper-threading
RAM 251 GB
Kernel 6.18.56-125.164.amzn2023.x86_64
OS Amazon Linux 2023.12.20261026
GCC 14.2.1
Binary layout strategy none
Time 2026-11-28 08:29:00 UTC
Job details https://github.com/php/php-src/actions/runs/46125992759 (Artifacts)
Changeset https://github.com/php/php-src/compare/8904d87f04..6c8cd78cd0

Laravel 12.11.0 demo app - 50 iterations, 50 warmups, 100 requests (sec)

PHP Min Max Std dev Rel std dev % Mean Mean diff % Median Median diff % Skewness Z-stat P-value Memory
PHP - baseline@8904d87 (NTS) 0.37902 0.38308 0.00058 0.15% 0.37938 0.00% 0.37928 0.00% 5.691 0.000 1.000 25.78 MB
PHP - opt1128 (NTS) 0.37845 0.37950 0.00018 0.05% 0.37873 -0.17% 0.37869 -0.15% 1.903 8.304 0.000 25.79 MB

Symfony 2.8.0 demo app - 50 iterations, 50 warmups, 100 requests (sec)

PHP Min Max Std dev Rel std dev % Mean Mean diff % Median Median diff % Skewness Z-stat P-value Memory
PHP - baseline@8904d87 (NTS) 0.68221 0.69330 0.00159 0.23% 0.68355 0.00% 0.68332 0.00% 4.844 0.000 1.000 26.32 MB
PHP - opt1128 (NTS) 0.67977 0.69020 0.00145 0.21% 0.68062 -0.43% 0.68035 -0.43% 6.087 8.276 0.000 26.35 MB

Wordpress 6.9 main page - 50 iterations, 20 warmups, 20 requests (sec)

PHP Min Max Std dev Rel std dev % Mean Mean diff % Median Median diff % Skewness Z-stat P-value Memory
PHP - baseline@8904d87 (NTS) 0.58916 0.59185 0.00069 0.12% 0.59007 0.00% 0.58981 0.00% 1.458 0.000 1.000 26.34 MB
PHP - opt1128 (NTS) 0.58809 0.59069 0.00049 0.08% 0.58865 -0.24% 0.58855 -0.21% 2.758 8.014 0.000 26.31 MB

bench.php - 50 iterations, 20 warmups, 2 requests (sec)

PHP Min Max Std dev Rel std dev % Mean Mean diff % Median Median diff % Skewness Z-stat P-value Memory
PHP - baseline@8904d87 (NTS) 0.45296 0.45534 0.00054 0.12% 0.45441 0.00% 0.45440 0.00% -0.374 0.000 1.000 26.34 MB
PHP - opt1128 (NTS) 0.45313 0.45824 0.00082 0.18% 0.45433 -0.02% 0.45432 -0.02% 2.246 1.275 0.202 26.31 MB

Comment thread ext/standard/string.c Resolved
Reuse the first match from the counting pass and stop copying matches at the last known match end. When all matches are adjacent, advance directly to the next match.
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants