Skip to content

Commit fbdded1

Browse files
committed
Use interned string for calling count() in Zend VM
Similar to f0dd79a Copied from GH-7695
1 parent c5d6f59 commit fbdded1

File tree

4 files changed

+15
-8
lines changed

4 files changed

+15
-8
lines changed

Zend/zend_string.h

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -574,6 +574,7 @@ EMPTY_SWITCH_DEFAULT_CASE()
574574
_(ZEND_STR_OFFSETSET, "offsetset") \
575575
_(ZEND_STR_OFFSETEXISTS, "offsetexists") \
576576
_(ZEND_STR_OFFSETUNSET, "offsetunset") \
577+
_(ZEND_STR_COUNT, "count") \
577578

578579

579580
typedef enum _zend_known_string_id {

Zend/zend_vm_def.h

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -9086,7 +9086,8 @@ ZEND_VM_COLD_CONST_HANDLER(190, ZEND_COUNT, CONST|TMPVAR|CV, UNUSED)
90869086
if (zend_class_implements_interface(zobj->ce, zend_ce_countable)) {
90879087
zval retval;
90889088

9089-
zend_call_method_with_0_params(zobj, NULL, NULL, "count", &retval);
9089+
zend_function *count_fn = zend_hash_find_ptr(&zobj->ce->function_table, ZSTR_KNOWN(ZEND_STR_COUNT));
9090+
zend_call_known_instance_method_with_0_params(count_fn, zobj, &retval);
90909091
count = zval_get_long(&retval);
90919092
zval_ptr_dtor(&retval);
90929093
break;

Zend/zend_vm_execute.h

Lines changed: 6 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -10564,7 +10564,8 @@ static ZEND_VM_COLD ZEND_OPCODE_HANDLER_RET ZEND_FASTCALL ZEND_COUNT_SPEC_CONST_
1056410564
if (zend_class_implements_interface(zobj->ce, zend_ce_countable)) {
1056510565
zval retval;
1056610566

10567-
zend_call_method_with_0_params(zobj, NULL, NULL, "count", &retval);
10567+
zend_function *count_fn = zend_hash_find_ptr(&zobj->ce->function_table, ZSTR_KNOWN(ZEND_STR_COUNT));
10568+
zend_call_known_instance_method_with_0_params(count_fn, zobj, &retval);
1056810569
count = zval_get_long(&retval);
1056910570
zval_ptr_dtor(&retval);
1057010571
break;
@@ -17857,7 +17858,8 @@ static ZEND_OPCODE_HANDLER_RET ZEND_FASTCALL ZEND_COUNT_SPEC_TMPVAR_UNUSED_HANDL
1785717858
if (zend_class_implements_interface(zobj->ce, zend_ce_countable)) {
1785817859
zval retval;
1785917860

17860-
zend_call_method_with_0_params(zobj, NULL, NULL, "count", &retval);
17861+
zend_function *count_fn = zend_hash_find_ptr(&zobj->ce->function_table, ZSTR_KNOWN(ZEND_STR_COUNT));
17862+
zend_call_known_instance_method_with_0_params(count_fn, zobj, &retval);
1786117863
count = zval_get_long(&retval);
1786217864
zval_ptr_dtor(&retval);
1786317865
break;
@@ -47584,7 +47586,8 @@ static ZEND_OPCODE_HANDLER_RET ZEND_FASTCALL ZEND_COUNT_SPEC_CV_UNUSED_HANDLER(Z
4758447586
if (zend_class_implements_interface(zobj->ce, zend_ce_countable)) {
4758547587
zval retval;
4758647588

47587-
zend_call_method_with_0_params(zobj, NULL, NULL, "count", &retval);
47589+
zend_function *count_fn = zend_hash_find_ptr(&zobj->ce->function_table, ZSTR_KNOWN(ZEND_STR_COUNT));
47590+
zend_call_known_instance_method_with_0_params(count_fn, zobj, &retval);
4758847591
count = zval_get_long(&retval);
4758947592
zval_ptr_dtor(&retval);
4759047593
break;

ext/standard/array.c

Lines changed: 6 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -702,18 +702,20 @@ PHP_FUNCTION(count)
702702
case IS_OBJECT: {
703703
zval retval;
704704
/* first, we check if the handler is defined */
705-
if (Z_OBJ_HT_P(array)->count_elements) {
705+
zend_object *zobj = Z_OBJ_P(array);
706+
if (zobj->handlers->count_elements) {
706707
RETVAL_LONG(1);
707-
if (SUCCESS == Z_OBJ_HT(*array)->count_elements(Z_OBJ_P(array), &Z_LVAL_P(return_value))) {
708+
if (SUCCESS == zobj->handlers->count_elements(zobj, &Z_LVAL_P(return_value))) {
708709
return;
709710
}
710711
if (EG(exception)) {
711712
RETURN_THROWS();
712713
}
713714
}
714715
/* if not and the object implements Countable we call its count() method */
715-
if (instanceof_function(Z_OBJCE_P(array), zend_ce_countable)) {
716-
zend_call_method_with_0_params(Z_OBJ_P(array), NULL, NULL, "count", &retval);
716+
if (instanceof_function(zobj->ce, zend_ce_countable)) {
717+
zend_function *count_fn = zend_hash_find_ptr(&zobj->ce->function_table, ZSTR_KNOWN(ZEND_STR_COUNT));
718+
zend_call_known_instance_method_with_0_params(count_fn, zobj, &retval);
717719
if (Z_TYPE(retval) != IS_UNDEF) {
718720
RETVAL_LONG(zval_get_long(&retval));
719721
zval_ptr_dtor(&retval);

0 commit comments

Comments
 (0)