Skip to content

Commit dcf7592

Browse files
committed
Optimize array to object casting
1 parent ca40664 commit dcf7592

File tree

2 files changed

+65
-5
lines changed

2 files changed

+65
-5
lines changed

Zend/zend_vm_def.h

Lines changed: 13 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -5309,7 +5309,7 @@ ZEND_VM_COLD_CONST_HANDLER(21, ZEND_CAST, CONST|TMP|VAR|CV, ANY, TYPE)
53095309
}
53105310

53115311
if (opline->extended_value == IS_ARRAY) {
5312-
if (Z_TYPE_P(expr) != IS_OBJECT) {
5312+
if (OP1_TYPE == IS_CONST || Z_TYPE_P(expr) != IS_OBJECT || Z_OBJCE_P(expr) == zend_ce_closure) {
53135313
if (Z_TYPE_P(expr) != IS_NULL) {
53145314
ZVAL_ARR(result, zend_new_array(1));
53155315
expr = zend_hash_index_add_new(Z_ARRVAL_P(result), 0, expr);
@@ -5321,6 +5321,18 @@ ZEND_VM_COLD_CONST_HANDLER(21, ZEND_CAST, CONST|TMP|VAR|CV, ANY, TYPE)
53215321
} else {
53225322
ZVAL_EMPTY_ARRAY(result);
53235323
}
5324+
} else if (Z_OBJ_HT_P(expr)->get_properties) {
5325+
HashTable *obj_ht = Z_OBJ_HT_P(expr)->get_properties(expr);
5326+
if (obj_ht) {
5327+
/* fast copy */
5328+
obj_ht = zend_proptable_to_symtable(obj_ht,
5329+
(Z_OBJCE_P(expr)->default_properties_count ||
5330+
Z_OBJ_P(expr)->handlers != &std_object_handlers ||
5331+
GC_IS_RECURSIVE(obj_ht)));
5332+
ZVAL_ARR(result, obj_ht);
5333+
} else {
5334+
ZVAL_EMPTY_ARRAY(result);
5335+
}
53245336
} else {
53255337
ZVAL_COPY_VALUE(result, expr);
53265338
Z_ADDREF_P(result);

Zend/zend_vm_execute.h

Lines changed: 52 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -3251,7 +3251,7 @@ static ZEND_VM_COLD ZEND_OPCODE_HANDLER_RET ZEND_FASTCALL ZEND_CAST_SPEC_CONST_H
32513251
}
32523252

32533253
if (opline->extended_value == IS_ARRAY) {
3254-
if (Z_TYPE_P(expr) != IS_OBJECT) {
3254+
if (IS_CONST == IS_CONST || Z_TYPE_P(expr) != IS_OBJECT || Z_OBJCE_P(expr) == zend_ce_closure) {
32553255
if (Z_TYPE_P(expr) != IS_NULL) {
32563256
ZVAL_ARR(result, zend_new_array(1));
32573257
expr = zend_hash_index_add_new(Z_ARRVAL_P(result), 0, expr);
@@ -3263,6 +3263,18 @@ static ZEND_VM_COLD ZEND_OPCODE_HANDLER_RET ZEND_FASTCALL ZEND_CAST_SPEC_CONST_H
32633263
} else {
32643264
ZVAL_EMPTY_ARRAY(result);
32653265
}
3266+
} else if (Z_OBJ_HT_P(expr)->get_properties) {
3267+
HashTable *obj_ht = Z_OBJ_HT_P(expr)->get_properties(expr);
3268+
if (obj_ht) {
3269+
/* fast copy */
3270+
obj_ht = zend_proptable_to_symtable(obj_ht,
3271+
(Z_OBJCE_P(expr)->default_properties_count ||
3272+
Z_OBJ_P(expr)->handlers != &std_object_handlers ||
3273+
GC_IS_RECURSIVE(obj_ht)));
3274+
ZVAL_ARR(result, obj_ht);
3275+
} else {
3276+
ZVAL_EMPTY_ARRAY(result);
3277+
}
32663278
} else {
32673279
ZVAL_COPY_VALUE(result, expr);
32683280
Z_ADDREF_P(result);
@@ -18075,7 +18087,7 @@ static ZEND_OPCODE_HANDLER_RET ZEND_FASTCALL ZEND_CAST_SPEC_TMP_HANDLER(ZEND_OPC
1807518087
}
1807618088

1807718089
if (opline->extended_value == IS_ARRAY) {
18078-
if (Z_TYPE_P(expr) != IS_OBJECT) {
18090+
if (IS_TMP_VAR == IS_CONST || Z_TYPE_P(expr) != IS_OBJECT || Z_OBJCE_P(expr) == zend_ce_closure) {
1807918091
if (Z_TYPE_P(expr) != IS_NULL) {
1808018092
ZVAL_ARR(result, zend_new_array(1));
1808118093
expr = zend_hash_index_add_new(Z_ARRVAL_P(result), 0, expr);
@@ -18087,6 +18099,18 @@ static ZEND_OPCODE_HANDLER_RET ZEND_FASTCALL ZEND_CAST_SPEC_TMP_HANDLER(ZEND_OPC
1808718099
} else {
1808818100
ZVAL_EMPTY_ARRAY(result);
1808918101
}
18102+
} else if (Z_OBJ_HT_P(expr)->get_properties) {
18103+
HashTable *obj_ht = Z_OBJ_HT_P(expr)->get_properties(expr);
18104+
if (obj_ht) {
18105+
/* fast copy */
18106+
obj_ht = zend_proptable_to_symtable(obj_ht,
18107+
(Z_OBJCE_P(expr)->default_properties_count ||
18108+
Z_OBJ_P(expr)->handlers != &std_object_handlers ||
18109+
GC_IS_RECURSIVE(obj_ht)));
18110+
ZVAL_ARR(result, obj_ht);
18111+
} else {
18112+
ZVAL_EMPTY_ARRAY(result);
18113+
}
1809018114
} else {
1809118115
ZVAL_COPY_VALUE(result, expr);
1809218116
Z_ADDREF_P(result);
@@ -21118,7 +21142,7 @@ static ZEND_OPCODE_HANDLER_RET ZEND_FASTCALL ZEND_CAST_SPEC_VAR_HANDLER(ZEND_OPC
2111821142
}
2111921143

2112021144
if (opline->extended_value == IS_ARRAY) {
21121-
if (Z_TYPE_P(expr) != IS_OBJECT) {
21145+
if (IS_VAR == IS_CONST || Z_TYPE_P(expr) != IS_OBJECT || Z_OBJCE_P(expr) == zend_ce_closure) {
2112221146
if (Z_TYPE_P(expr) != IS_NULL) {
2112321147
ZVAL_ARR(result, zend_new_array(1));
2112421148
expr = zend_hash_index_add_new(Z_ARRVAL_P(result), 0, expr);
@@ -21130,6 +21154,18 @@ static ZEND_OPCODE_HANDLER_RET ZEND_FASTCALL ZEND_CAST_SPEC_VAR_HANDLER(ZEND_OPC
2113021154
} else {
2113121155
ZVAL_EMPTY_ARRAY(result);
2113221156
}
21157+
} else if (Z_OBJ_HT_P(expr)->get_properties) {
21158+
HashTable *obj_ht = Z_OBJ_HT_P(expr)->get_properties(expr);
21159+
if (obj_ht) {
21160+
/* fast copy */
21161+
obj_ht = zend_proptable_to_symtable(obj_ht,
21162+
(Z_OBJCE_P(expr)->default_properties_count ||
21163+
Z_OBJ_P(expr)->handlers != &std_object_handlers ||
21164+
GC_IS_RECURSIVE(obj_ht)));
21165+
ZVAL_ARR(result, obj_ht);
21166+
} else {
21167+
ZVAL_EMPTY_ARRAY(result);
21168+
}
2113321169
} else {
2113421170
ZVAL_COPY_VALUE(result, expr);
2113521171
Z_ADDREF_P(result);
@@ -37227,7 +37263,7 @@ static ZEND_OPCODE_HANDLER_RET ZEND_FASTCALL ZEND_CAST_SPEC_CV_HANDLER(ZEND_OPCO
3722737263
}
3722837264

3722937265
if (opline->extended_value == IS_ARRAY) {
37230-
if (Z_TYPE_P(expr) != IS_OBJECT) {
37266+
if (IS_CV == IS_CONST || Z_TYPE_P(expr) != IS_OBJECT || Z_OBJCE_P(expr) == zend_ce_closure) {
3723137267
if (Z_TYPE_P(expr) != IS_NULL) {
3723237268
ZVAL_ARR(result, zend_new_array(1));
3723337269
expr = zend_hash_index_add_new(Z_ARRVAL_P(result), 0, expr);
@@ -37239,6 +37275,18 @@ static ZEND_OPCODE_HANDLER_RET ZEND_FASTCALL ZEND_CAST_SPEC_CV_HANDLER(ZEND_OPCO
3723937275
} else {
3724037276
ZVAL_EMPTY_ARRAY(result);
3724137277
}
37278+
} else if (Z_OBJ_HT_P(expr)->get_properties) {
37279+
HashTable *obj_ht = Z_OBJ_HT_P(expr)->get_properties(expr);
37280+
if (obj_ht) {
37281+
/* fast copy */
37282+
obj_ht = zend_proptable_to_symtable(obj_ht,
37283+
(Z_OBJCE_P(expr)->default_properties_count ||
37284+
Z_OBJ_P(expr)->handlers != &std_object_handlers ||
37285+
GC_IS_RECURSIVE(obj_ht)));
37286+
ZVAL_ARR(result, obj_ht);
37287+
} else {
37288+
ZVAL_EMPTY_ARRAY(result);
37289+
}
3724237290
} else {
3724337291
ZVAL_COPY_VALUE(result, expr);
3724437292
Z_ADDREF_P(result);

0 commit comments

Comments
 (0)