-
Notifications
You must be signed in to change notification settings - Fork 8k
Improve performance of array_map() #18158
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
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -6736,44 +6736,73 @@ PHP_FUNCTION(array_map) | |
| ZEND_PARSE_PARAMETERS_END(); | ||
|
|
||
| if (n_arrays == 1) { | ||
| zend_ulong num_key; | ||
| zend_string *str_key; | ||
| zval *zv, arg; | ||
| int ret; | ||
|
|
||
| if (Z_TYPE(arrays[0]) != IS_ARRAY) { | ||
| zend_argument_type_error(2, "must be of type array, %s given", zend_zval_value_name(&arrays[0])); | ||
| RETURN_THROWS(); | ||
| } | ||
| maxlen = zend_hash_num_elements(Z_ARRVAL(arrays[0])); | ||
| const HashTable *input = Z_ARRVAL(arrays[0]); | ||
| maxlen = zend_hash_num_elements(input); | ||
|
|
||
| /* Short-circuit: if no callback and only one array, just return it. */ | ||
| if (!ZEND_FCI_INITIALIZED(fci) || !maxlen) { | ||
| ZVAL_COPY(return_value, &arrays[0]); | ||
| return; | ||
| } | ||
|
|
||
| array_init_size(return_value, maxlen); | ||
| zend_hash_real_init(Z_ARRVAL_P(return_value), HT_IS_PACKED(Z_ARRVAL(arrays[0]))); | ||
|
|
||
| ZEND_HASH_FOREACH_KEY_VAL(Z_ARRVAL(arrays[0]), num_key, str_key, zv) { | ||
| fci.retval = &result; | ||
| fci.param_count = 1; | ||
| fci.params = &arg; | ||
|
|
||
| ZVAL_COPY(&arg, zv); | ||
| ret = zend_call_function(&fci, &fci_cache); | ||
| i_zval_ptr_dtor(&arg); | ||
| if (ret != SUCCESS || Z_TYPE(result) == IS_UNDEF) { | ||
| zend_array_destroy(Z_ARR_P(return_value)); | ||
| RETURN_NULL(); | ||
| } | ||
| if (str_key) { | ||
| _zend_hash_append(Z_ARRVAL_P(return_value), str_key, &result); | ||
| } else { | ||
| zend_hash_index_add_new(Z_ARRVAL_P(return_value), num_key, &result); | ||
| } | ||
| } ZEND_HASH_FOREACH_END(); | ||
| fci.retval = &result; | ||
| fci.param_count = 1; | ||
|
|
||
| if (HT_IS_PACKED(input)) { | ||
| array_init_size(return_value, input->nNumUsed); | ||
| HashTable *output = Z_ARRVAL_P(return_value); | ||
| zend_hash_real_init_packed(output); | ||
|
|
||
| uint32_t undefs = 0; | ||
| ZEND_HASH_FILL_PACKED(output) { | ||
| /* Can't use ZEND_HASH_PACKED_FOREACH_VAL() because we need to also account for the UNDEF values | ||
| * so the keys in the output array will match those of the input array. */ | ||
| for (zval *cur = input->arPacked, *end = input->arPacked + input->nNumUsed; cur != end; cur++) { | ||
|
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. Is this not using 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. Correct. 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. I would add a comment, and possibly a test even to make sure this doesn't get changed in the future. 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. I've added a comment now. Adding a test was not necessary as 3 tests already test this. |
||
| if (EXPECTED(!Z_ISUNDEF_P(cur))) { | ||
| fci.params = cur; | ||
| zend_result ret = zend_call_function(&fci, &fci_cache); | ||
| ZEND_ASSERT(ret == SUCCESS); | ||
| ZEND_IGNORE_VALUE(ret); | ||
| if (UNEXPECTED(Z_ISUNDEF(result))) { | ||
| ZEND_HASH_FILL_FINISH(); | ||
| zend_array_destroy(output); | ||
| RETURN_NULL(); | ||
| } | ||
| } else { | ||
| ZVAL_UNDEF(&result); | ||
| undefs++; | ||
| } | ||
| ZEND_HASH_FILL_ADD(&result); | ||
| } | ||
| } ZEND_HASH_FILL_END(); | ||
| output->nNumOfElements -= undefs; | ||
| } else { | ||
| zend_ulong num_key; | ||
| zend_string *str_key; | ||
|
|
||
| array_init_size(return_value, maxlen); | ||
| HashTable *output = Z_ARRVAL_P(return_value); | ||
| zend_hash_real_init_mixed(output); | ||
|
|
||
| ZEND_HASH_MAP_FOREACH_KEY_VAL(input, num_key, str_key, fci.params) { | ||
| zend_result ret = zend_call_function(&fci, &fci_cache); | ||
| ZEND_ASSERT(ret == SUCCESS); | ||
Girgias marked this conversation as resolved.
Show resolved
Hide resolved
|
||
| ZEND_IGNORE_VALUE(ret); | ||
| if (UNEXPECTED(Z_ISUNDEF(result))) { | ||
| zend_array_destroy(output); | ||
| RETURN_NULL(); | ||
| } | ||
| if (str_key) { | ||
| _zend_hash_append(output, str_key, &result); | ||
| } else { | ||
| zend_hash_index_add_new(output, num_key, &result); | ||
| } | ||
| } ZEND_HASH_FOREACH_END(); | ||
| } | ||
| } else { | ||
| uint32_t *array_pos = (HashPosition *)ecalloc(n_arrays, sizeof(HashPosition)); | ||
|
|
||
|
|
@@ -6874,7 +6903,11 @@ PHP_FUNCTION(array_map) | |
| fci.param_count = n_arrays; | ||
| fci.params = params; | ||
|
|
||
| if (zend_call_function(&fci, &fci_cache) != SUCCESS || Z_TYPE(result) == IS_UNDEF) { | ||
| zend_result ret = zend_call_function(&fci, &fci_cache); | ||
| ZEND_ASSERT(ret == SUCCESS); | ||
| ZEND_IGNORE_VALUE(ret); | ||
|
|
||
nielsdos marked this conversation as resolved.
Show resolved
Hide resolved
|
||
| if (Z_TYPE(result) == IS_UNDEF) { | ||
| efree(array_pos); | ||
| zend_array_destroy(Z_ARR_P(return_value)); | ||
| for (i = 0; i < n_arrays; i++) { | ||
|
|
||
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.
Any reason this isn't moved up so that the two
HT_IS_PACKED(input)branches can be joined?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.
No reason, I'll merge them.