Skip to content

Commit 4aada39

Browse files
committed
Micro-optmization
1 parent d31d4d1 commit 4aada39

File tree

1 file changed

+72
-69
lines changed

1 file changed

+72
-69
lines changed

ext/standard/array.c

Lines changed: 72 additions & 69 deletions
Original file line numberDiff line numberDiff line change
@@ -4113,18 +4113,22 @@ static inline zval *array_column_fetch_prop(zval *data, zval *name, zval *rv) /*
41134113
if (Z_OBJ_HANDLER_P(data, has_property)(data, name, 2, NULL)
41144114
|| Z_OBJ_HANDLER_P(data, has_property)(data, name, 0, NULL)) {
41154115
prop = Z_OBJ_HANDLER_P(data, read_property)(data, name, BP_VAR_R, NULL, rv);
4116+
if (prop) {
4117+
ZVAL_DEREF(prop);
4118+
}
41164119
}
41174120
} else if (Z_TYPE_P(data) == IS_ARRAY) {
41184121
if (Z_TYPE_P(name) == IS_STRING) {
41194122
prop = zend_symtable_find(Z_ARRVAL_P(data), Z_STR_P(name));
41204123
} else if (Z_TYPE_P(name) == IS_LONG) {
41214124
prop = zend_hash_index_find(Z_ARRVAL_P(data), Z_LVAL_P(name));
41224125
}
4126+
if (prop) {
4127+
ZVAL_DEREF(prop);
4128+
Z_TRY_ADDREF_P(prop);
4129+
}
41234130
}
41244131

4125-
if (prop) {
4126-
ZVAL_DEREF(prop);
4127-
}
41284132

41294133
return prop;
41304134
}
@@ -4135,98 +4139,97 @@ static inline zval *array_column_fetch_prop(zval *data, zval *name, zval *rv) /*
41354139
value_key and optionally indexed by the index_key */
41364140
PHP_FUNCTION(array_column)
41374141
{
4138-
zval *zcolumn = NULL, *zkey = NULL, *data;
4139-
HashTable *arr_hash;
4140-
zval *zcolval = NULL, *zkeyval = NULL, rvc, rvk;
4142+
HashTable *input;
4143+
zval *colval, *data, rv;
4144+
zval *column = NULL, *index = NULL;
41414145

41424146
ZEND_PARSE_PARAMETERS_START(2, 3)
4143-
Z_PARAM_ARRAY_HT(arr_hash)
4144-
Z_PARAM_ZVAL_EX(zcolumn, 1, 0)
4147+
Z_PARAM_ARRAY_HT(input)
4148+
Z_PARAM_ZVAL_EX(column, 1, 0)
41454149
Z_PARAM_OPTIONAL
4146-
Z_PARAM_ZVAL_EX(zkey, 1, 0)
4150+
Z_PARAM_ZVAL_EX(index, 1, 0)
41474151
ZEND_PARSE_PARAMETERS_END();
41484152

4149-
if ((zcolumn && !array_column_param_helper(zcolumn, "column")) ||
4150-
(zkey && !array_column_param_helper(zkey, "index"))) {
4153+
if ((column && !array_column_param_helper(column, "column")) ||
4154+
(index && !array_column_param_helper(index, "index"))) {
41514155
RETURN_FALSE;
41524156
}
41534157

4154-
array_init_size(return_value, zend_hash_num_elements(arr_hash));
4155-
if (!zkey) {
4158+
array_init_size(return_value, zend_hash_num_elements(input));
4159+
if (!index) {
41564160
zend_hash_real_init_packed(Z_ARRVAL_P(return_value));
41574161
ZEND_HASH_FILL_PACKED(Z_ARRVAL_P(return_value)) {
4158-
ZEND_HASH_FOREACH_VAL(arr_hash, data) {
4162+
ZEND_HASH_FOREACH_VAL(input, data) {
41594163
ZVAL_DEREF(data);
4160-
if (!zcolumn) {
4161-
zcolval = data;
4162-
Z_TRY_ADDREF_P(zcolval);
4163-
} else if ((zcolval = array_column_fetch_prop(data, zcolumn, &rvc)) == NULL) {
4164+
if (!column) {
4165+
Z_TRY_ADDREF_P(data);
4166+
colval = data;
4167+
} else if ((colval = array_column_fetch_prop(data, column, &rv)) == NULL) {
41644168
continue;
4165-
} else if (zcolval != &rvc) {
4166-
Z_TRY_ADDREF_P(zcolval);
41674169
}
4168-
ZEND_HASH_FILL_ADD(zcolval);
4170+
ZEND_HASH_FILL_ADD(colval);
41694171
} ZEND_HASH_FOREACH_END();
41704172
} ZEND_HASH_FILL_END();
41714173
} else {
4172-
ZEND_HASH_FOREACH_VAL(arr_hash, data) {
4174+
ZEND_HASH_FOREACH_VAL(input, data) {
41734175
ZVAL_DEREF(data);
41744176

4175-
if (!zcolumn) {
4176-
zcolval = data;
4177-
Z_TRY_ADDREF_P(zcolval);
4178-
} else if ((zcolval = array_column_fetch_prop(data, zcolumn, &rvc)) == NULL) {
4177+
if (!column) {
4178+
Z_TRY_ADDREF_P(data);
4179+
colval = data;
4180+
} else if ((colval = array_column_fetch_prop(data, column, &rv)) == NULL) {
41794181
continue;
4180-
} else if (zcolval != &rvc) {
4181-
Z_TRY_ADDREF_P(zcolval);
41824182
}
41834183

4184-
/* Failure will leave zkeyval alone which will land us on the final else block below
4184+
/* Failure will leave keyval alone which will land us on the final else block below
41854185
* which is to append the value as next_index
41864186
*/
4187-
if (zkey) {
4188-
zkeyval = array_column_fetch_prop(data, zkey, &rvk);
4189-
}
4190-
if (zkeyval) {
4191-
switch (Z_TYPE_P(zkeyval)) {
4192-
case IS_STRING:
4193-
zend_symtable_update(Z_ARRVAL_P(return_value), Z_STR_P(zkeyval), zcolval);
4194-
break;
4195-
case IS_LONG:
4196-
zend_hash_index_update(Z_ARRVAL_P(return_value), Z_LVAL_P(zkeyval), zcolval);
4197-
break;
4198-
case IS_OBJECT:
4199-
{
4200-
zend_string *tmp_key;
4201-
zend_string *key = zval_get_tmp_string(zkeyval, &tmp_key);
4202-
zend_symtable_update(Z_ARRVAL_P(return_value), key, zcolval);
4203-
zend_tmp_string_release(tmp_key);
4204-
break;
4187+
if (index) {
4188+
zval rv;
4189+
zval *keyval = array_column_fetch_prop(data, index, &rv);
4190+
4191+
if (keyval) {
4192+
switch (Z_TYPE_P(keyval)) {
4193+
case IS_STRING:
4194+
zend_symtable_update(Z_ARRVAL_P(return_value), Z_STR_P(keyval), colval);
4195+
break;
4196+
case IS_LONG:
4197+
zend_hash_index_update(Z_ARRVAL_P(return_value), Z_LVAL_P(keyval), colval);
4198+
break;
4199+
case IS_OBJECT:
4200+
{
4201+
zend_string *tmp_key;
4202+
zend_string *key = zval_get_tmp_string(keyval, &tmp_key);
4203+
zend_symtable_update(Z_ARRVAL_P(return_value), key, colval);
4204+
zend_tmp_string_release(tmp_key);
4205+
break;
4206+
}
4207+
case IS_NULL:
4208+
zend_hash_update(Z_ARRVAL_P(return_value), ZSTR_EMPTY_ALLOC(), colval);
4209+
break;
4210+
case IS_DOUBLE:
4211+
zend_hash_index_update(Z_ARRVAL_P(return_value),
4212+
zend_dval_to_lval(Z_DVAL_P(keyval)), colval);
4213+
break;
4214+
case IS_TRUE:
4215+
zend_hash_index_update(Z_ARRVAL_P(return_value), 1, colval);
4216+
break;
4217+
case IS_FALSE:
4218+
zend_hash_index_update(Z_ARRVAL_P(return_value), 0, colval);
4219+
break;
4220+
case IS_RESOURCE:
4221+
zend_hash_index_update(Z_ARRVAL_P(return_value), Z_RES_HANDLE_P(keyval), colval);
4222+
break;
4223+
default:
4224+
zend_hash_next_index_insert(Z_ARRVAL_P(return_value), colval);
4225+
break;
42054226
}
4206-
case IS_NULL:
4207-
zend_hash_update(Z_ARRVAL_P(return_value), ZSTR_EMPTY_ALLOC(), zcolval);
4208-
break;
4209-
case IS_DOUBLE:
4210-
zend_hash_index_update(Z_ARRVAL_P(return_value), zend_dval_to_lval(Z_DVAL_P(zkeyval)), zcolval);
4211-
break;
4212-
case IS_TRUE:
4213-
zend_hash_index_update(Z_ARRVAL_P(return_value), 1, zcolval);
4214-
break;
4215-
case IS_FALSE:
4216-
zend_hash_index_update(Z_ARRVAL_P(return_value), 0, zcolval);
4217-
break;
4218-
case IS_RESOURCE:
4219-
zend_hash_index_update(Z_ARRVAL_P(return_value), Z_RES_HANDLE_P(zkeyval), zcolval);
4220-
break;
4221-
default:
4222-
add_next_index_zval(return_value, zcolval);
4223-
break;
4224-
}
4225-
if (zkeyval == &rvk) {
4226-
zval_ptr_dtor(&rvk);
4227+
zval_ptr_dtor(keyval);
4228+
} else {
4229+
zend_hash_next_index_insert(Z_ARRVAL_P(return_value), colval);
42274230
}
42284231
} else {
4229-
add_next_index_zval(return_value, zcolval);
4232+
zend_hash_next_index_insert(Z_ARRVAL_P(return_value), colval);
42304233
}
42314234
} ZEND_HASH_FOREACH_END();
42324235
}

0 commit comments

Comments
 (0)