Skip to content

Commit 4381895

Browse files
kaja47nikic
authored andcommitted
Speed up array_column for consecutive objects of the same class
1 parent 122deea commit 4381895

File tree

1 file changed

+10
-7
lines changed

1 file changed

+10
-7
lines changed

ext/standard/array.c

Lines changed: 10 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -4018,7 +4018,7 @@ PHP_FUNCTION(array_count_values)
40184018
}
40194019
/* }}} */
40204020

4021-
static inline zval *array_column_fetch_prop(zval *data, zend_string *name_str, zend_long name_long, zval *rv) /* {{{ */
4021+
static inline zval *array_column_fetch_prop(zval *data, zend_string *name_str, zend_long name_long, void **cache_slot, zval *rv) /* {{{ */
40224022
{
40234023
zval *prop = NULL;
40244024

@@ -4033,9 +4033,9 @@ static inline zval *array_column_fetch_prop(zval *data, zend_string *name_str, z
40334033
/* The has_property check is first performed in "exists" mode (which returns true for
40344034
* properties that are null but exist) and then in "has" mode to handle objects that
40354035
* implement __isset (which is not called in "exists" mode). */
4036-
if (Z_OBJ_HANDLER_P(data, has_property)(Z_OBJ_P(data), tmp_str, ZEND_PROPERTY_EXISTS, NULL)
4037-
|| Z_OBJ_HANDLER_P(data, has_property)(Z_OBJ_P(data), tmp_str, ZEND_PROPERTY_ISSET, NULL)) {
4038-
prop = Z_OBJ_HANDLER_P(data, read_property)(Z_OBJ_P(data), tmp_str, BP_VAR_R, NULL, rv);
4036+
if (Z_OBJ_HANDLER_P(data, has_property)(Z_OBJ_P(data), tmp_str, ZEND_PROPERTY_EXISTS, cache_slot)
4037+
|| Z_OBJ_HANDLER_P(data, has_property)(Z_OBJ_P(data), tmp_str, ZEND_PROPERTY_ISSET, cache_slot)) {
4038+
prop = Z_OBJ_HANDLER_P(data, read_property)(Z_OBJ_P(data), tmp_str, BP_VAR_R, cache_slot, rv);
40394039
if (prop) {
40404040
ZVAL_DEREF(prop);
40414041
if (prop != rv) {
@@ -4081,6 +4081,9 @@ PHP_FUNCTION(array_column)
40814081
Z_PARAM_STR_OR_LONG_OR_NULL(index_str, index_long, index_is_null)
40824082
ZEND_PARSE_PARAMETERS_END();
40834083

4084+
void* cache_slot_column[3] = { NULL, NULL, NULL };
4085+
void* cache_slot_index[3] = { NULL, NULL, NULL };
4086+
40844087
array_init_size(return_value, zend_hash_num_elements(input));
40854088
/* Index param is not passed */
40864089
if (index_is_null) {
@@ -4091,7 +4094,7 @@ PHP_FUNCTION(array_column)
40914094
if (column_is_null) {
40924095
Z_TRY_ADDREF_P(data);
40934096
colval = data;
4094-
} else if ((colval = array_column_fetch_prop(data, column_str, column_long, &rv)) == NULL) {
4097+
} else if ((colval = array_column_fetch_prop(data, column_str, column_long, cache_slot_column, &rv)) == NULL) {
40954098
continue;
40964099
}
40974100
ZEND_HASH_FILL_ADD(colval);
@@ -4104,12 +4107,12 @@ PHP_FUNCTION(array_column)
41044107
if (column_is_null) {
41054108
Z_TRY_ADDREF_P(data);
41064109
colval = data;
4107-
} else if ((colval = array_column_fetch_prop(data, column_str, column_long, &rv)) == NULL) {
4110+
} else if ((colval = array_column_fetch_prop(data, column_str, column_long, cache_slot_column, &rv)) == NULL) {
41084111
continue;
41094112
}
41104113

41114114
zval rv;
4112-
zval *keyval = array_column_fetch_prop(data, index_str, index_long, &rv);
4115+
zval *keyval = array_column_fetch_prop(data, index_str, index_long, cache_slot_index, &rv);
41134116
if (keyval) {
41144117
array_set_zval_key(Z_ARRVAL_P(return_value), keyval, colval);
41154118
zval_ptr_dtor(colval);

0 commit comments

Comments
 (0)