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

Cleanup method signatures. #3775

Closed
wants to merge 3 commits into from
Closed

Cleanup method signatures. #3775

wants to merge 3 commits into from

Conversation

dereuromark
Copy link
Member

As per guidelines only public methods should be typehinted as it is not cost-free.

This mainly removes the typehinting of protected methods that are mainly used from within the class.
Protected methods in often extended classes like Exception are not touched.

@jippi
Copy link
Contributor

jippi commented Jun 22, 2014

How does that affect performance? I dislike this change a lot..

@markstory markstory added this to the 3.0.0 milestone Jun 22, 2014
@ADmad
Copy link
Member

ADmad commented Jun 22, 2014

Here's what SO has to say about it.

@@ -77,7 +77,7 @@ class DigestAuthenticate extends BasicAuthenticate {
* used on this request.
* @param array $config Array of config to use.
*/
public function __construct(ComponentRegistry $registry, $config) {
public function __construct(ComponentRegistry $registry, array $config = array()) {
Copy link
Member

Choose a reason for hiding this comment

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

Why not use new style arrays?

@ADmad
Copy link
Member

ADmad commented Jun 22, 2014

Tests failing.

@jippi
Copy link
Contributor

jippi commented Jun 23, 2014

I really don't like this... it's a nice defense against users doing wrong things.. and yes, users do mess around in the internals all the time, extending, replacing classes..

Considering we do this for Cake 2.x as well, and the bownty app still manages to service 95th percentile in less than 40ms I really really don't think it's a worthwhile "optimization" - we should optimize for reading the code and making it clear to understand than to the CPU in these cases.

If you really need this kind of micro optimization, you can probably afford one more web box, it's an extreme edge case performance wise, even for a site like bownty with 100k+ requests a day that are stupidly complex.

It's a lot more simple and fast to read the core code when you don't have to jump back and forth between the method signature and a huge doc block to deduce the basics like object types.

The SO article do not provide any kind of useful benchmarks to backup their claim either, it's pure guesswork that "yes, it's slower" - which could just be a few ms which imo makes keeping the signatures a no brainer

@AD7six
Copy link
Member

AD7six commented Jun 23, 2014

What Jippi said.

If it has a measurable performance cost (I find that unlikely, a SO post from 4 years ago doesn't really provide much motivation to consider it either) it would be more appropriate to (as a user) remove the type hints - which wouldn't be a particularly difficult sed call.

@dereuromark
Copy link
Member Author

Note that in the past some added typehints for protected methods (which was actually also my preferred way) and that was also declined or disliked.
So we should try to find a consistent way of handling this then IMO.

@dereuromark
Copy link
Member Author

And you will also have to remove the statement here: http://book.cakephp.org/3.0/en/contributing/cakephp-coding-conventions.html#typehinting

We only typehint public methods, though, as typehinting is not cost-free

@dereuromark
Copy link
Member Author

@ADmad

Argument 1 passed to iterator_to_array() must implement interface Traversable, null given

I was a little bit perplexed about how to fix that one @ https://travis-ci.org/cakephp/cakephp/jobs/28184081

@markstory
Copy link
Member

I can look into the performance impact of typehinting later this week if people are interested.

@ADmad
Copy link
Member

ADmad commented Jun 23, 2014

I didn't mean that SO link to be an authoritative guide just a reference :) The stats provided by @jippi make lot more sense.

@dereuromark I don't have any idea either.

@Marlinc
Copy link
Contributor

Marlinc commented Jun 23, 2014

To be honest, I don't like this change either because you will need to look at the docblock in order to see the type.

@dereuromark
Copy link
Member Author

@Marlinc In that case we should not promote the opposite (= no typehinting for protected methods). Instead we should promote defensive coding and also typehint protected methods consistently throughout the core.

@markstory
Copy link
Member

I don't understand the complaints about having to look at the doc blocks. The doc block is generally 1-4 lines higher than the method signature, is this a big issue?

@Marlinc
Copy link
Contributor

Marlinc commented Jun 24, 2014

Well it might not be such a big problem as I think it is because most IDE's read the doc blocks very well.

@bcrowe
Copy link
Contributor

bcrowe commented Jun 25, 2014

Having a type hint vs. having to read the docblock indeed isn't really a huge win. But, considering the argument of performance: Realistically, if your application is getting enough traffic that this would ever become an issue... one should look into migrating from shared-hosting/a single node, etc. This kind of micro-optimization seems to only cater to an application that's pushing its shared-hosting / VPS's limits, but for some reason cannot afford to scale to additional nodes or a more powerful shared-hosting plan, etc? Personally, I'd much rather see defensive code. I think @dereuromark's last comment on promoting the opposite would be more suitable.

@jippi
Copy link
Contributor

jippi commented Jun 25, 2014

👍 for defensive code rather than micro optimizations like these :) I'm all for is_null vs === NULL since they don't really change anything in terms of readability or code execution.

This change does both, and I'm not in favor of either :)

euromark added 2 commits June 26, 2014 01:25
…atures

Conflicts:
	src/Controller/Component/AuthComponent.php
@dereuromark
Copy link
Member Author

With the changes in cakephp/docs#1468 we can probably close this as invalid change. We now encourage typehinting in general then for a more defensive approach.

We still didn't investigate the performance impact yet, though.
This would be interesting if we add typehints to all protected methods now, as well.

@markstory
Copy link
Member

I'm planning on looking at typehint impacts one of these days 😄

@lorenzo
Copy link
Member

lorenzo commented Jul 8, 2014

@markstory
Copy link
Member

67% is a non-trivial bump. I think sticking with only putting typehints on public methods and key protected methods makes sense. Adding typehints to every method seems like overkill to me.

@dereuromark
Copy link
Member Author

So which of the above changes would you find worth reverting to the way it was before then?
Most of those seem to be internal ones (non key so to speak).

@jippi
Copy link
Contributor

jippi commented Jul 9, 2014

@lorenzo that test is from 2011, can you re-run it on a modern php version with opcache ? :)

update

-> time php bench.php
1.541451215744  1.228688955307

real    0m2.884s
user    0m2.855s
sys 0m0.019s

@lorenzo
Copy link
Member

lorenzo commented Jul 9, 2014

I did, got similar results. 30% worse in 5.5

@lorenzo
Copy link
Member

lorenzo commented Jul 9, 2014

Not sure if that would be different in 5.4 or with a different processor...

@markstory
Copy link
Member

30% worse is significant enough to warrant not using typehints on every protected method.

@milesj
Copy link

milesj commented Jul 9, 2014

That benchmark is doing a million loops. Find me an app that ever does a million loops and I'll show you a shitty implementation. This change is a terrible one and the optimization improvements would be negligible in the real world.

@markstory
Copy link
Member

I agree that no single request should be calling a million methods. But it is not unreasonable for an application ofdecent complexity to call a few thousand methods, which will add up. I admit this could become a slippery slope where all sorts of silly micro-optimizations can be justified. Personally, I don't see a huge amount of value in typehints as they don't halt execution, and php is not statically typed no matter how many hints you use.

@milesj
Copy link

milesj commented Jul 10, 2014

Type hints throw fatal errors, which do halt execution. Not sure what you're trying to say. You're sacrificing type integrity/safety for a few micro-optimizations.

@markstory
Copy link
Member

My bad, you're right. I thought they just emitted warnings for some silly reason. I'm not against type hints on public methods, or even commonly extended protected methods. I don't think there is much value in putting type-hints on every method.

@dereuromark
Copy link
Member Author

Let's just close this then. Before someone goes all kamikaze on us ;) It ain't worth it.

@markstory
Copy link
Member

Ok.

@markstory markstory closed this Jul 10, 2014
@ravage84
Copy link
Member

Even though it has already been decided:

For me, a framework should tend to go a more stable, may be even defensive, way.
Even on non-public methods as they could be used by extension developers in some way.

If someone really would need the extra speed and would be willing to sacrifice the benefit of type hinting, he could craft a patch or a script, which removes the type hints from the core.

Or if we really identify one or two non-public methods, which are used really very often and thus make a great impact, we still could decide to cherry pick these to be removed.

At last: Removing security for speed within a framework is easier for a end user developer than the opposite.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

None yet

10 participants