Skip to content

Commit f0b1170

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

4 files changed

Lines changed: 67 additions & 15 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: 50 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -3585,6 +3585,52 @@ 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_quad(char *buf, uint32_t quad)
3593+
{
3594+
const char *hi = &zend_two_digits[(quad / 100) * 2];
3595+
const char *lo = &zend_two_digits[(quad % 100) * 2];
3596+
buf -= 4;
3597+
buf[0] = hi[0];
3598+
buf[1] = hi[1];
3599+
buf[2] = lo[0];
3600+
buf[3] = lo[1];
3601+
return buf;
3602+
}
3603+
3604+
static zend_always_inline char *zend_print_ulong_digits(char *buf, zend_ulong num)
3605+
{
3606+
uint32_t rem;
3607+
3608+
while (num >= 10000) {
3609+
buf = zend_print_quad(buf, (uint32_t) (num % 10000));
3610+
num /= 10000;
3611+
}
3612+
rem = (uint32_t) num;
3613+
if (rem >= 100) {
3614+
const char *lo = &zend_two_digits[(rem % 100) * 2];
3615+
rem /= 100;
3616+
*--buf = lo[1];
3617+
*--buf = lo[0];
3618+
}
3619+
if (rem >= 10) {
3620+
*--buf = zend_two_digits[rem * 2 + 1];
3621+
*--buf = zend_two_digits[rem * 2];
3622+
} else {
3623+
*--buf = (char) rem + '0';
3624+
}
3625+
return buf;
3626+
}
3627+
3628+
ZEND_API char *ZEND_FASTCALL zend_print_ulong_to_buf_slow(char *buf, zend_ulong num)
3629+
{
3630+
*buf = '\0';
3631+
return zend_print_ulong_digits(buf, num);
3632+
}
3633+
35883634
ZEND_API zend_string* ZEND_FASTCALL zend_long_to_str(zend_long num) /* {{{ */
35893635
{
35903636
if ((zend_ulong)num <= 9) {
@@ -3618,17 +3664,12 @@ static zend_always_inline char *zend_print_u64_to_buf(char *buf, uint64_t num64)
36183664
return zend_print_ulong_to_buf(buf, num64);
36193665
#else
36203666
*buf = '\0';
3667+
36213668
while (num64 > ZEND_ULONG_MAX) {
3622-
*--buf = (char) (num64 % 10) + '0';
3623-
num64 /= 10;
3669+
buf = zend_print_quad(buf, (uint32_t) (num64 % 10000));
3670+
num64 /= 10000;
36243671
}
3625-
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;
3672+
return zend_print_ulong_digits(buf, (zend_ulong) num64);
36323673
#endif
36333674
}
36343675

‎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)