Skip to content

Zend: Add zend_check_method_accessible() to DRY method visibility checks #18995

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

Open
wants to merge 3 commits into
base: master
Choose a base branch
from

Conversation

TimWolla
Copy link
Member

@TimWolla TimWolla commented Jul 1, 2025

To (partly) follow up on #18919 (comment) and #18919 (comment). Best reviewed with whitespace changes ignored.

@TimWolla TimWolla requested review from iluuu1994 and nielsdos July 1, 2025 10:01
@TimWolla TimWolla requested a review from dstogov as a code owner July 1, 2025 10:01
@TimWolla TimWolla force-pushed the dry-method-visibility-checks branch 2 times, most recently from 270af52 to 9c89298 Compare July 1, 2025 10:29
static inline bool zend_check_method_accessible(const zend_function *fn, const zend_class_entry *scope)
{
if (
!(fn->common.fn_flags & ZEND_ACC_PUBLIC)
Copy link
Member

Choose a reason for hiding this comment

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

I'd prefer making this an assert, as many of the call-sites have already accounted for this. Some of them are quite hot too, and there's no guarantee the compiler will omit the repeated check.

Copy link
Member

Choose a reason for hiding this comment

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

Symfony instruction count went up by 0.01%. Seems to confirm this is not omitted, or the function isn't inlined.

Copy link
Member Author

Choose a reason for hiding this comment

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

Indeed. For zend_std_get_constructor it appears the flag is rechecked. I hate this, the point of the method was that I don't need to think about this 😒

Copy link
Member

Choose a reason for hiding this comment

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

Note though that the increase of 0.01% could also be noise. It's within the noise range that I often see on PRs. Looking at the assembly would confirm whether inlining happened or not.

@iluuu1994
Copy link
Member

I'm also not sure if the inline should be zend_always_inline. Might be worth checking the assembly whether the inlining happens.

Copy link
Member

@nielsdos nielsdos left a comment

Choose a reason for hiding this comment

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

Nice cleanup.

Comment on lines +142 to +145
if (!(fn->common.fn_flags & ZEND_ACC_PUBLIC)
&& fn->common.scope != scope
&& (UNEXPECTED(fn->common.fn_flags & ZEND_ACC_PRIVATE)
|| UNEXPECTED(!zend_check_protected(zend_get_function_root_class(fn), scope)))) {
Copy link
Member

Choose a reason for hiding this comment

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

Typically the style is to indent subconditions with spaces. One space for every sublevel. I.e.

Suggested change
if (!(fn->common.fn_flags & ZEND_ACC_PUBLIC)
&& fn->common.scope != scope
&& (UNEXPECTED(fn->common.fn_flags & ZEND_ACC_PRIVATE)
|| UNEXPECTED(!zend_check_protected(zend_get_function_root_class(fn), scope)))) {
if (!(fn->common.fn_flags & ZEND_ACC_PUBLIC)
&& fn->common.scope != scope
&& (UNEXPECTED(fn->common.fn_flags & ZEND_ACC_PRIVATE)
|| UNEXPECTED(!zend_check_protected(zend_get_function_root_class(fn), scope)))) {
ZVAL_UNDEF(EX_VAR(opline->result.var));
HANDLE_EXCEPTION();
}
if (!zend_check_method_accessible(clone, scope)) {
Copy link
Member

Choose a reason for hiding this comment

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

If inlining is not guaranteed then the extra check on the public flag is not eliminated. always inlining might be better.

static inline bool zend_check_method_accessible(const zend_function *fn, const zend_class_entry *scope)
{
if (
!(fn->common.fn_flags & ZEND_ACC_PUBLIC)
Copy link
Member

Choose a reason for hiding this comment

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

Note though that the increase of 0.01% could also be noise. It's within the noise range that I often see on PRs. Looking at the assembly would confirm whether inlining happened or not.

@TimWolla TimWolla force-pushed the dry-method-visibility-checks branch from 98dc4f0 to db4b7ee Compare July 2, 2025 06:49
zend_object_store_ctor_failed(zobj);
constructor = NULL;
}
if (!zend_check_method_accessible(constructor, scope)) {
Copy link
Member

@nielsdos nielsdos Jul 2, 2025

Choose a reason for hiding this comment

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

To solve the fn_flags reload problem, just insert an ZEND_ASSERT/ZEND_ASSUME here, right before the call to zend_check_method_accessible (preferably an assertion).

Copy link
Member Author

Choose a reason for hiding this comment

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

Ah, good idea. That would keep zend_check_method_accessible() easy to use compared to asserting within zend_check_method_accessible() itself. I'll check this tomorrow.

Copy link
Member Author

Choose a reason for hiding this comment

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

Now done. The CI Benchmark still sees a 0.01% increase, which now might actually be noise then?

Copy link
Member

Choose a reason for hiding this comment

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

Can't check now. You didn't specify always_inline though. I also see 0.02 on symfony

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