-
-
Notifications
You must be signed in to change notification settings - Fork 1.3k
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
Optimize performance of checking is can access object #6944
Conversation
94e0fbf
to
d29a3bb
Compare
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 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...
Exactly. I forgot about it. |
ac5cd50
to
e35c4ab
Compare
src/Admin/AbstractAdmin.php
Outdated
&& null !== $object | ||
$canAccessObject = | ||
null !== $object | ||
&& \in_array($action, ['show', 'edit', 'delete', 'acl', 'history'], true) |
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'm not sure this check improve performances
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.
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)
;
e35c4ab
to
99f8db4
Compare
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.
Tests are failing
99f8db4
to
9c9d1c9
Compare
@@ -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); |
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.
Another example of performance optimization.
9c9d1c9
to
c21b11b
Compare
// nothing to do for non-internal actions | ||
if (!\in_array($action, ['tree', 'show', 'edit', 'delete', 'acl', 'history', 'list', 'batch'], true)) { | ||
return []; | ||
} |
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.
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?
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.
indeed
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.
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.
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.
there is a configureActionButtons
method for this.
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.
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 ?
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.
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
).
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 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.
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.
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.
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.
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.
c21b11b
to
86c2f2c
Compare
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.
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. |
4db6edf
86c2f2c
to
4db6edf
Compare
4db6edf
to
9b2873a
Compare
src/Admin/AbstractAdmin.php
Outdated
@@ -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 |
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.
Why commenting this ?
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.
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.
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
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 removed the commented code.
296dd6b
to
5406a64
Compare
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.
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
5406a64
to
01dad35
Compare
@phansys i didn't notice your comment. I change the name of the constants to |
Thanks @peter-gribanov |
Subject
Remove code duplication to improve performance.
Changelog