Skip to content

Commit 9ddc25a

Browse files
authored
Simplify array_any(), array_all(), array_find(), array_find_key() (#17978)
By returning something more semantically meaningful that SUCCESS/FAILURE we can avoid refcounting for array_all() and array_any(). Also we can avoid resetting the input values to UNDEF.
1 parent 918332c commit 9ddc25a

File tree

1 file changed

+23
-53
lines changed

1 file changed

+23
-53
lines changed

ext/standard/array.c

Lines changed: 23 additions & 53 deletions
Original file line numberDiff line numberDiff line change
@@ -6591,24 +6591,22 @@ PHP_FUNCTION(array_filter)
65916591
/* }}} */
65926592

65936593
/* {{{ Internal function to find an array element for a user closure. */
6594-
static zend_result php_array_find(const HashTable *array, zend_fcall_info fci, zend_fcall_info_cache fci_cache, zval *result_key, zval *result_value, bool negate_condition)
6594+
enum php_array_find_result {
6595+
PHP_ARRAY_FIND_EXCEPTION = -1,
6596+
PHP_ARRAY_FIND_NONE = 0,
6597+
PHP_ARRAY_FIND_SOME = 1,
6598+
};
6599+
6600+
static enum php_array_find_result php_array_find(const HashTable *array, zend_fcall_info fci, zend_fcall_info_cache fci_cache, zval *result_key, zval *result_value, bool negate_condition)
65956601
{
65966602
zend_ulong num_key;
65976603
zend_string *str_key;
65986604
zval retval;
65996605
zval args[2];
66006606
zval *operand;
66016607

6602-
if (result_value != NULL) {
6603-
ZVAL_UNDEF(result_value);
6604-
}
6605-
6606-
if (result_key != NULL) {
6607-
ZVAL_UNDEF(result_key);
6608-
}
6609-
66106608
if (zend_hash_num_elements(array) == 0) {
6611-
return SUCCESS;
6609+
return PHP_ARRAY_FIND_NONE;
66126610
}
66136611

66146612
ZEND_ASSERT(ZEND_FCI_INITIALIZED(fci));
@@ -6631,7 +6629,7 @@ static zend_result php_array_find(const HashTable *array, zend_fcall_info fci, z
66316629
ZEND_ASSERT(result == SUCCESS);
66326630

66336631
if (UNEXPECTED(Z_ISUNDEF(retval))) {
6634-
return FAILURE;
6632+
return PHP_ARRAY_FIND_EXCEPTION;
66356633
}
66366634

66376635
bool retval_true = zend_is_true(&retval);
@@ -6649,103 +6647,75 @@ static zend_result php_array_find(const HashTable *array, zend_fcall_info fci, z
66496647
ZVAL_COPY(result_key, &args[1]);
66506648
}
66516649

6652-
break;
6650+
return PHP_ARRAY_FIND_SOME;
66536651
}
66546652
} ZEND_HASH_FOREACH_END();
66556653

6656-
return SUCCESS;
6654+
return PHP_ARRAY_FIND_NONE;
66576655
}
66586656
/* }}} */
66596657

66606658
/* {{{ Search within an array and returns the first found element value. */
66616659
PHP_FUNCTION(array_find)
66626660
{
6663-
zval *array = NULL;
6661+
HashTable *array;
66646662
zend_fcall_info fci;
66656663
zend_fcall_info_cache fci_cache = empty_fcall_info_cache;
66666664

66676665
ZEND_PARSE_PARAMETERS_START(2, 2)
6668-
Z_PARAM_ARRAY(array)
6666+
Z_PARAM_ARRAY_HT(array)
66696667
Z_PARAM_FUNC(fci, fci_cache)
66706668
ZEND_PARSE_PARAMETERS_END();
66716669

6672-
if (php_array_find(Z_ARR_P(array), fci, fci_cache, NULL, return_value, false) != SUCCESS) {
6673-
RETURN_THROWS();
6674-
}
6675-
6676-
if (Z_TYPE_P(return_value) == IS_UNDEF) {
6677-
RETURN_NULL();
6678-
}
6670+
php_array_find(array, fci, fci_cache, NULL, return_value, false);
66796671
}
66806672
/* }}} */
66816673

