-
-
Notifications
You must be signed in to change notification settings - Fork 9.6k
[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 our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
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 some if
to "improve code quality". Something like this:
// used a 'switch' instead of 'if' conditions to enable a PHP micro-optimization
👎 also sorry, as this is not a perf critical code path, and the CS change doesn't improve readability. |
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. |
Well, here is a benchmark... <?php
require 'vendor/autoload.php';
$container = new \Symfony\Component\DependencyInjection\ContainerBuilder();
class Test {
private $arg;
public function __construct(string $arg)
{
$this->arg = $arg;
}
public function getArg(): 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);
echo sprintf("Elapsed: %f\n", $end - $start); Average with switch: 8.350835 |
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. |
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
switch
when the value is a string allows PHP 7.2 to convert the structure into a jump table.