Uh oh!
There was an error while loading.Please reload this page.
- Notifications
You must be signed in to change notification settings - Fork9.7k
[DI] Use a switch to select the appropriate env processor (optim)#25628
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 ourterms of service andprivacy statement. We’ll occasionally send you account related emails.
Already on GitHub?Sign in to your account
Uh oh!
There was an error while loading.Please reload this page.
Conversation
| if ('string' ===$prefix) { | ||
| return (string)$env; | ||
| } | ||
| switch ($prefix) { |
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 recommend to add a help comment here to avoid some people in the future refactoring it into someif to "improve code quality". Something like this:
// used a 'switch' instead of 'if' conditions to enable a PHP micro-optimizationnicolas-grekas commentedDec 29, 2017
👎 also sorry, as this is not a perf critical code path, and the CS change doesn't improve readability. |
dunglas commentedDec 29, 2017 • edited
Loading Uh oh!
There was an error while loading.Please reload this page.
edited
Uh oh!
There was an error while loading.Please reload this page.
Regarding the readability, it will if we continue to add builtin processors#25627 (imo it already improves it). It's also the appropriate structure for this type of case. |
dunglas commentedDec 29, 2017 • edited
Loading Uh oh!
There was an error while loading.Please reload this page.
edited
Uh oh!
There was an error while loading.Please reload this page.
Well, here is a benchmark... <?phprequire'vendor/autoload.php';$container =new \Symfony\Component\DependencyInjection\ContainerBuilder();class Test {private$arg;publicfunction__construct(string$arg) {$this->arg =$arg; }publicfunctiongetArg():string {return$this->arg; }}$container ->register(Test::class, Test::class) ->addArgument('%env(string:FLAG)%') ->setShared(false);$start =microtime(true);for ($i =0;$i <1000000;$i++) {$container->get(Test::class)->getArg();}$end =microtime(true);echosprintf("Elapsed: %f\n",$end -$start); Average with switch: 8.350835 |
dunglas commentedDec 29, 2017 • edited
Loading Uh oh!
There was an error while loading.Please reload this page.
edited
Uh oh!
There was an error while loading.Please reload this page.
My patch targets the 3.4 🍎 branch. And I was comparing with master 🍐. Comparing 3.4 and my patch, the results are of course more coherent: if (avg): 8.954663 The good thing is:master is way faster than 3.4! Regarding those new numbers, I think we should merge this patch: it's indeed a micro-optim, but promoting new PHP features, performance best practices and the appropriate construct (even if we can debate that) is a thing Symfony should do. |
fabpot commentedJan 3, 2018
I don't think it makes sense to make such a change. We are not promoting anything. Nobody will notice that. Let's close. |
Using a
switchwhen the value is a string allows PHP 7.2to convert the structure into a jump table.