forked from nette/utils
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
ObjectMixin: optimized call() performance for events
- Loading branch information
Showing
1 changed file
with
11 additions
and
5 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
ac853ac
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.
Did you a benchmark test? preg is usually faster than calling more statements.
And why did you remove $prop =
FALSE
at beginning?ac853ac
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, I've measure the impact. This is faster, at least on my laptop. The
preg_match
makes the entire event call (in worst-case / most-common scenario) 10 % slower. Removing the$prop = FALSE
is micro optimization with barely measurable results. I can change it back.ac853ac
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.
10 % of 100 ms is nice, 10 % of 100 µs is premature optimization. How much it saves when you run one request in regular application?
ac853ac
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.
Nette itself uses only few event calls. But application based on Nette can use a lot more events. I opened a thread on forum where I discuss real numbers and possible optimizations http://forum.nette.org/en/21287-possible-events-performance-optimizations
btw: and it's 20 % (100 vs. 120 ms for 10k unique event calls), I did the math wrong =)
ac853ac
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 understand, but optimization like „removing $prop = FALSE for the price of new lines of code and less readability“ are absolutely useless.
preg_match is called once per property per request. When its performance is nearly the same as optimized performance, I vote for better readability.
You can replace
$methods
with$m
,ObjectMixin
withO
, everything will make Nette faster, but it is really not good idea.ac853ac
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.
Moving
$methods
in condition is ok, but keep in mind operator precedence.ac853ac
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.
You can argue that entire event system is fast enough (therefore the absolute speedup will always be low), but not that code with 20 % speedup is nearly the same. 20 % is a lot faster.
It should be OK, otherwise I assume that tests would fail. Should I add brackets to make it more obvious?
ac853ac
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.
Braces will make it more obvious, because it works only thanks to
&
.ac853ac
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 unable to measure any speed-up with
strncmp($name, 'on', 2)
variant…ac853ac
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.
How did you measure it?
Test only for the line:
Results are 126 ms vs. 53 ms on my laptop with PHP 5.6.2 (the difference is even larger with PHP 7.0.0-dev).
ac853ac
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 is possible to do it even faster :-)
ac853ac
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.
Hm, I've tried both
ord($name[2]) >> 5 === 2
andctype_upper
before sending the PR but the impact on event call was too small to be measurable. Now that I've written benchmark for this line only, I see that theord
approach is slightly faster. Or is there yet another way?ac853ac
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.
a5e3e7f