Skip to content

perf: ext/standard: Avoid int-to-string conversion in array_unique() - #25845

Open
iliaal wants to merge 1 commit into
php:masterfrom
iliaal:opt/array-unique-sort-string-int
Open

iliaal wants to merge 1 commit into
php:masterfrom
iliaal:opt/array-unique-sort-string-int

Conversation

@iliaal

@iliaal iliaal commented Nov 27, 2026

Copy link
Copy Markdown
Member

With the default SORT_STRING flag, array_unique() converts every integer element to a temporary string before looking it up. This stores integers as integer keys and, once the first integer appears, aliases canonical numeric strings to the same keys, which avoids the conversion and keeps the string comparison semantics. Benchmarks are in the commit message.

In SORT_STRING mode, array_unique() converted every integer element to a
temporary string before inserting it into the table of seen values. Insert
integers directly as integer keys. When the first integer arrives, add an
integer alias for each canonical numeric string key already seen, and look
up later numeric strings by their integer value, so integers, numeric
strings, floats and references still compare by their string form. Arrays
of strings only pay one extra branch per element.

Neoverse-N1, GCC 13.3 -O2 NTS, 200,000 elements: sparse integers 1.91x,
dense integers 6.22x, alternating integers and numeric strings 2.11x,
strings only +0.3% (+0.7% instructions).

@ArtUkrainskiy ArtUkrainskiy left a comment

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.

I measured this on master 43a7692 with GCC 15 and Clang 21 (Ryzen 9 5950X, flags of the official Docker image). Multiples of 4096 or of 2^20 go quadratic, see the line comments for a fix.

Change in CPU cycles per call against master, negative = fewer cycles (faster). The last two columns add the scramble from the suggestions:

call master, cycles (GCC 15) GCC 15 Clang 21 + scramble, GCC 15 + scramble, Clang 21
1,000 distinct ints, $i * 4096, shuffled 114,831 +2610% +2444% −63% −63%
10,000 distinct ints, $i * 4096, shuffled 1,239,076 +6820% +6583% −67% −66%
10,000 distinct ints, $i << 20, shuffled 1,297,418 +23541% +22843% −74% −72%
1,000 ints from 0–700 112,195 −69% −70% −70% −73%
10,000 random ints 1,648,717 −75% −75% −74% −74%
1,000 daily timestamps 155,104 −2.3% −12% −77% −80%
1,000 values, int and numeric string alternate 76,144 −44% −44% −44% −48%
999 numeric strings, then one int 40,205 +70% +67% +69% +66%
1,000 strings key123 38,991 −2.9% +3.2% −3.5% +3.6%

With the scramble the output is the same as master on 60k random arrays. Patch and scripts: https://github.com/ArtUkrainskiy/php-src-bench/tree/main/reports/pr-25845-array-unique-int

Comment thread ext/standard/array.c
Comment on lines +5016 to +5020
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);

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

Comment thread ext/standard/array.c
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));

Comment thread ext/standard/array.c
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)));

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants