Skip to content
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

Optimize performance of checking is can access object #6944

Merged

Conversation

peter-gribanov
Copy link
Contributor

Subject

Remove code duplication to improve performance.

Changelog

### Fixed

- Optimize performance of checking is can access object in `AbstractAdmin::configureActionButtons()`

Copy link
Member

@VincentLanglet VincentLanglet left a comment

Choose a reason for hiding this comment

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

The display is made this way:

    {% for item in admin.getActionButtons(action, (object is defined) ? object : null ) %}
        {% if item.template is defined %}
            {% include item.template %}
        {% endif %}
    {% endfor %}

So I assume it will change the order...

@peter-gribanov
Copy link
Contributor Author

So I assume it will change the order...

Exactly. I forgot about it.

@peter-gribanov peter-gribanov force-pushed the canAccessObject_performance branch 2 times, most recently from ac5cd50 to e35c4ab Compare March 15, 2021 09:07
phansys
phansys previously approved these changes Mar 15, 2021
&& null !== $object
$canAccessObject =
null !== $object
&& \in_array($action, ['show', 'edit', 'delete', 'acl', 'history'], true)
Copy link
Member

Choose a reason for hiding this comment

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

I'm not sure this check improve performances

Copy link
Contributor Author

Choose a reason for hiding this comment

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

If $action is show or edit then $this->id($object) call thrice. As far as i can tell, calling method AbstractAdmin::id() is not free. This is a minor performance improvement, but it removes the duplication of code.

I can improve performance for internal actions by removing this line:

$canAccessObject =
    null !== $object
-    && \in_array($action, ['show', 'edit', 'delete', 'acl', 'history'], true)
    && null !== $this->id($object)
;

VincentLanglet
VincentLanglet previously approved these changes Mar 16, 2021
Copy link
Member

@VincentLanglet VincentLanglet left a comment

Choose a reason for hiding this comment

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

Tests are failing

@@ -2172,7 +2172,7 @@ public function testGetActionButtonsListWithoutExtraChecks(): void
$admin->method('isAclEnabled')->willReturn(true);
$admin->method('getExtensions')->willReturn([]);

$admin->expects($this->exactly(9))->method('hasRoute')->willReturn(false);
$admin->expects($this->exactly(4))->method('hasRoute')->willReturn(false);
Copy link
Contributor Author

Choose a reason for hiding this comment

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

Another example of performance optimization.

VincentLanglet
VincentLanglet previously approved these changes Mar 16, 2021
// nothing to do for non-internal actions
if (!\in_array($action, ['tree', 'show', 'edit', 'delete', 'acl', 'history', 'list', 'batch'], true)) {
return [];
}
Copy link
Contributor Author

Choose a reason for hiding this comment

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

All the following code for this method does not make sense for custom actions. Perhaps it would be better to immediately identify these custom actions?

Copy link
Member

Choose a reason for hiding this comment

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

indeed

Copy link
Contributor

Choose a reason for hiding this comment

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

Can someone explain to me why this is the case? I have custom actions from where I'd like to be able to move back to "edit" mode.

Copy link
Member

Choose a reason for hiding this comment

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

there is a configureActionButtons method for this.

Copy link
Member

Choose a reason for hiding this comment

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

Oh, you meant the early return ?

All the logic here doesn't make sens for custom action, the call to configureActionButtons still does. Can you open a pr @toooni ?

Copy link
Contributor

Choose a reason for hiding this comment

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

Yes, the early return. Yes, the configureActionButtons call does always make sense and I can make a PR. But why does an edit action button not make sense in a custom action? I'd think the default behaviour should be that the the edit (and other actions) should be visible like in internal actions (these could still be unset in configureActionButtons).

Copy link
Member

Choose a reason for hiding this comment

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

I recommend you to open the PR with what you think is the good behavior and we'll discuss about it.

But why does an edit action button would make sens in a custom action too ? For each actions, we have a check like

self::MASK_OF_ACTION_EDIT & $actionBit

Removing the early return won't add automatically the edit button in your custom action, and I think it's the right move. Custom action are custom, so buttons should be configured.

Copy link
Contributor Author

Choose a reason for hiding this comment

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

It has always been a challenge to add standard action buttons to custom action pages. But this must always be manually configured. Not every custom page needs to add an edit button.

Copy link
Contributor

Choose a reason for hiding this comment

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

Ok. I might be a bit biased because in my cases I could always need the edit action button. I've created a PR #7483 to allow configureActionButtons again.

Btw. maybe the default action buttons should be predefined somewhere so it would be easier to re-add them in custom actions.

VincentLanglet
VincentLanglet previously approved these changes Mar 17, 2021
phansys
phansys previously approved these changes Mar 17, 2021
Copy link
Member

@phansys phansys left a comment

Choose a reason for hiding this comment

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

Maybe we could use a bitmask in order to avoid the multiple calls to in_array().

@peter-gribanov
Copy link
Contributor Author

Maybe we could use a bitmask in order to avoid the multiple calls to in_array().

Interesting idea. The performance will clearly be better. It may be easier to manage this.

@@ -72,6 +72,34 @@ abstract class AbstractAdmin extends AbstractTaggedAdmin implements AdminInterfa
Doctrine\\\Orm|Doctrine\\\Phpcr|Doctrine\\\MongoDB|Doctrine\\\CouchDB
)\\\(.*)@x';

private const ACTION_TREE = 1;
// private const ACTION_CREATE = 2; // not used now
Copy link
Member

Choose a reason for hiding this comment

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

Why commenting this ?

Copy link
Contributor Author

Choose a reason for hiding this comment

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

I suggest we reserve a bit for the Create action. But the conditions for the Create button are not described in method AbstractAdmin::configureActionButtons() and this constant is not used.

image

Would you rather reserve a bit, remove unused code, or leave dead code?

Copy link
Member

Choose a reason for hiding this comment

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

If we need it, we could add it as the last bit so it's maybe not necessary to reserve a bit.
But if we reserve a bit, I prefer to create the const rather than having a comment.

Maybe @sonata-project/contributors think differently

Copy link
Contributor Author

Choose a reason for hiding this comment

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

I removed the commented code.

@VincentLanglet VincentLanglet requested review from a team and phansys March 24, 2021 19:13
VincentLanglet
VincentLanglet previously approved these changes Mar 29, 2021
@VincentLanglet VincentLanglet requested a review from a team March 29, 2021 13:56
src/Admin/AbstractAdmin.php Outdated Show resolved Hide resolved
Copy link
Member

@phansys phansys left a comment

Choose a reason for hiding this comment

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

As mentioned in a previous comment, I'd use MASK_ACTION_* instead of ACTION_*_IN constants, but since they are private, I think it isn't a blocker.

use bitmap in AbstractAdmin::configureActionButtons()

not reserve a bit for the Create action

rename mask constants
@peter-gribanov
Copy link
Contributor Author

@phansys i didn't notice your comment. I change the name of the constants to MASK_OF_ACTION_* and MASK_OF_ACTIONS_USING_OBJECT.

@phansys phansys requested review from VincentLanglet and a team March 29, 2021 15:35
@VincentLanglet VincentLanglet merged commit 3997b4f into sonata-project:3.x Mar 29, 2021
@VincentLanglet
Copy link
Member

Thanks @peter-gribanov

@peter-gribanov peter-gribanov deleted the canAccessObject_performance branch March 30, 2021 08:04
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Projects
None yet
Development

Successfully merging this pull request may close these issues.

None yet

4 participants