Skip to content

Optimize strlen() and in_array() opcodes #4981

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Closed
wants to merge 1 commit into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
66 changes: 49 additions & 17 deletions Zend/zend_vm_def.h
Original file line number Diff line number Diff line change
Expand Up @@ -8286,7 +8286,9 @@ ZEND_VM_COLD_CONST_HANDLER(121, ZEND_STRLEN, CONST|TMPVAR|CV, ANY)
value = GET_OP1_ZVAL_PTR_UNDEF(BP_VAR_R);
if (EXPECTED(Z_TYPE_P(value) == IS_STRING)) {
ZVAL_LONG(EX_VAR(opline->result.var), Z_STRLEN_P(value));
FREE_OP1();
if (OP1_TYPE & (IS_TMP_VAR|IS_VAR)) {
zval_ptr_dtor_str(value);
}
ZEND_VM_NEXT_OPCODE();
} else {
bool strict;
Expand Down Expand Up @@ -8897,33 +8899,63 @@ ZEND_VM_COLD_CONSTCONST_HANDLER(189, ZEND_IN_ARRAY, CONST|TMP|VAR|CV, CONST, NUM
HashTable *ht = Z_ARRVAL_P(RT_CONSTANT(opline, opline->op2));
zval *result;

SAVE_OPLINE();
op1 = GET_OP1_ZVAL_PTR_DEREF(BP_VAR_R);
op1 = GET_OP1_ZVAL_PTR_UNDEF(BP_VAR_R);
if (EXPECTED(Z_TYPE_P(op1) == IS_STRING)) {
result = zend_hash_find_ex(ht, Z_STR_P(op1), OP1_TYPE == IS_CONST);
if (OP1_TYPE & (IS_TMP_VAR|IS_VAR)) {
zval_ptr_dtor_str(op1);
}
ZEND_VM_SMART_BRANCH(result, 0);
} else if (opline->extended_value) {
if (EXPECTED(Z_TYPE_P(op1) == IS_LONG)) {
result = zend_hash_index_find(ht, Z_LVAL_P(op1));
} else {
ZEND_VM_SMART_BRANCH(result, 0);
}
if (OP1_TYPE & (IS_CV | IS_VAR)) {
SAVE_OPLINE();
if ((OP1_TYPE & IS_CV) && UNEXPECTED(Z_TYPE_P(op1) == IS_UNDEF)) {
ZVAL_UNDEFINED_OP1();
ZEND_VM_SMART_BRANCH(0, 1);
}
result = NULL;
ZVAL_DEREF(op1);
if (Z_TYPE_P(op1) == IS_STRING) {
result = zend_hash_find_ex(ht, Z_STR_P(op1), OP1_TYPE == IS_CONST);
} else if (Z_TYPE_P(op1) == IS_LONG) {
result = zend_hash_index_find(ht, Z_LVAL_P(op1));
}
Comment on lines +8922 to +8926
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

These checks make sense only if op1 was IS_REFERENCE

FREE_OP1();
ZEND_VM_SMART_BRANCH(result, OP1_TYPE & IS_VAR);
}
ZEND_VM_SMART_BRANCH(0, 0);
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

FREE_OP1() and exception checks missing

} else if (Z_TYPE_P(op1) <= IS_FALSE) {
if (OP1_TYPE == IS_CV && UNEXPECTED(Z_TYPE_P(op1) == IS_UNDEF)) {
SAVE_OPLINE();
ZVAL_UNDEFINED_OP1();
result = zend_hash_find_ex(ht, ZSTR_EMPTY_ALLOC(), 1);
FREE_OP1();
ZEND_VM_SMART_BRANCH(result, 1);
Comment on lines +8935 to +8937
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

It doesn't make a lot of sense to over-optimize for exceptional cases.

}
result = zend_hash_find_ex(ht, ZSTR_EMPTY_ALLOC(), 1);
} else {
zend_string *key;
zval key_tmp, *val;

result = NULL;
ZEND_HASH_FOREACH_STR_KEY_VAL(ht, key, val) {
ZVAL_STR(&key_tmp, key);
if (zend_compare(op1, &key_tmp) == 0) {
result = val;
break;
}
} ZEND_HASH_FOREACH_END();
ZEND_VM_SMART_BRANCH(result, 0);
}
SAVE_OPLINE();
zend_string *key;
zval key_tmp, *val;
if (OP1_TYPE & (IS_CV | IS_VAR)) {
ZVAL_DEREF(op1);
}

result = NULL;
ZEND_HASH_FOREACH_STR_KEY_VAL(ht, key, val) {
ZVAL_STR(&key_tmp, key);
if (zend_compare(op1, &key_tmp) == 0) {
result = val;
break;
}
} ZEND_HASH_FOREACH_END();
FREE_OP1();
ZEND_VM_SMART_BRANCH(result, 1);
ZEND_VM_SMART_BRANCH(result, OP1_TYPE & (IS_TMP_VAR|IS_VAR));
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

zend_compare() may throw exception

}

ZEND_VM_COLD_CONST_HANDLER(190, ZEND_COUNT, CONST|TMPVAR|CV, UNUSED)
Expand Down
Loading