Repository navigation
perf: ext/standard: Avoid int-to-string conversion in array_unique() #25845
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Open
iliaal
wants to merge
1
commit into
php:master
Choose a base branch
from
iliaal:opt/array-unique-sort-string-int
base: master
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
+117
−6
Open
Changes from all commits
Commits
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change | ||||
|---|---|---|---|---|---|---|
|
|
@@ -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); | ||||||
| } | ||||||
| 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); | ||||||
|
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Same scramble here.
Suggested change
|
||||||
| } | ||||||
| } | ||||||
| } | ||||||
|
|
||||||
| /* {{{ Removes duplicate values from array */ | ||||||
| PHP_FUNCTION(array_unique) | ||||||
| { | ||||||
|
|
@@ -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)); | ||||||
|
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. And here.
Suggested change
|
||||||
| } 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) { | ||||||
|
|
||||||
75 changes: 75 additions & 0 deletions
75
ext/standard/tests/array/array_unique_sort_string_int_mixed.phpt
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| 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) |
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Integer keys in
seenhash by their low bits, so$i << 20all 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.