Skip to content

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

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

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

Conversation

@iliaal

@iliaal iliaal commented Nov 27, 2026

Copy link
Copy Markdown
Owner

In SORT_STRING mode, array_unique() converts every integer element to a temporary zend_string via zval_get_tmp_string() before inserting it into the seen HashTable.

Insert IS_LONG elements directly as integer keys in seen and lazily add integer aliases for any previously seen canonical numeric string keys when the first integer is encountered, keeping pure-string arrays free of ZEND_HANDLE_NUMERIC checks while preserving SORT_STRING equivalence between ints, numeric strings, floats, and references.

On ARM64 (Neoverse-N1, GCC 13.3 -O2 NTS): sparse 64-bit ints 5.33x (-81.30%), dense ints 4.43x (-77.44%), mixed arrays 1.23x (-18.90%), pure-string control +1.00% (+0.87% insns).

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).
@iliaal
iliaal force-pushed the opt/array-unique-sort-string-int branch from a2bec53 to cb739d2 Compare November 27, 2026 16:46
@iliaal

iliaal commented Nov 27, 2026

Copy link
Copy Markdown
Owner Author

Submitted upstream as php/php-src phpGH-25845: php#25845

@iliaal iliaal closed this Nov 27, 2026
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant