[php-src] master: ext/standard: Optimize str_repeat() when the multiplier is one (#23681)
| From: | Weilin Du via GitHub | Date: | Mon, 14 Sep 2026 16:03:31 +0000 |
| Subject: | [php-src] master: ext/standard: Optimize str_repeat() when the multiplier is one (#23681) | ||
| Groups: | php.cvs | ||
| Request: | Send a blank email to php-cvs+get-141197@lists.php.net to get a copy of this message | ||
Author: Weilin Du (LamentXU123)
Committer: GitHub (web-flow)
Pusher: LamentXU123
Date: 2026-09-15T00:03:28+08:00
Commit: https://github.com/php/php-src/commit/93fd714daa6b32372d6b7c913433c386054455c7
Raw diff: https://github.com/php/php-src/commit/93fd714daa6b32372d6b7c913433c386054455c7.diff
ext/standard: Optimize str_repeat() when the multiplier is one (#23681)
Just reuse the input string when the multiplier is one :)
Co-authored-by: Nora Dossche <7771979+ndossche@users.noreply.github.com>
Changed paths:
A ext/opcache/tests/jit/str_repeat_refcount.phpt
M UPGRADING
M Zend/Optimizer/zend_func_infos.h
M ext/standard/basic_functions.stub.php
M ext/standard/basic_functions_arginfo.h
M ext/standard/basic_functions_decl.h
M ext/standard/string.c
Diff:
diff --git a/UPGRADING b/UPGRADING
index 07223cd7a21f..ee7f4c3ab73d 100644
--- a/UPGRADING
+++ b/UPGRADING
@@ -1110,6 +1110,7 @@ PHP 8.6 UPGRADE NOTES
. Improved performance of intval('+0b...', 2) and intval('0b...', 2).
. Improved performance of str_split().
. Improved performance of str_pad().
+ . Improved performance of str_repeat() when the multiplier is 1.
- URI:
. Improved performance of Uri\WhatWg\Url::parse() when collecting
diff --git a/Zend/Optimizer/zend_func_infos.h b/Zend/Optimizer/zend_func_infos.h
index b14de7888a94..0add5a32f911 100644
--- a/Zend/Optimizer/zend_func_infos.h
+++ b/Zend/Optimizer/zend_func_infos.h
@@ -493,7 +493,6 @@ static const func_info_t func_infos[] = {
F1("hebrev", MAY_BE_STRING),
F1("strip_tags", MAY_BE_STRING),
F1("str_getcsv",
MAY_BE_ARRAY|MAY_BE_ARRAY_KEY_LONG|MAY_BE_ARRAY_OF_STRING|MAY_BE_ARRAY_OF_NULL),
- F1("str_repeat", MAY_BE_STRING),
F1("count_chars",
MAY_BE_ARRAY|MAY_BE_ARRAY_KEY_LONG|MAY_BE_ARRAY_OF_LONG|MAY_BE_STRING),
F1("localeconv",
MAY_BE_ARRAY|MAY_BE_ARRAY_KEY_STRING|MAY_BE_ARRAY_OF_LONG|MAY_BE_ARRAY_OF_STRING|MAY_BE_ARRAY_OF_ARRAY),
F1("sscanf",
MAY_BE_ARRAY|MAY_BE_ARRAY_KEY_LONG|MAY_BE_ARRAY_OF_ANY|MAY_BE_LONG|MAY_BE_NULL),
diff --git a/ext/opcache/tests/jit/str_repeat_refcount.phpt
b/ext/opcache/tests/jit/str_repeat_refcount.phpt
new file mode 100644
index 000000000000..9a9b83cb9291
--- /dev/null
+++ b/ext/opcache/tests/jit/str_repeat_refcount.phpt
@@ -0,0 +1,23 @@
+--TEST--
+str_repeat() may return a shared string
+--EXTENSIONS--
+opcache
+--INI--
+opcache.enable=1
+opcache.enable_cli=1
+opcache.file_update_protection=0
+opcache.jit=1205
+opcache.jit_buffer_size=32M
+--FILE--
+<?php
+function test(int $length): void {
+ $input = str_repeat('a', $length);
+ str_repeat($input, 1);
+ $overwrite = str_repeat('b', $length);
+ var_dump($input, $overwrite);
+}
+test(16);
+?>
+--EXPECT--
+string(16) "aaaaaaaaaaaaaaaa"
+string(16) "bbbbbbbbbbbbbbbb"
diff --git a/ext/standard/basic_functions.stub.php b/ext/standard/basic_functions.stub.php
index 6b3b11a5fa3e..4b455ec331b6 100644
--- a/ext/standard/basic_functions.stub.php
+++ b/ext/standard/basic_functions.stub.php
@@ -2593,7 +2593,6 @@ function parse_str(string $string, &$result): void {}
*/
function str_getcsv(string $string, string $separator = ",", string $enclosure =
"\"", string $escape = "\\"): array {}
-/** @refcount 1 */
function str_repeat(string $string, int $times): string {}
/**
diff --git a/ext/standard/basic_functions_arginfo.h b/ext/standard/basic_functions_arginfo.h
index 677c5e668600..b770a1e8684c 100644
--- a/ext/standard/basic_functions_arginfo.h
+++ b/ext/standard/basic_functions_arginfo.h
@@ -1,5 +1,5 @@
/* This is a generated file, edit basic_functions.stub.php instead.
- * Stub hash: 4feeab72edf6b7440af99a60e1401492828f141e
+ * Stub hash: b67e9418e158d5726b6a54d446b816264c747116
* Has decl header: yes */
#include "zend_attributes.h"
diff --git a/ext/standard/basic_functions_decl.h b/ext/standard/basic_functions_decl.h
index d60317766993..aab4e4b5f09e 100644
--- a/ext/standard/basic_functions_decl.h
+++ b/ext/standard/basic_functions_decl.h
@@ -1,8 +1,8 @@
/* This is a generated file, edit basic_functions.stub.php instead.
- * Stub hash: 4feeab72edf6b7440af99a60e1401492828f141e */
+ * Stub hash: b67e9418e158d5726b6a54d446b816264c747116 */
-#ifndef ZEND_BASIC_FUNCTIONS_DECL_4feeab72edf6b7440af99a60e1401492828f141e_H
-#define ZEND_BASIC_FUNCTIONS_DECL_4feeab72edf6b7440af99a60e1401492828f141e_H
+#ifndef ZEND_BASIC_FUNCTIONS_DECL_b67e9418e158d5726b6a54d446b816264c747116_H
+#define ZEND_BASIC_FUNCTIONS_DECL_b67e9418e158d5726b6a54d446b816264c747116_H
typedef enum zend_enum_SortDirection {
ZEND_ENUM_SortDirection_Ascending = 1,
@@ -20,4 +20,4 @@ typedef enum zend_enum_RoundingMode {
ZEND_ENUM_RoundingMode_PositiveInfinity = 8,
} zend_enum_RoundingMode;
-#endif /* ZEND_BASIC_FUNCTIONS_DECL_4feeab72edf6b7440af99a60e1401492828f141e_H */
+#endif /* ZEND_BASIC_FUNCTIONS_DECL_b67e9418e158d5726b6a54d446b816264c747116_H */
diff --git a/ext/standard/string.c b/ext/standard/string.c
index f7c2ed37cf64..2c5990a6a8c8 100644
--- a/ext/standard/string.c
+++ b/ext/standard/string.c
@@ -5638,6 +5638,10 @@ PHP_FUNCTION(str_repeat)
if (ZSTR_LEN(input_str) == 0 || mult == 0)
RETURN_EMPTY_STRING();
+ if (mult == 1) {
+ RETURN_STR_COPY(input_str);
+ }
+
/* Initialize the result string */
result = zend_string_safe_alloc(ZSTR_LEN(input_str), mult, 0, 0);
result_len = ZSTR_LEN(input_str) * mult;