Skip to content

Commit abc7780

Browse files
committed
ext/standard/array: split implementation of in_array() and array_search()
Idea to benchmark
1 parent 031b4c6 commit abc7780

File tree

1 file changed

+62
-64
lines changed

1 file changed

+62
-64
lines changed

ext/standard/array.c

Lines changed: 62 additions & 64 deletions
Original file line numberDiff line numberDiff line change
@@ -1523,88 +1523,64 @@ PHP_FUNCTION(array_walk_recursive)
15231523
}
15241524
/* }}} */
15251525

1526-
/* void php_search_array(INTERNAL_FUNCTION_PARAMETERS, int behavior)
1527-
* 0 = return boolean
1528-
* 1 = return key
1529-
*/
1530-
static inline void _php_search_array(zval *return_value, zval *value, zval *array, bool strict, int behavior) /* {{{ */
1526+
static inline void php_search_array(zval *return_value, zval *value, HashTable *array, bool strict) /* {{{ */
15311527
{
15321528
zval *entry; /* pointer to array entry */
15331529
zend_ulong num_idx;
15341530
zend_string *str_idx;
15351531

15361532
if (strict) {
15371533
if (Z_TYPE_P(value) == IS_LONG) {
1538-
ZEND_HASH_FOREACH_KEY_VAL(Z_ARRVAL_P(array), num_idx, str_idx, entry) {
1534+
ZEND_HASH_FOREACH_KEY_VAL(array, num_idx, str_idx, entry) {
15391535
ZVAL_DEREF(entry);
15401536
if (Z_TYPE_P(entry) == IS_LONG && Z_LVAL_P(entry) == Z_LVAL_P(value)) {
1541-
if (behavior == 0) {
1542-
RETURN_TRUE;
1537+
if (str_idx) {
1538+
RETURN_STR_COPY(str_idx);
15431539
} else {
1544-
if (str_idx) {
1545-
RETURN_STR_COPY(str_idx);
1546-
} else {
1547-
RETURN_LONG(num_idx);
1548-
}
1540+
RETURN_LONG(num_idx);
15491541
}
15501542
}
15511543
} ZEND_HASH_FOREACH_END();
15521544
} else {
1553-
ZEND_HASH_FOREACH_KEY_VAL(Z_ARRVAL_P(array), num_idx, str_idx, entry) {
1545+
ZEND_HASH_FOREACH_KEY_VAL(array, num_idx, str_idx, entry) {
15541546
ZVAL_DEREF(entry);
15551547
if (fast_is_identical_function(value, entry)) {
1556-
if (behavior == 0) {
1557-
RETURN_TRUE;
1548+
if (str_idx) {
1549+
RETURN_STR_COPY(str_idx);
15581550
} else {
1559-
if (str_idx) {
1560-
RETURN_STR_COPY(str_idx);
1561-
} else {
1562-
RETURN_LONG(num_idx);
1563-
}
1551+
RETURN_LONG(num_idx);
15641552
}
15651553
}
15661554
} ZEND_HASH_FOREACH_END();
15671555
}
15681556
} else {
15691557
if (Z_TYPE_P(value) == IS_LONG) {
1570-
ZEND_HASH_FOREACH_KEY_VAL(Z_ARRVAL_P(array), num_idx, str_idx, entry) {
1558+
ZEND_HASH_FOREACH_KEY_VAL(array, num_idx, str_idx, entry) {
15711559
if (fast_equal_check_long(value, entry)) {
1572-
if (behavior == 0) {
1573-
RETURN_TRUE;
1560+
if (str_idx) {
1561+
RETURN_STR_COPY(str_idx);
15741562
} else {
1575-
if (str_idx) {
1576-
RETURN_STR_COPY(str_idx);
1577-
} else {
1578-
RETURN_LONG(num_idx);
1579-
}
1563+
RETURN_LONG(num_idx);
15801564
}
15811565
}
15821566
} ZEND_HASH_FOREACH_END();
15831567
} else if (Z_TYPE_P(value) == IS_STRING) {
1584-
ZEND_HASH_FOREACH_KEY_VAL(Z_ARRVAL_P(array), num_idx, str_idx, entry) {
1568+
ZEND_HASH_FOREACH_KEY_VAL(array, num_idx, str_idx, entry) {
15851569
if (fast_equal_check_string(value, entry)) {
1586-
if (behavior == 0) {
1587-
RETURN_TRUE;
1570+
if (str_idx) {
1571+
RETURN_STR_COPY(str_idx);
15881572
} else {
1589-
if (str_idx) {
1590-
RETURN_STR_COPY(str_idx);
1591-
} else {
1592-
RETURN_LONG(num_idx);
1593-
}
1573+
RETURN_LONG(num_idx);
15941574
}
15951575
}
15961576
} ZEND_HASH_FOREACH_END();
15971577
} else {
1598-
ZEND_HASH_FOREACH_KEY_VAL(Z_ARRVAL_P(array), num_idx, str_idx, entry) {
1578+
ZEND_HASH_FOREACH_KEY_VAL(array, num_idx, str_idx, entry) {
15991579
if (fast_equal_check_function(value, entry)) {
1600-
if (behavior == 0) {
1601-
RETURN_TRUE;
1580+
if (str_idx) {
1581+
RETURN_STR_COPY(str_idx);
16021582
} else {
1603-
if (str_idx) {
1604-
RETURN_STR_COPY(str_idx);
1605-
} else {
1606-
RETURN_LONG(num_idx);
1607-
}
1583+
RETURN_LONG(num_idx);
16081584
}
16091585
}
16101586
} ZEND_HASH_FOREACH_END();
@@ -1615,30 +1591,59 @@ static inline void _php_search_array(zval *return_value, zval *value, zval *arra
16151591
}
16161592
/* }}} */
16171593

1618-
/* void php_search_array(INTERNAL_FUNCTION_PARAMETERS, int behavior)
1619-
* 0 = return boolean
1620-
* 1 = return key
1621-
*/
1622-
static inline void php_search_array(INTERNAL_FUNCTION_PARAMETERS, int behavior)
1594+
/* {{{ Searches the array for a given value and returns the corresponding key if successful */
1595+
PHP_FUNCTION(array_search)
16231596
{
1624-
zval *value, /* value to check for */
1625-
*array; /* array to check in */
1597+
zval *value; /* value to check for */
1598+
HashTable *array; /* array to check in */
16261599
bool strict = 0; /* strict comparison or not */
16271600

16281601
ZEND_PARSE_PARAMETERS_START(2, 3)
16291602
Z_PARAM_ZVAL(value)
1630-
Z_PARAM_ARRAY(array)
1603+
Z_PARAM_ARRAY_HT(array)
16311604
Z_PARAM_OPTIONAL
16321605
Z_PARAM_BOOL(strict)
16331606
ZEND_PARSE_PARAMETERS_END();
16341607

1635-
_php_search_array(return_value, value, array, strict, behavior);
1608+
php_search_array(return_value, value, array, strict);
1609+
}
1610+
/* }}} */
1611+
1612+
static inline bool php_in_array(const HashTable *ht, zval *value, bool strict)
1613+
{
1614+
if (strict) {
1615+
ZEND_HASH_FOREACH_VAL(ht, /* const */ zval *entry) {
1616+
ZVAL_DEREF(entry);
1617+
if (fast_is_identical_function(value, entry)) {
1618+
return true;
1619+
}
1620+
} ZEND_HASH_FOREACH_END();
1621+
} else {
1622+
ZEND_HASH_FOREACH_VAL(ht, /* const */ zval *entry) {
1623+
ZVAL_DEREF(entry);
1624+
if (fast_equal_check_function(value, entry)) {
1625+
return true;
1626+
}
1627+
} ZEND_HASH_FOREACH_END();
1628+
}
1629+
return false;
16361630
}
16371631

16381632
/* {{{ Checks if the given value exists in the array */
16391633
PHP_FUNCTION(in_array)
16401634
{
1641-
php_search_array(INTERNAL_FUNCTION_PARAM_PASSTHRU, 0);
1635+
zval *value; /* value to check for */
1636+
HashTable *array; /* array to check in */
1637+
bool strict = 0; /* strict comparison or not */
1638+
1639+
ZEND_PARSE_PARAMETERS_START(2, 3)
1640+
Z_PARAM_ZVAL(value)
1641+
Z_PARAM_ARRAY_HT(array)
1642+
Z_PARAM_OPTIONAL
1643+
Z_PARAM_BOOL(strict)
1644+
ZEND_PARSE_PARAMETERS_END();
1645+
1646+
RETURN_BOOL(php_in_array(array, value, strict));
16421647
}
16431648
/* }}} */
16441649

@@ -1649,7 +1654,7 @@ ZEND_FRAMELESS_FUNCTION(in_array, 2)
16491654
Z_FLF_PARAM_ZVAL(1, value);
16501655
Z_FLF_PARAM_ARRAY(2, array);
16511656

1652-
_php_search_array(return_value, value, array, false, 0);
1657+
RETURN_BOOL(php_in_array(Z_ARR_P(array), value, false));
16531658

16541659
flf_clean:;
16551660
}
@@ -1663,18 +1668,11 @@ ZEND_FRAMELESS_FUNCTION(in_array, 3)
16631668
Z_FLF_PARAM_ARRAY(2, array);
16641669
Z_FLF_PARAM_BOOL(3, strict);
16651670

1666-
_php_search_array(return_value, value, array, strict, 0);
1671+
RETURN_BOOL(php_in_array(Z_ARR_P(array), value, strict));
16671672

16681673
flf_clean:;
16691674
}
16701675

1671-
/* {{{ Searches the array for a given value and returns the corresponding key if successful */
1672-
PHP_FUNCTION(array_search)
1673-
{
1674-
php_search_array(INTERNAL_FUNCTION_PARAM_PASSTHRU, 1);
1675-
}
1676-
/* }}} */
1677-
16781676
static zend_always_inline bool php_valid_var_name(const zend_string *var_name) /* {{{ */
16791677
{
16801678
/* first 256 bits for first character, and second 256 bits for the next */

0 commit comments

Comments
 (0)