[php-src] Issue #24380: Performance of preg_match varies depending on how subject string is initialized

From: Date: Fri, 16 Oct 2026 21:54:27 +0000
Subject: [php-src] Issue #24380: Performance of preg_match varies depending on how subject string is initialized
Groups: php.bugs 
Request: Send a blank email to php-bugs+get-253010@lists.php.net to get a copy of this message
Issue: https://github.com/php/php-src/issues/24380 Author: rr-it ### Description The following code: ```php <?php /* * Run pinned to specific CPU core: * taskset -c 3 php perf-regex-strings.php */ $pattern = '/[&]/u'; // This will not produce any match in test subject. $textA = 'weengahhoghecho1eeSheeG5el9cie5b'; $textB = sprintf('%s', 'weengahhoghecho1eeSheeG5el9cie5b'); $textC = 'weengahhoghecho1eeSheeG5el9cie5b' . 'X'; $textD_1 = 'weengahhoghecho1eeSheeG5el9cie5'; $textD = $textD_1 . 'b'; $tests = ['A', 'B', 'C', 'D', 'A', 'B', 'C', 'D']; foreach ($tests as $test) { $subject = ''; switch ($test) { case 'A': $subject = $textA; break; case 'B': $subject = $textB; break; case 'C': $subject = $textC; break; case 'D': $subject = $textD; break; } $start = microtime(true); for ($i = 0; $i < 1000000; $i++) { preg_match($pattern, $subject); } $diff = sprintf('%.6f sec.', microtime(true) - $start); echo "Execution time $test: $diff\n"; gc_collect_cycles(); } ``` See: https://3v4l.org/dPqvu#v8.5.11 Resulted in this output: ``` Execution time A: 0.084869 sec. Execution time B: 0.052948 sec. Execution time C: 0.052931 sec. Execution time D: 0.030114 sec. Execution time A: 0.052482 sec. Execution time B: 0.052212 sec. Execution time C: 0.052929 sec. Execution time D: 0.031143 sec. ``` But I expected this output instead: ``` All execution times should nearly be identical. $textD is somehow fastest for PHP 8.4 and 8.5 $textB and $textB are fastest for PHP 8.2 and PHP 8.3 ``` ### PHP Version ```plain PHP 8.2, 8.3, 8.4, 8.5 on Debian. ``` ### Operating System _No response_

« previous php.bugs (#253010) next »