Commit 39c15a88beb for php
commit 39c15a88bebfcd4904702b7db8aed743543915b6
Author: Alexander Danilov <10532067+adapik@users.noreply.github.com>
Date: Tue Nov 17 19:22:55 2026 +0200
Zend: Improve integer to string conversion performance (#25444)
diff --git a/NEWS b/NEWS
index d9d6fd636df..8c8379da143 100644
--- a/NEWS
+++ b/NEWS
@@ -8,6 +8,9 @@ PHP NEWS
result is unused (GH-23048). (Artem Ukrainskiy)
. Fixed bug GH-9993 (Bad performance with hard-coded GC_THRESHOLD_* values).
(Kamil Tekiela)
+ . Improved performance of integer to string conversion, used by string casts,
+ echo, implode(), json_encode(), serialize() and other places, by emitting
+ two digits per step. (Alexander Danilov)
- Curl:
. Negative values for CURLOPT_POSTREDIR are now rejected, causing
diff --git a/UPGRADING b/UPGRADING
index 083b8e94531..9dbcd594a29 100644
--- a/UPGRADING
+++ b/UPGRADING
@@ -212,6 +212,8 @@ PHP 8.7 UPGRADE NOTES
. The garbage collector now raises its threshold when its runs traverse a
large graph of live objects, so that it runs less often. Before, it
traversed the whole graph again after every 10000 possible roots.
+ . Improved performance of integer to string conversion (string casts, echo,
+ implode(), json_encode(), serialize(), var_export() and others).
- DOM:
. Parsing HTML with Dom\HTMLDocument or innerHTML uses much less memory for
diff --git a/Zend/zend_operators.c b/Zend/zend_operators.c
index 2960b4be140..00fb66ced5f 100644
--- a/Zend/zend_operators.c
+++ b/Zend/zend_operators.c
@@ -3585,6 +3585,52 @@ ZEND_API int ZEND_FASTCALL zend_compare_objects(zval *o1, zval *o2) /* {{{ */
}
/* }}} */
+static const char zend_two_digits[201] =
+ "0001020304050607080910111213141516171819202122232425262728293031323334353637383940414243444546474849"
+ "5051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899";
+
+static zend_always_inline char *zend_print_quad(char *buf, uint32_t quad)
+{
+ const char *hi = &zend_two_digits[(quad / 100) * 2];
+ const char *lo = &zend_two_digits[(quad % 100) * 2];
+ buf -= 4;
+ buf[0] = hi[0];
+ buf[1] = hi[1];
+ buf[2] = lo[0];
+ buf[3] = lo[1];
+ return buf;
+}
+
+static zend_always_inline char *zend_print_ulong_digits(char *buf, zend_ulong num)
+{
+ uint32_t rem;
+
+ while (num >= 10000) {
+ buf = zend_print_quad(buf, (uint32_t) (num % 10000));
+ num /= 10000;
+ }
+ rem = (uint32_t) num;
+ if (rem >= 100) {
+ const char *lo = &zend_two_digits[(rem % 100) * 2];
+ rem /= 100;
+ *--buf = lo[1];
+ *--buf = lo[0];
+ }
+ if (rem >= 10) {
+ *--buf = zend_two_digits[rem * 2 + 1];
+ *--buf = zend_two_digits[rem * 2];
+ } else {
+ *--buf = (char) rem + '0';
+ }
+ return buf;
+}
+
+ZEND_API char *ZEND_FASTCALL zend_print_ulong_to_buf_slow(char *buf, zend_ulong num)
+{
+ *buf = '\0';
+ return zend_print_ulong_digits(buf, num);
+}
+
ZEND_API zend_string* ZEND_FASTCALL zend_long_to_str(zend_long num) /* {{{ */
{
if ((zend_ulong)num <= 9) {
@@ -3618,17 +3664,12 @@ static zend_always_inline char *zend_print_u64_to_buf(char *buf, uint64_t num64)
return zend_print_ulong_to_buf(buf, num64);
#else
*buf = '\0';
+
while (num64 > ZEND_ULONG_MAX) {
- *--buf = (char) (num64 % 10) + '0';
- num64 /= 10;
+ buf = zend_print_quad(buf, (uint32_t) (num64 % 10000));
+ num64 /= 10000;
}
-
- zend_ulong num = (zend_ulong) num64;
- do {
- *--buf = (char) (num % 10) + '0';
- num /= 10;
- } while (num > 0);
- return buf;
+ return zend_print_ulong_digits(buf, (zend_ulong) num64);
#endif
}
diff --git a/Zend/zend_operators.h b/Zend/zend_operators.h
index 319209de066..885e6f426e7 100644
--- a/Zend/zend_operators.h
+++ b/Zend/zend_operators.h
@@ -977,14 +977,20 @@ static zend_always_inline bool fast_is_not_identical_function(const zval *op1, c
return !zend_is_identical(op1, op2);
}
+ZEND_API char *ZEND_FASTCALL zend_print_ulong_to_buf_slow(char *buf, zend_ulong num);
+
/* buf points to the END of the buffer */
static zend_always_inline char *zend_print_ulong_to_buf(char *buf, zend_ulong num) {
- *buf = '\0';
- do {
- *--buf = (char) (num % 10) + '0';
- num /= 10;
- } while (num > 0);
- return buf;
+ if (EXPECTED(num < 100)) {
+ uint32_t n = (uint32_t) num;
+ *buf = '\0';
+ *--buf = (char) (n % 10) + '0';
+ if (n >= 10) {
+ *--buf = (char) (n / 10) + '0';
+ }
+ return buf;
+ }
+ return zend_print_ulong_to_buf_slow(buf, num);
}
/* buf points to the END of the buffer */