Skip to content

Commit f40a84c

Browse files
divinity76laruence
andauthored
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>
1 parent a8bbfb8 commit f40a84c

1 file changed

Lines changed: 2 additions & 1 deletion

File tree

‎ext/standard/string.c‎

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -5664,7 +5664,8 @@ PHP_FUNCTION(str_repeat)
56645664

56655665
while (e<ee) {
56665666
l = (e-s) < (ee-e) ? (e-s) : (ee-e);
5667-
memmove(e, s, l);
5667+
/* src [s, s+l) and dst [e, e+l) cannot overlap: l <= e-s */
5668+
memcpy(e, s, l);
56685669
e += l;
56695670
}
56705671
}

0 commit comments

Comments
 (0)