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

A few optimizations #7931

Merged
merged 3 commits into from
May 6, 2013
Merged

A few optimizations #7931

merged 3 commits into from
May 6, 2013

Conversation

Seldaek
Copy link
Member

@Seldaek Seldaek commented May 4, 2013

No description provided.

isset() vs in_array makes it take half the time (1ms/req here)
substr() does not have to scan the whole string so it's a wee bit faster
@raziel057
Copy link
Contributor

It´s nice to see that symfony contributors focus on improving performances to have a more powerfull 2.3 LTS release. I also thougth to change the ake by isset + ake in some class of the HttpFoundation or other pretty used components. What do you think about that? I would like to make benchmarks to view if it can be interesting.

@@ -136,20 +136,20 @@ public function matches(Request $request)
}

foreach ($this->attributes as $key => $pattern) {
if (!preg_match('#'.str_replace('#', '\\#', $pattern).'#', $request->attributes->get($key))) {
if (!preg_match('{'.$pattern.'}', $request->attributes->get($key))) {
Copy link
Member

Choose a reason for hiding this comment

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

I don't think using { } as delimitor is wise when you don't control the regex entered by the user as they have a special meaning in the regex. Can they really be used as delimitor when the regex is \d{3} ?

Copy link
Member Author

Choose a reason for hiding this comment

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

It is quite wise yes, especially because the character has a special meaning in regexes. That means it'll already be escaped so no need to escape it. The engine can deal with \d{3} just fine since it can count the opening/closing brackets, and skip those that are escaped. That's why I tend to always use {} as delimiters, because it doesn't add yet another char you have to escape.

Due to the BC $this->setRequest() call in the onKernelRequest method, the
request is set twice every time in Symfony, and RequestContext::fromRequest
takes 1ms to run here so if we can skip one call it is a win.
@fabpot
Copy link
Member

fabpot commented May 5, 2013

@raziel057 Don't waste your time, that's not worth it... for at least two reasons:

  • Whenever we are using array_key_exists, there is a reason for it;
  • The speed difference between isset and array_key_exists won't make any difference on a typical request.

Now, it you can demonstrate that changing some calls to isset do not break something and it makes a typical request significantly faster (more than 1%), then go for it. But trust me, that's a big challenge ;)

@Seldaek
Copy link
Member Author

Seldaek commented May 5, 2013

@fabpot I think he meant using if (isset() || array_key_exists()) { return $val } else { return $default }, which might be beneficial if most calls end up querying for a value that is present, but that's indeed a long shot.

@@ -72,9 +73,10 @@ public function __construct($matcher, RequestContext $context = null, LoggerInte
*/
public function setRequest(Request $request = null)
{
if (null !== $request) {
if (null !== $request && $this->request !== $request) {
Copy link
Member

Choose a reason for hiding this comment

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

What if some values of the request have changed?

Copy link
Member Author

Choose a reason for hiding this comment

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

Requests should really not be changed.. And as described in the commit, the problem is that it's always called twice for BC with frameworks that don't call setRequest

Copy link
Member

Choose a reason for hiding this comment

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

Yes they should not change. But unfortunately requests are not immutable. So you basically can't make this assumtion. As this its a BC break.

fabpot added a commit that referenced this pull request May 6, 2013
This PR was merged into the master branch.

Discussion
----------

A few optimizations

Commits
-------

ea633f5 [HttpKernel] Avoid updating the context if the request did not change
997d549 [HttpFoundation] Avoid a few unnecessary str_replace() calls
f5e7f24 [HttpFoundation] Optimize ServerBag::getHeaders()
@fabpot fabpot closed this May 6, 2013
@fabpot fabpot merged commit ea633f5 into symfony:master May 6, 2013
fabpot added a commit that referenced this pull request May 6, 2013
This PR was merged into the master branch.

Discussion
----------

[HttpFoundation] Reverted a part of #7931

refs: #7931

Commits
-------

09c2114 Reverted a part of f5e7f24
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

7 participants