Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Appearance settings

[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

Closed
wants to merge 1 commit into from

Conversation

dunglas
Copy link
Member

@dunglas dunglas commented Dec 29, 2017

Q A
Branch? 3.4
Bug fix? no
New feature? no
BC breaks? no
Deprecations? no
Tests pass? yes
Fixed tickets n/a
License MIT
Doc PR n/a

Using a switch when the value is a string allows PHP 7.2 to convert the structure into a jump table.

if ('string' === $prefix) {
return (string) $env;
}
switch ($prefix) {
Copy link
Member

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

@nicolas-grekas
Copy link
Member

👎 also sorry, as this is not a perf critical code path, and the CS change doesn't improve readability.

@dunglas
Copy link
Member Author

dunglas commented Dec 29, 2017

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
Copy link
Member Author

dunglas commented Dec 29, 2017

Well, here is a benchmark... and switch is actually slower than if: (see below, switch is faster).

<?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
Average with if: 6.927684

@dunglas dunglas closed this Dec 29, 2017
@dunglas
Copy link
Member Author

dunglas commented Dec 29, 2017

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
switch (avg): 8.880407

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.

@dunglas dunglas reopened this Dec 29, 2017
@nicolas-grekas nicolas-grekas added this to the 4.1 milestone Dec 30, 2017
@fabpot
Copy link
Member

fabpot commented Jan 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.

@fabpot fabpot closed this Jan 3, 2018
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

Successfully merging this pull request may close these issues.

5 participants
Morty Proxy This is a proxified and sanitized view of the page, visit original site.