From f0b1170f6eda38598bbb2ca4ecfa80c0fcee571b Mon Sep 17 00:00:00 2001 From: Alexander Danilov Date: Fri, 13 Nov 2026 15:01:09 +0200 Subject: [PATCH] Zend: Improve integer to string conversion performance --- NEWS | 3 +++ UPGRADING | 2 ++ Zend/zend_operators.c | 59 ++++++++++++++++++++++++++++++++++++------- Zend/zend_operators.h | 18 ++++++++----- 4 files changed, 67 insertions(+), 15 deletions(-) diff --git a/NEWS b/NEWS index a0bce4ed6f43..7b17648a3db1 100644 --- a/NEWS +++ b/NEWS @@ -6,6 +6,9 @@ PHP NEWS . A flat unkeyed list assignment from an array literal with one value per target, such as [$a, $b] = [$b, $a], no longer builds the array when the result is unused (GH-23048). (Artem Ukrainskiy) + . 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: . Fixed curl_reset() retaining CURLOPT_PRIVATE after reset. (lacatoire) diff --git a/UPGRADING b/UPGRADING index d9242ecb3c17..829a7829210d 100644 --- a/UPGRADING +++ b/UPGRADING @@ -204,6 +204,8 @@ PHP 8.7 UPGRADE NOTES is copied, and they are assigned to the targets directly. A value its target does not keep is now released at that assignment instead of after the last one. + . Improved performance of integer to string conversion (string casts, echo, + implode(), json_encode(), serialize(), var_export() and others). - Standard: . Improved performance of strcspn() and strpbrk() when searching longer strings diff --git a/Zend/zend_operators.c b/Zend/zend_operators.c index 2960b4be1400..00fb66ced5fa 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 319209de066e..885e6f426e76 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 */