[php-src] PR #25901: ext/standard: Keep the first 16 match positions in str_replace()

From: Date: Sun, 29 Nov 2026 15:34:02 +0000
Subject: [php-src] PR #25901: ext/standard: Keep the first 16 match positions in str_replace()
Groups: php.git-pulls 
Request: Send a blank email to git-pulls+get-41204@lists.php.net to get a copy of this message
Pull Request: https://github.com/php/php-src/pull/25901 Author: ArtUkrainskiy This is a follow-up to #25851. When the search string is longer than one byte and the replacement has a different length, str_replace() makes two passes. The first one counts the matches, the second one searches for them again and copies. After #25851 the second pass skips the search for the first match, and for all of them when they're adjacent. Every other match is still searched for twice. This patch stores the start positions of the first 16 matches during the first pass. The second pass copies them without searching, then goes on as before. The array lives in a separate zend_never_inline function that only runs for two or more matches. With -fstack-protector-strong a local array adds a stack check to the whole function. When I kept it in php_str_to_str_ex(), a call with no match took 4.5–4.7% more cycles. The cost is a second copy of the size and copy code for the one-match case. Change in CPU cycles per call against master, negative = fewer cycles (faster). Ryzen 9 5950X, flags of the official Docker image, median of 15 passes: | call | master, cycles (GCC 15) | GCC 15 | Clang 21 | |---|--:|--:|--:| | 68 B English, one {{name}} | 239 | −4.4% | +2.7% | | 4 KB English, one {{name}} | 470 | +1.7% | 0% | | 77 B English, two {{name}} | 317 | −2.9% | −11% | | 1 KB English, 3 × {{name}} | 438 | −6.9% | −13% | | 4 KB English, 10 × {{name}} | 1,150 | −20% | −24% | | 4 KB English, 32 × {{name}} | 2,877 | −15% | −17% | | 4 KB English, no match | 215 | +2.4% | +0.6% | | 66 KB English, \r\n → \n, 1,092 matches | 89,619 | +0.5% | −3.1% | | 70 KB English, -- → —, 1,638 matches | 125,918 | +1.0% | −1.5% | | 64 KB, 6,553 adjacent abcdefghij → x (example from #25851) | 629,566 | +2.2% | +0.6% | | 4 KB Russian, сервер → server, 38 matches | 15,579 | −19% | −18% | | 1 KB Japanese, ユーザー → user, 11 matches | 7,120 | −44% | −45% | | 1 KB Spanish, canción → cancion, 13 matches | 1,458 | −30% | −36% | No match runs one instruction fewer per call than master. Master against master gives medians within 1.7%. With thousands of matches the patch saves at most 16 searches per call, so those calls stay within 3.1% of master. 16 is a guess, I didn't try 32 or 64. Output and count match master on 300,000 random cases with GCC 15 and Clang 21, NTS and ZTS, plus 20,000 under ASan/UBSan. The new test covers 1, 2, 15, 16, 17 and 40 matches with gaps, and 16, 17 and 40 adjacent ones. It catches three mutations of the new code. I only measured on x86-64 AMD, not on Intel or aarch64. str_ireplace() has the same two passes. I'd do it in a separate PR if this one is fine. UPGRADING already has the entry from #25851 for this path, so I didn't add one. Full tables, the two variants I dropped, and scripts: https://github.com/ArtUkrainskiy/php-src-bench/tree/main/reports/str-replace-match-positions

« previous php.git-pulls (#41204) next »