[php-src] master: Optimize str_repeat (#23128)

From: Date: Sun, 18 Oct 2026 09:31:28 +0000
Subject: [php-src] master: Optimize str_repeat (#23128)
Groups: php.cvs 
Request: Send a blank email to php-cvs+get-142390@lists.php.net to get a copy of this message
Author: divinity76 (divinity76) Committer: GitHub (web-flow) Pusher: ndossche Date: 2026-10-18T11:31:25+02:00 Commit: https://github.com/php/php-src/commit/f40a84cd61efdbaf8a6cf9eaaf354521238d84f0 Raw diff: https://github.com/php/php-src/commit/f40a84cd61efdbaf8a6cf9eaaf354521238d84f0.diff Optimize str_repeat (#23128) * Optimize str_repeat The memmove got me thinking "can src and dst actually overlap?", and it turns out no: - e always points to populated data + 1 - l may reach the end of populated data, but will never exceed it memcpy is safe here, and faster (unless the compiler already spotted it in an optimization pass and replaced it internally, entirely possible) * comment pinning the non-overlap invariant Co-authored-by: Xinchen Hui <laruence@gmail.com> --------- Co-authored-by: Xinchen Hui <laruence@gmail.com> Changed paths: M ext/standard/string.c Diff: diff --git a/ext/standard/string.c b/ext/standard/string.c index 2e949da741dc..3775074b590c 100644 --- a/ext/standard/string.c +++ b/ext/standard/string.c @@ -5664,7 +5664,8 @@ PHP_FUNCTION(str_repeat) while (e<ee) { l = (e-s) < (ee-e) ? (e-s) : (ee-e); - memmove(e, s, l); + /* src [s, s+l) and dst [e, e+l) cannot overlap: l <= e-s */ + memcpy(e, s, l); e += l; } }

« previous php.cvs (#142390) next »