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
2 changes: 2 additions & 0 deletions NEWS
Original file line number Diff line number Diff line change
Expand Up @@ -238,6 +238,8 @@ PHP NEWS
specified number base. (Sjoerd Langkemper)
. Improved performance of in_array() and array_search() in strict mode when
searching for a string. (Alexander Danilov)
. Improved performance of array_unique() with SORT_STRING on arrays that
contain integers. (Ilia Alshanetsky)

- URI:
. Fix casing of enum cases in UriHostType and UrlHostType to match the RFC
Expand Down
2 changes: 2 additions & 0 deletions UPGRADING
Original file line number Diff line number Diff line change
Expand Up @@ -257,6 +257,8 @@ PHP 8.7 UPGRADE NOTES
encoding.
. Improved performance of in_array() and array_search() in strict mode when
searching for a string.
. Improved performance of array_unique() with SORT_STRING on arrays that
contain integers.

- MBString:
. Improved performance of mb_strlen for UTF-8 strings.
Expand Down
44 changes: 38 additions & 6 deletions ext/standard/array.c
Original file line number Diff line number Diff line change
Expand Up @@ -5013,6 +5013,27 @@ static void array_bucketindex_swap(void *p, void *q)
*g = t;
}

static zend_always_inline zval *php_array_unique_add_string(HashTable *seen, zend_string *str, bool has_int)
{
zend_ulong h;
if (UNEXPECTED(has_int) && ZEND_HANDLE_NUMERIC(str, h)) {
return zend_hash_index_add_empty_element(seen, h);
Comment on lines +5016 to +5020

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Integer keys in seen hash by their low bits, so $i << 20 all lands in one bucket and the call goes quadratic. A one-to-one scramble keeps them spread. I haven't built the 32-bit branch.

Suggested change
static zend_always_inline zval *php_array_unique_add_string(HashTable *seen, zend_string *str, bool has_int)
{
zend_ulong h;
if (UNEXPECTED(has_int) && ZEND_HANDLE_NUMERIC(str, h)) {
return zend_hash_index_add_empty_element(seen, h);
static zend_always_inline zend_ulong php_array_unique_mix(zend_ulong h)
{
#if SIZEOF_ZEND_LONG == 8
h *= Z_UL(0x9E3779B97F4A7C15);
return (h >> 32) | (h << 32);
#else
h *= Z_UL(0x9E3779B9);
return (h >> 16) | (h << 16);
#endif
}
static zend_always_inline zval *php_array_unique_add_string(HashTable *seen, zend_string *str, bool has_int)
{
zend_ulong h;
if (UNEXPECTED(has_int) && ZEND_HANDLE_NUMERIC(str, h)) {
return zend_hash_index_add_empty_element(seen, php_array_unique_mix(h));

}
return zend_hash_add_empty_element(seen, str);
}

static void php_array_unique_add_numeric_aliases(HashTable *seen)
{
uint32_t n = seen->nNumUsed;
for (uint32_t i = 0; i < n; i++) {
zend_ulong h;
zend_string *key = seen->arData[i].key;
if (key && ZEND_HANDLE_NUMERIC(key, h)) {
zend_hash_index_add_empty_element(seen, h);

@ArtUkrainskiy ArtUkrainskiy Nov 28, 2026 •

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Same scramble here.

Suggested change
zend_hash_index_add_empty_element(seen, h);
zend_hash_index_add_empty_element(seen, php_array_unique_mix(h));

}
}
}

/* {{{ Removes duplicate values from array */
PHP_FUNCTION(array_unique)
{
Expand Down Expand Up @@ -5043,15 +5064,26 @@ PHP_FUNCTION(array_unique)
zend_hash_init(&seen, zend_hash_num_elements(Z_ARRVAL_P(array)), NULL, NULL, 0);
array_init(return_value);

bool has_int = false;
ZEND_HASH_FOREACH_KEY_VAL(Z_ARRVAL_P(array), num_key, str_key, val) {
zval *retval;
if (Z_TYPE_P(val) == IS_STRING) {
retval = zend_hash_add_empty_element(&seen, Z_STR_P(val));
if (EXPECTED(Z_TYPE_P(val) == IS_STRING)) {
retval = php_array_unique_add_string(&seen, Z_STR_P(val), has_int);
} else {
zend_string *tmp_str_val;
zend_string *str_val = zval_get_tmp_string(val, &tmp_str_val);
retval = zend_hash_add_empty_element(&seen, str_val);
zend_tmp_string_release(tmp_str_val);
zval *lookup_val = val;
ZVAL_DEREF(lookup_val);
if (Z_TYPE_P(lookup_val) == IS_LONG) {
if (UNEXPECTED(!has_int)) {
has_int = true;
php_array_unique_add_numeric_aliases(&seen);
}
retval = zend_hash_index_add_empty_element(&seen, (zend_ulong) Z_LVAL_P(lookup_val));

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

And here.

Suggested change
retval = zend_hash_index_add_empty_element(&seen, (zend_ulong) Z_LVAL_P(lookup_val));
retval = zend_hash_index_add_empty_element(&seen, php_array_unique_mix((zend_ulong) Z_LVAL_P(lookup_val)));

} else {
zend_string *tmp_str_val;
zend_string *str_val = zval_get_tmp_string(lookup_val, &tmp_str_val);
retval = php_array_unique_add_string(&seen, str_val, has_int);
zend_tmp_string_release(tmp_str_val);
}
}

if (retval) {
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,75 @@
--TEST--
array_unique() SORT_STRING preserves string-int equivalence across mixed inputs
--FILE--
<?php
$refInt = 1;
$refStr = '42';
$bigStrThenInt = [];
for ($i = 0; $i < 100; $i++) {
$bigStrThenInt[] = (string) $i;
}
$bigStrThenInt[] = 50;
$bigStrThenInt[] = 200;
$bigStrThenInt[] = '200';

var_dump(array_unique(['1', 1, '1', '01', 'a', 'a']));
var_dump(array_unique([1, '1', '01', 1.0, '1.0']));
var_dump(array_unique([1.0, 1, '1']));
var_dump(array_unique(['a', 'b', &$refInt, '1', &$refStr, 42]));
var_dump(array_unique(['-5', -5, '-0', 0, '0']));
var_dump(array_unique([PHP_INT_MIN, (string) PHP_INT_MIN, PHP_INT_MAX, (string) PHP_INT_MAX]));
var_dump(array_unique([str_repeat('9', 30), PHP_INT_MAX, str_repeat('9', 30)]));
var_dump(count(array_unique($bigStrThenInt)));
?>
--EXPECTF--
array(3) {
[0]=>
string(1) "1"
[3]=>
string(2) "01"
[4]=>
string(1) "a"
}
array(3) {
[0]=>
int(1)
[2]=>
string(2) "01"
[4]=>
string(3) "1.0"
}
array(1) {
[0]=>
float(1)
}
array(4) {
[0]=>
string(1) "a"
[1]=>
string(1) "b"
[2]=>
&int(1)
[4]=>
&string(2) "42"
}
array(3) {
[0]=>
string(2) "-5"
[2]=>
string(2) "-0"
[3]=>
int(0)
}
array(2) {
[0]=>
int(%i)
[2]=>
int(%d)
}
array(2) {
[0]=>
string(30) "999999999999999999999999999999"
[1]=>
int(%d)
}
int(101)
Loading