Movatterモバイル変換


[0]ホーム

URL:


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

Sign up
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 ourterms of service andprivacy statement. We’ll occasionally send you account related emails.

Already on GitHub?Sign in to your account

Closed
dunglas wants to merge1 commit intosymfony:3.4fromdunglas:env-processor-switch

Conversation

@dunglas
Copy link
Member

QA
Branch?3.4
Bug fix?no
New feature?no
BC breaks?no
Deprecations?no
Tests pass?yes
Fixed ticketsn/a
LicenseMIT
Doc PRn/a

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

sstok reacted with thumbs up emoji
if ('string' ===$prefix) {
return (string)$env;
}
switch ($prefix) {

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-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
MemberAuthor

dunglas commentedDec 29, 2017
edited
Loading

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
MemberAuthor

dunglas commentedDec 29, 2017
edited
Loading

Well, here is a benchmark...andswitch is actually slower thanif: (see below, switch is faster).

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

GromNaN reacted with confused emoji

@dunglas
Copy link
MemberAuthor

dunglas commentedDec 29, 2017
edited
Loading

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.

sstok and Taluu reacted with thumbs up emoji

@dunglasdunglas reopened thisDec 29, 2017
@nicolas-grekasnicolas-grekas added this to the4.1 milestoneDec 30, 2017
@fabpot
Copy link
Member

I don't think it makes sense to make such a change. We are not promoting anything. Nobody will notice that. Let's close.

Taluu reacted with confused emoji

@fabpotfabpot closed thisJan 3, 2018
Sign up for freeto join this conversation on GitHub. Already have an account?Sign in to comment

Reviewers

@javiereguiluzjaviereguiluzjaviereguiluz left review comments

Assignees

No one assigned

Projects

None yet

Milestone

4.1

Development

Successfully merging this pull request may close these issues.

5 participants

@dunglas@nicolas-grekas@fabpot@javiereguiluz@carsonbot

[8]ページ先頭

©2009-2025 Movatter.jp