-
-
Notifications
You must be signed in to change notification settings - Fork 9.6k
[EventDispatcher] small optimization #19312
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
[EventDispatcher] small optimization #19312
Conversation
We don't make "performance optimization" without proofs that it indeed improves performance significantly. Can you tell us what kind of improvement you see with your changes? |
@fabpot Because And I did test yesterday
upd.
|
previously I seen that guys submit https://blackfire.io/ metrics with PR, may be that will solve problem? |
@@ -105,7 +105,7 @@ public function removeListener($eventName, $listener) | |||
public function hasListeners($eventName = null) | |||
{ | |||
if (null === $eventName) { | |||
return (bool) count($this->listenerIds) || (bool) count($this->listeners); | |||
return count($this->listenerIds) || count($this->listeners); |
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.
what about return $this->listenerIds || $this->listeners
?
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.
Thanks. Fixed.
Result of performance test.
+-------+------------------------+-------+------------------+---------------------+-------+
| GROUP | NAME | COUNT | TIME | SINGLE | COST |
+-------+------------------------+-------+------------------+---------------------+-------+
| Test | count($a) || count($b) | 1000 | 0.40582633018494 | 0.00040582633018494 | 123 % |
| Test | $a || $b | 1000 | 0.32871198654175 | 0.00032871198654175 | 100 % |
+-------+------------------------+-------+------------------+---------------------+-------+
👍 (with |
@@ -105,7 +105,7 @@ public function removeListener($eventName, $listener) | |||
public function hasListeners($eventName = null) | |||
{ | |||
if (null === $eventName) { | |||
return (bool) count($this->listenerIds) || (bool) count($this->listeners); | |||
return $this->listenerIds || $this->listeners; |
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.
In #19320 we rejected to replace count()
with empty()
. Imo we should be consistent with our decisions here.
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. I think the current expression better conveys the intent.
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.
#19320 was a broad PR changing everything at once. On a case by case basis, we not to me.
I this case, casting to bool feels strange. At least, I'd expect 0 < count(...)
. But personally (and I know I'm not the usual guy concerning CS), I prefer the proposed way. Less steps in my personal PHP virtual machine :)
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.
Changing this line to return 0 < count($this->listenerIds) || 0 < count($this->listeners);
would be okay for me.
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 we need to use return 0 < count($this->listenerIds) || 0 < count($this->listeners)
?
Do you want be sure, that you will get bool?
But, if I use logical operators (||, &&, and so on) I will get bool.
For example,
return 10 || 0
; you will not get 10, you will get result of logical operation, it will be true
, we do not need to convert it to bool.
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 debating this is pointless. Let's keep what we already have as it works and let's move on.
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 is better to write clear and optimized code and use comments and annotations for easy understanding?
I think, some constructions like as return 0 < count($this->listenerIds) || 0 < count($this->listeners)
is not easier to understanding, or it maybe easier, for people, who do not know php enough well.
I believe, to use code constructions that have excess of logic is not correct way to build great and fast framework.
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 am not Symfony person, and I can't decide, but whenever you attend LondonPHP meetups, good speakers advertise to write clean code [and I agree with them], which I think this PR aiming for, if it get us tiny performance improvement I think we should consider to adopt it :)
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.
Please, define "clean code" in this context.
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 someone not familiar with this piece of code, return 0 < count($this->listenerIds) || 0 < count($this->listeners);
looks much easier to understand than return (bool) count($this->listenerIds) || (bool) count($this->listeners)
and return $this->listenerIds || $this->listeners;
.
$this->listenerIds || $this->listeners
returnsbool
isset($params[0])
is faster and safer thanis_array($params)