Skip to content

[New Rule] Slow array SHOULD NOT be used in loop #20

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
lenaorobei opened this issue Jan 29, 2019 · 12 comments
Closed

[New Rule] Slow array SHOULD NOT be used in loop #20

lenaorobei opened this issue Jan 29, 2019 · 12 comments
Assignees
Labels
accepted New rule is accepted new rule New feature implementation proposal New rule proposal

Comments

@lenaorobei
Copy link
Contributor

lenaorobei commented Jan 29, 2019

Rule

The use of slow array functions (array_merge) in loop is discouraged.

Reason

Merging arrays in a loop is slow and causes high CPU usage. Some benchmarking.

Bad example:

    $options = [];
    foreach ($configurationSources as $source) {
        // code here
        $options = array_merge($options, $source->getOptions());
    }

Good example:

    $options = [];
    foreach ($configurationSources as $source) {
        // code here
        $options[] = $source->getOptions();
    }

    // PHP 5.6+
    $options = array_merge([], ...$options);

Implementation

  • Subscribe to T_STRING token and check if it's content is array_merge.
  • Try to find outer loop.
  • If loop is found rase a warning (need to discuss additional logic).
<severity>7</severity>
<type>warning</type>
@lenaorobei lenaorobei added proposal New rule proposal new rule New feature implementation labels Jan 29, 2019
@paliarush
Copy link
Contributor

What is going to happen with core code for which error is generated instead of warning by default?

@lenaorobei
Copy link
Contributor Author

We can throw a warning with incremental approach.

@lenaorobei lenaorobei added the need to discuss Rule requires discussion label Jan 29, 2019
@Vinai
Copy link

Vinai commented Jan 30, 2019

Mixed feelings. I often use merge in a reduce function to avoid changing local state (and save a line of code).
Hope PHP optimizes for more functional patterns in future, now that FP is becoming mainstream.
Even Java now has optimized map/reduce/filter chains, and more and more languages support STM.
If PHP follows, this sniff would be outdated.
Maybe sniffs should focus on algorithmic complexity instead of Runtime implementation details.

@shochdoerfer
Copy link
Member

Slow is a matter of definition :) In a loop with just a few iterations, you most likely won't notice that much of a difference.

@Vinai
Copy link

Vinai commented Jan 30, 2019

Exactly. That’s why focusing on big O algorithmic complexity seems like a good idea since that’s assumes scalability in the worst case scenario.
Stating this I feel stupid because I’m sure you are well aware.

@lenaorobei lenaorobei changed the title [New Rule] Slow array function used in loop [New Rule] Slow array SHOULD NOT be used in loop Jan 31, 2019
@himanshu8dev
Copy link

For my point of view, This is good practice for developer who don't care about script execution it will also effect on performance and in the end it will slow down the site performance, This should be implemented..

@lenaorobei lenaorobei added the accepted New rule is accepted label Feb 13, 2019
@lenaorobei lenaorobei removed the need to discuss Rule requires discussion label Feb 19, 2019
@larsroettig larsroettig self-assigned this Mar 20, 2019
@lenaorobei
Copy link
Contributor Author

@buskamuza @kandy

Need you input here.

See related PR #72

@kandy
Copy link

kandy commented Mar 25, 2019

For me, it makes sense to have this rule

@larsroettig
Copy link
Member

Hi @kandy,
sense yes but if you look to findings maybe there are some false positives

@kandy
Copy link

kandy commented Mar 25, 2019

@larsroettig, Definitely, there are some false positives and the warning is a good signal to review such places.

larsroettig pushed a commit that referenced this issue Mar 26, 2019
larsroettig pushed a commit that referenced this issue Mar 29, 2019
lenaorobei added a commit that referenced this issue Apr 8, 2019
#20  Impelement ArrayMerge sniff in foreach
@lenaorobei
Copy link
Contributor Author

Implemented in #72

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
accepted New rule is accepted new rule New feature implementation proposal New rule proposal
Projects
None yet
Development

No branches or pull requests

7 participants