66826674
/* {{{ Search within an array and returns the first found element key. */
66836675
PHP_FUNCTION(array_find_key)
66846676
{
6685-
zval *array = NULL;
6677+
HashTable *array;
66866678
zend_fcall_info fci;
66876679
zend_fcall_info_cache fci_cache = empty_fcall_info_cache;
66886680

66896681
ZEND_PARSE_PARAMETERS_START(2, 2)
6690-
Z_PARAM_ARRAY(array)
6682+
Z_PARAM_ARRAY_HT(array)
66916683
Z_PARAM_FUNC(fci, fci_cache)
66926684
ZEND_PARSE_PARAMETERS_END();
66936685

6694-
if (php_array_find(Z_ARR_P(array), fci, fci_cache, return_value, NULL, false) != SUCCESS) {
6695-
RETURN_THROWS();
6696-
}
6697-
6698-
if (Z_TYPE_P(return_value) == IS_UNDEF) {
6699-
RETURN_NULL();
6700-
}
6686+
php_array_find(array, fci, fci_cache, return_value, NULL, false);
67016687
}
67026688
/* }}} */
67036689

67046690
/* {{{ Checks if at least one array element satisfies a callback function. */
67056691
PHP_FUNCTION(array_any)
67066692
{
6707-
zval *array = NULL;
6693+
HashTable *array;
67086694
zend_fcall_info fci;
67096695
zend_fcall_info_cache fci_cache = empty_fcall_info_cache;
67106696

67116697
ZEND_PARSE_PARAMETERS_START(2, 2)
6712-
Z_PARAM_ARRAY(array)
6698+
Z_PARAM_ARRAY_HT(array)
67136699
Z_PARAM_FUNC(fci, fci_cache)
67146700
ZEND_PARSE_PARAMETERS_END();
67156701

6716-
if (php_array_find(Z_ARR_P(array), fci, fci_cache, return_value, NULL, false) != SUCCESS) {
6717-
RETURN_THROWS();
6718-
}
6719-
6720-
bool retval = !Z_ISUNDEF_P(return_value);
6721-
if (Z_TYPE_P(return_value) == IS_STRING) {
6722-
zval_ptr_dtor_str(return_value);
6723-
}
6724-
RETURN_BOOL(retval);
6702+
RETURN_BOOL(php_array_find(array, fci, fci_cache, NULL, NULL, false) == PHP_ARRAY_FIND_SOME);
67256703
}
67266704
/* }}} */
67276705

67286706
/* {{{ Checks if all array elements satisfy a callback function. */
67296707
PHP_FUNCTION(array_all)
67306708
{
6731-
zval *array = NULL;
6709+
HashTable *array;
67326710
zend_fcall_info fci;
67336711
zend_fcall_info_cache fci_cache = empty_fcall_info_cache;
67346712

67356713
ZEND_PARSE_PARAMETERS_START(2, 2)
6736-
Z_PARAM_ARRAY(array)
6714+
Z_PARAM_ARRAY_HT(array)
67376715
Z_PARAM_FUNC(fci, fci_cache)
67386716
ZEND_PARSE_PARAMETERS_END();
67396717

6740-
if (php_array_find(Z_ARR_P(array), fci, fci_cache, return_value, NULL, true) != SUCCESS) {
6741-
RETURN_THROWS();
6742-
}
6743-
6744-
bool retval = Z_ISUNDEF_P(return_value);
6745-
if (Z_TYPE_P(return_value) == IS_STRING) {
6746-
zval_ptr_dtor_str(return_value);
6747-
}
6748-
RETURN_BOOL(retval);
6718+
RETURN_BOOL(php_array_find(array, fci, fci_cache, NULL, NULL, true) == PHP_ARRAY_FIND_NONE);
67496719
}
67506720
/* }}} */
67516721

0 commit comments

Comments
 (0)