Skip to content

Improve ZEND_NEW RC inference #13239

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

Conversation

iluuu1994
Copy link
Member

ZEND_NEW returns RC1 if the instanciated class has no constructor.

@bwoebi
Copy link
Member

bwoebi commented Jan 24, 2024

That's only true for create_object == NULL and the handler being zend_std_get_constructor, I think. For anything else we cannot do such guarantees (extension code...).

@iluuu1994
Copy link
Member Author

iluuu1994 commented Jan 24, 2024

@bwoebi I forgot about create_object. I wasn't aware of the get_constructor handler... Do you think it's enough to check zend_class_entry.default_object_handlers? It may theoretically differ from zend_object.handlers at runtime. If not, I suppose we must restrict this to user classes. Same goes for write_property in #13237.

It's worth noting that we already make some assumptions here. E.g. we're assuming that assigning to a declared, untyped property does not throw, even though an overwritten write_property might.

return !(prop_info->flags & ZEND_ACC_PUBLIC)
&& prop_info->ce != op_array->scope;

@bwoebi
Copy link
Member

bwoebi commented Jan 24, 2024

I think it should be good enough to check default_object_handlers. Yes, at runtime it may change theoretically, but at that point I think it should be the extensions responsibility to be a good citizen and either notify at minit-time by changing these.

But be aware that this is Optimizer code of which one does not necessarily see the effects when developing extensions...

@bwoebi
Copy link
Member

bwoebi commented Jan 24, 2024

E.g. we're assuming that assigning to a declared, untyped property does not throw, even though an overwritten write_property might.

:-/

I think that's not a great check. I just checked the code of the extension I maintain (dd-trace-php), we do throw on < PHP 8 in the write handler to emulate a readonly. We don't do further validity checks in our code, but as long as we don't have proper virtual properties, I guess extensions will use it to validate.

Copy link
Member

@dstogov dstogov left a comment

Choose a reason for hiding this comment

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

I don't object. Just select a good enough condition.

Does this cause any improvements in the JIT generated code?

@iluuu1994 iluuu1994 force-pushed the improve-new-rc-inference branch from e788202 to e3d7c69 Compare January 29, 2024 10:31
ZEND_NEW returns RC1 if the instanciated class has no constructor.
@iluuu1994 iluuu1994 force-pushed the improve-new-rc-inference branch from e3d7c69 to e7f4860 Compare January 29, 2024 10:37
if (ce
&& !ce->constructor
&& !ce->create_object
&& ce->default_object_handlers->get_constructor == zend_std_get_constructor) {
Copy link
Member

Choose a reason for hiding this comment

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

The last line is not necessary. If create_object is NULL then ce->default_object_handlers->get_constructor == zend_std_get_constructor is true.

Copy link
Member Author

Choose a reason for hiding this comment

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

As discussed in the other issue, the default handlers may be changed without specifying create_object. create_object may still be used to swap the handlers. Support for this could be dropped in the future, essentially making zend_object->handlers and implementation detail, i.e. a cached pointer to object->ce->default_object_handlers to avoid a pointer indirection.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

Successfully merging this pull request may close these issues.

3 participants