-
Notifications
You must be signed in to change notification settings - Fork 7.8k
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
Add optimizer support for prototype methods #7452
base: master
Are you sure you want to change the base?
Conversation
f0ad035
to
bdd4094
Compare
b390a1a
to
c7e5496
Compare
c483aec
to
610c6ad
Compare
@@ -387,7 +391,7 @@ public function toOptimizerTypeMask(): string { | |||
case "iterable": | |||
return "MAY_BE_ARRAY|MAY_BE_ARRAY_KEY_ANY|MAY_BE_ARRAY_OF_ANY|MAY_BE_OBJECT"; |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I think MAY_BE_ARRAY_OF_REF
would also make sense here?
@@ -387,7 +391,7 @@ public function toOptimizerTypeMask(): string { | |||
case "iterable": |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
(GitHub doesn't allow commenting on the above line for case "callable":
)
Does callable also need to include MAY_BE_ARRAY_OF_REF? If I use callable
as a parameter type hint, I can still modify fields of an array callable by reference (and see them get modified)
php > class B { public function main() { echo "in main\n"; } }
php > $class = new B();
php > $method = 'main';
php > $callable = [&$class, &$method];
php > $callable();
in main
php > function example(callable $c) { $c(); $c[1] = 'changed'; }
php > example($callable);
in main
php > var_dump($callable);
array(2) {
[0]=>
&object(B)#1 (0) {
}
[1]=>
&string(7) "changed"
}
php >
Not sure what the other implications of this would be for the optimizer, @nikic would know more (E.g. something with a callable type hint might easily cease to be a callable?)
Zend/Optimizer/zend_func_info.c
Outdated
zend_string *name = callee_func->common.function_name; | ||
if (!name) { | ||
zend_string *name = get_function_or_method_name(callee_func); | ||
if (zend_string_equals_literal_ci(name, "main")) { |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Isn't main already a valid name for a internal function or an instance method <?php function main() {}
? "(main)"
would at least avoid conflicts, though I'm not sure where else get_function_or_method_name is used
Is there a reason
c9f8b3b
to
71e152d
Compare
71e152d
to
ce8811e
Compare
ce8811e
to
c91496b
Compare
/** @return array<int, string> */ | ||
public function pickArrayKeys(array $array, int $num): array {} |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
This is not correct, the returned array keys might be integers. The correct type would be list<int|string>
.
@@ -100,18 +101,20 @@ ZEND_API int zend_func_info_rid = -1; | |||
|
|||
uint32_t zend_get_internal_func_info( | |||
const zend_function *callee_func, const zend_call_info *call_info, const zend_ssa *ssa) { | |||
if (callee_func->common.scope) { | |||
/* This is a method, not a function. */ | |||
if (callee_func->common.scope && !call_info->is_prototype) { |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Shouldn't this be callee_func->common.scope && call_info->is_prototype
? is_prototype
indicates we might be calling an overridden method.
@@ -3885,6 +3885,7 @@ static zend_always_inline zend_result _zend_update_type_info( | |||
case ZEND_DO_ICALL: | |||
case ZEND_DO_UCALL: | |||
case ZEND_DO_FCALL_BY_NAME: | |||
case ZEND_INIT_METHOD_CALL: |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
This looks wrong. ZEND_INIT_METHOD_CALL
does not define a result. Methods are called using DO_FCALL
, so this is already handled.
0002 T1 = FETCH_DIM_IS V0 int(0) | ||
0003 T0 = COALESCE T1 0005 | ||
0004 T0 = QM_ASSIGN int(0) | ||
0005 VERIFY_RETURN_TYPE T0 |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Shouldn't these be optimized away then? What does this test test?
@@ -914,6 +914,28 @@ zend_function *zend_optimizer_get_called_func( | |||
} | |||
return fbc; | |||
} | |||
} else if (opline->op1_type == IS_CONST | |||
&& Z_TYPE_P(CRT_CONSTANT(opline->op1)) == IS_OBJECT |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Objects cannot be IS_CONST
. I don't think this branch is hit. You should look at the inferred SSA variable instead.
/* Prototype methods are potentially overridden. fbc still contains useful type information. | ||
* Some optimizations may not be applied, like inlining or inferring the send-mode of superfluous args. | ||
* A method cannot be overridden if the class or method is final. */ | ||
if ((fbc->common.fn_flags & ZEND_ACC_FINAL) == 0 && |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Nit: &&
on next line.
&& !(op_array->scope->ce_flags & ZEND_ACC_TRAIT)) { | ||
zend_string *method_name = Z_STR_P(CRT_CONSTANT(opline->op2) + 1); | ||
zend_function *fbc = zend_hash_find_ptr( | ||
&op_array->scope->function_table, method_name); |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
The class here should be based on op1 though, not op_array->scope
.
No description provided.