Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 3 additions & 0 deletions NEWS
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand Down
2 changes: 2 additions & 0 deletions UPGRADING
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
59 changes: 50 additions & 9 deletions Zend/zend_operators.c
Original file line number Diff line number Diff line change
Expand Up @@ -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) {
Expand Down Expand Up @@ -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) {
Comment thread
adapik marked this conversation as resolved.
Resolved
*--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
}

Expand Down
18 changes: 12 additions & 6 deletions Zend/zend_operators.h
Original file line number Diff line number Diff line change
Expand Up @@ -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) {
Comment thread
adapik marked this conversation as resolved.
Resolved
*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 */
Expand Down
Loading