Skip to content

Make \Request::get more performant. #12369

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

Closed
wants to merge 2 commits into from

Conversation

KorvinSzanto
Copy link
Contributor

Previously request get ran ALL searches regardless of whether a result was found. Now it only runs the ones required to find the value.

@@ -723,7 +723,17 @@ public static function getHttpMethodParameterOverride()
*/
public function get($key, $default = null, $deep = false)
{
return $this->query->get($key, $this->attributes->get($key, $this->request->get($key, $default, $deep), $deep), $deep);
$result = $this->query($key, $this, $deep);
if ($result === $this) {
Copy link
Member

Choose a reason for hiding this comment

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

I would rather use the opposite conditions and using an early return rather than assigning $result over and over again inside conditions on it (which can be confusing when seeing a list of if ($result === $this) { conditions)

Copy link
Contributor Author

Choose a reason for hiding this comment

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

We'd still need to store them to a variable, the only difference is the return would be within each conditional. I'd rather have a single return no?

@javiereguiluz
Copy link
Member

@KorvinSzanto thanks for your pull request. We're always looking for ways to improve performance!

Before discussing about the actual code changes, I'd like to ask you if you have some stats about the performance improvement. That would help maintainers to make a better decision. You can use any of the available performance analysis tools, such as Xdebug, XHProf, Blackfire, QafooLabsProfiler, etc.

@stof
Copy link
Member

stof commented Oct 31, 2014

@javiereguiluz currently, the fallback to each bag is done by querying the second bag and using the value as default for the first bag. This is totally inefficient algorithm, given that it means you look in all bags all the time instead of stopping the algorithm once you have a value (the code we have would be efficient only if the ->get() call of a bag was returning the value lazily, but this is of course not the case in PHP)

@javiereguiluz
Copy link
Member

@stof I know that it looks like a performance improvement, but the first rule of application profiling is that you should not trust your instinct but the metrics and profiles. Given that there are a lot of tools available out there, it should be easy to calculate the performance improvement.

if ($result === $this) {
return $default;
}
return $result;
Copy link
Member

Choose a reason for hiding this comment

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

I would have written it this way:

if ($this !== $result = $this->query->get($key, $this, $deep)) {
    return $result;
}

if ($this !== $result = $this->attributes->get($key, $this, $deep)) {
    return $result;
}

if ($this !== $result = $this->request->get($key, $this, $deep)) {
    return $result;
}

return $default;

@Taluu
Copy link
Contributor

Taluu commented Nov 1, 2014

I'm not sure that using $this as a defqult value - and a basis for comparision - is really performant. How about a random key, or even a randon stdClass object ?

@Tobion
Copy link
Contributor

Tobion commented Nov 1, 2014

@Taluu Why should an extra storage object/key be more performant than something that already exists?

@wouterj
Copy link
Member

wouterj commented Nov 1, 2014

Fyi, the new way is indeed faster (while the old way is a little bit quicker when request matches): http://jsbin.com/baguvo/1/quiet (times are the average of 10 tests with 1000 calls)

@javiereguiluz
Copy link
Member

@KorvinSzanto it would be great if you have some time to finish this PR. Here are some of the things that may be necessary:

@KorvinSzanto
Copy link
Contributor Author

@javiereguiluz, I'd rather close this and let fabpot submit as he'd like it.

fabpot added a commit that referenced this pull request Nov 21, 2014
…rvinSzanto, xabbuh)

This PR was submitted for the 2.7 branch but it was merged into the 2.3 branch instead (closes #12537).

Discussion
----------

[HttpFoundation] Make Request::get() more performant

| Q             | A
| ------------- | ---
| Bug fix?      | no
| New feature?  | no
| BC breaks?    | no
| Deprecations? | no
| Tests pass?   | yes
| Fixed tickets |
| License       | MIT
| Doc PR        |

This finishes the work started by @KorvinSzanto in #12369.

Commits
-------

3039935 reformat code as suggested by @fabpot
ad64223 Fix typo
4162713 Make `\Request::get` more performant.
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.

7 participants