Skip to content

Performance enhancement for HTML Class. #1180

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

Merged
merged 5 commits into from
Dec 3, 2012
Merged

Conversation

thybag
Copy link
Contributor

@thybag thybag commented Sep 4, 2012

Hello,

We've recently started using laravel for a number of internal web-apps, but have run in to a couple of issues when load testing. The underlying issue being that the config::get method is significantly more expensive than a standard array access. This is compounded by it being invoked a 100-1000 times per page load.

This pull request includes a change we've made to attempt to mitigate this issue, which functions by caching the application.encoding variable within the HTML class. To use one of our apps as an example, for a large form this reduced the number of calls to config::get from 1200+ invocations (72ms) to 125 (9ms).

All core unit-test's pass with this change in place.

Thank you,
Carl

Note: I originally targeted the wrong branch with my pull request ( #1177 ) so have closed it and opened this one instead. Sorry for the inconvenience.

…ls to Config::get();

Calling the "Config::get('application.encoding')" is expensive and within a large form (using the form builder) having it requested multiple times can result in a significant performance drag.

Caching this value reduced calls to Config:get within our project from 1200+ to 125. All core tests appear to pass with this change in place.
@travisbot
Copy link

This pull request passes (merged 6e44b40 into ec13efb).

@daylerees
Copy link
Contributor

Nice pull, will let Taylor have a peek! Thanks for providing plenty of info :)

@conatus
Copy link

conatus commented Sep 4, 2012

PS This was the performance issue I alluded to in the Laravel IRC chat last week and promised to e-mail Taylor. But rather a patch than an e-mail!

@franzliedke
Copy link
Contributor

Good idea!

How about a static method in this class that returns the charset, from the static variable if it is set, else from the config. Keeps the repetition out of there and is prettier...

…ass (in order to remove duplication of logic). All tests continue to pass.
@thybag
Copy link
Contributor Author

thybag commented Sep 5, 2012

Hello,

I've just committed an additional change to conform with franzliedke's suggestion. The new logic has now been localised in to a get_encoding method to avoid unnecessary duplication.

Hopefully this is okay,
Thanks,
Carl

*
* @return string
*/
public static function get_encoding(){
Copy link
Member

Choose a reason for hiding this comment

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

Bracket on it's own line and should be able to use protected here.

@travisbot
Copy link

This pull request passes (merged a9be66d into ec13efb).

@thybag
Copy link
Contributor Author

thybag commented Sep 5, 2012

In response to crynobone, I've now resolved the coding style issues with my original implementation. (Bracket is now on its own line and method scope is protected).

Thanks,
Carl

@travisbot
Copy link

This pull request passes (merged f55bb85 into ec13efb).

@JoostK
Copy link
Contributor

JoostK commented Sep 5, 2012

Nice one!

I'd suggest you change get_encoding to encoding as that's Laravel style, and you could change the implementation as follows:

return static::$encoding ?: static::$encoding = Config::get('application.encoding');

@kbanman
Copy link
Contributor

kbanman commented Sep 8, 2012

Calls to Config::get() aren't all that expensive; the Config class is already caching the config.

@thybag
Copy link
Contributor Author

thybag commented Sep 9, 2012

Hello,

I've renamed get_encoding() method to just encoding() as per JoosK's suggestion, although I left the syntax as it was as it seems more readable that way. I'm more than happy to change it if an alternate syntax is preferred though.

Thanks,
Carl

P.S. @kbanman In a small application I agree that the speed hit is negligible, but when scaling those small speed hits add up in a very noticeable way.

Just as an example, benchmarking the Config::get("application.encoding") against a standard array access for the same value, for one million calls, will result in the config::get method costing 4.515 seconds compared to 0.139 seconds for the direct array access. (roughly 32 times slower). Its also fair to note that the slowdown will be more significant the deeper the config item is nested, ie. getting the value for foo.bar.test will be slower than foo.bar.

Although its true no reasonable application is ever likely to be making anywhere near a million calls to the config::get method, if you scale the application up to a few 100 concurrent users and consider that the method can be hit upwards of a 1000 times a page load, as far as the server is concerned it may as well be. Hence while for many applications saving some calls to the get::config method likely wont make any noticeable difference to the page speed, as soon as you start scaling in any significant way, those tiny savings will add up very quickly.

@jasonlewis
Copy link
Contributor

My opinion is that what @JoostK has posted is a cleaner piece of code then what you still have.

Changed the return of the method in line with suggestions from @JoostK.
@conatus
Copy link

conatus commented Sep 10, 2012

Just made the change suggested. Thanks.

taylorotwell added a commit that referenced this pull request Sep 26, 2012
Performance enhancement for Str Class (see pull request #1180)
@conatus
Copy link

conatus commented Oct 3, 2012

@taylorotwell Hi - any chance of getting this merged alongside the identical changes to Str in #1180? Thanks.

@franzliedke
Copy link
Contributor

Looks like #1204 (the one with the improvements in the Str class) already got merged. It would only make sense for this one, then, too ;)

@conatus
Copy link

conatus commented Oct 4, 2012

Opps. That's what I mean!

@thybag
Copy link
Contributor Author

thybag commented Oct 17, 2012

Hello,

Any idea if this change will be able to make its way in to the main codebase? or are there further changes/tweaks needed before this is able to happen.

Just noticing that a very similar fix to this was included a few weeks back.

Thank you,
Carl

@TommyC81
Copy link
Contributor

+1

taylorotwell added a commit that referenced this pull request Dec 3, 2012
Performance enhancement for HTML Class.
@taylorotwell taylorotwell merged commit 8ff052c into laravel:develop Dec 3, 2012
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.