Skip to content

Commit d98534c

Browse files
committed
Zend: Improve integer to string conversion performance
1 parent 6c0388f commit d98534c

4 files changed

Lines changed: 70 additions & 16 deletions

File tree

‎NEWS‎

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,9 @@ PHP NEWS
66
. A flat unkeyed list assignment from an array literal with one value per
77
target, such as [$a, $b] = [$b, $a], no longer builds the array when the
88
result is unused (GH-23048). (Artem Ukrainskiy)
9+
. Improved performance of integer to string conversion, used by string casts,
10+
echo, implode(), json_encode(), serialize() and other places, by emitting
11+
two digits per step. (Alexander Danilov)
912

1013
- Curl:
1114
. Fixed curl_reset() retaining CURLOPT_PRIVATE after reset. (lacatoire)

‎UPGRADING‎

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -204,6 +204,8 @@ PHP 8.7 UPGRADE NOTES
204204
is copied, and they are assigned to the targets directly. A value its
205205
target does not keep is now released at that assignment instead of after
206206
the last one.
207+
. Improved performance of integer to string conversion (string casts, echo,
208+
implode(), json_encode(), serialize(), var_export() and others).
207209

208210
- Standard:
209211
. Improved performance of strcspn() and strpbrk() when searching longer strings

‎Zend/zend_operators.c‎

Lines changed: 53 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -3585,6 +3585,47 @@ ZEND_API int ZEND_FASTCALL zend_compare_objects(zval *o1, zval *o2) /* {{{ */
35853585
}
35863586
/* }}} */
35873587

3588+
static const char zend_two_digits[201] =
3589+
"0001020304050607080910111213141516171819202122232425262728293031323334353637383940414243444546474849"
3590+
"5051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899";
3591+
3592+
static zend_always_inline char *zend_print_ulong_digits(char *buf, zend_ulong num)
3593+
{
3594+
uint32_t rem;
3595+
3596+
while (num >= 10000) {
3597+
uint32_t quad = (uint32_t) (num % 10000);
3598+
const char *hi = &zend_two_digits[(quad / 100) * 2];
3599+
const char *lo = &zend_two_digits[(quad % 100) * 2];
3600+
num /= 10000;
3601+
buf -= 4;
3602+
buf[0] = hi[0];
3603+
buf[1] = hi[1];
3604+
buf[2] = lo[0];
3605+
buf[3] = lo[1];
3606+
}
3607+
rem = (uint32_t) num;
3608+
if (rem >= 100) {
3609+
const char *lo = &zend_two_digits[(rem % 100) * 2];
3610+
rem /= 100;
3611+
*--buf = lo[1];
3612+
*--buf = lo[0];
3613+
}
3614+
if (rem >= 10) {
3615+
*--buf = zend_two_digits[rem * 2 + 1];
3616+
*--buf = zend_two_digits[rem * 2];
3617+
} else {
3618+
*--buf = (char) rem + '0';
3619+
}
3620+
return buf;
3621+
}
3622+
3623+
ZEND_API char *ZEND_FASTCALL zend_print_ulong_to_buf_slow(char *buf, zend_ulong num) /* {{{ */
3624+
{
3625+
*buf = '\0';
3626+
return zend_print_ulong_digits(buf, num);
3627+
}
3628+
35883629
ZEND_API zend_string* ZEND_FASTCALL zend_long_to_str(zend_long num) /* {{{ */
35893630
{
35903631
if ((zend_ulong)num <= 9) {
@@ -3618,17 +3659,19 @@ static zend_always_inline char *zend_print_u64_to_buf(char *buf, uint64_t num64)
36183659
return zend_print_ulong_to_buf(buf, num64);
36193660
#else
36203661
*buf = '\0';
3621-
while (num64 > ZEND_ULONG_MAX) {
3622-
*--buf = (char) (num64 % 10) + '0';
3623-
num64 /= 10;
3624-
}
36253662

3626-
zend_ulong num = (zend_ulong) num64;
3627-
do {
3628-
*--buf = (char) (num % 10) + '0';
3629-
num /= 10;
3630-
} while (num > 0);
3631-
return buf;
3663+
while (num64 > ZEND_ULONG_MAX) {
3664+
uint32_t quad = (uint32_t) (num64 % 10000);
3665+
const char *hi = &zend_two_digits[(quad / 100) * 2];
3666+
const char *lo = &zend_two_digits[(quad % 100) * 2];
3667+
num64 /= 10000;
3668+
buf -= 4;
3669+
buf[0] = hi[0];
3670+
buf[1] = hi[1];
3671+
buf[2] = lo[0];
3672+
buf[3] = lo[1];
3673+
}
3674+
return zend_print_ulong_digits(buf, (zend_ulong) num64);
36323675
#endif
36333676
}
36343677

‎Zend/zend_operators.h‎

Lines changed: 12 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -977,14 +977,20 @@ static zend_always_inline bool fast_is_not_identical_function(const zval *op1, c
977977
return !zend_is_identical(op1, op2);
978978
}
979979

980+
ZEND_API char *ZEND_FASTCALL zend_print_ulong_to_buf_slow(char *buf, zend_ulong num);
981+
980982
/* buf points to the END of the buffer */
981983
static zend_always_inline char *zend_print_ulong_to_buf(char *buf, zend_ulong num) {
982-
*buf = '\0';
983-
do {
984-
*--buf = (char) (num % 10) + '0';
985-
num /= 10;
986-
} while (num > 0);
987-
return buf;
984+
if (EXPECTED(num < 100)) {
985+
uint32_t n = (uint32_t) num;
986+
*buf = '\0';
987+
*--buf = (char) (n % 10) + '0';
988+
if (n >= 10) {
989+
*--buf = (char) (n / 10) + '0';
990+
}
991+
return buf;
992+
}
993+
return zend_print_ulong_to_buf_slow(buf, num);
988994
}
989995

990996
/* buf points to the END of the buffer */

0 commit comments

Comments
 (0)