Skip to content

Improve performance of InterfaceValidator #27903

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

lbajsarowicz
Copy link
Contributor

@lbajsarowicz lbajsarowicz commented Apr 20, 2020

Description (*)

According to the fact that we have:

  • 1455 after plugins
  • 819 before plugins
  • 246 around plugins

in Magento, I changed the order of conditions in Magento Interface Validator that is executed every single time when plugins are mentioned, to improve the logic performance.

Only by changing the order of conditions, we get in average 15% of performance improvement:
image

Benchmarking method (pseudocode)

for ($i = 0; $i < 1455000; $i++) {
    getMethodType('afterNothingMatters');
}

for ($i = 0; $i < 819000; $i++) {
    getMethodType('beforeNothingMatters');
}

for ($i = 0; $i < 246000; $i++) {
    getMethodType('aroundNothingMatters');
}

for ($i = 0; $i < 1000000; $i++) {
    getMethodType('actuallyNothingMatters');
}

Additionally, replaced Elvis (?:) operator with null coalescing operator (??) which does not juggle types during comparison.

On line 73 you can see that I moved === null comparison before the other comparison because it's cheaper to compare to null than execute additional class method.

Replaced substr comparison with strpos which during my benchmark improved the performance slightly:
image

After all my changes, the Class performs about 21% faster
image

How do you...?

The tool used for performance comparisons is blackfire.io

Related Pull Requests

Fixed Issues (if relevant)

N/A

Manual testing scenarios (*)

N/A

Questions or comments

Support

Solving this issue is supported by Mediotype

Contribution checklist (*)

  • Pull request has a meaningful description of its purpose
  • All commits are accompanied by meaningful commit messages
  • All new or changed code is covered with unit/integration tests (if applicable)
  • All automated tests passed successfully (all builds are green)

@m2-assistant
Copy link

m2-assistant bot commented Apr 20, 2020

Hi @lbajsarowicz. Thank you for your contribution
Here is some useful tips how you can test your changes using Magento test environment.
Add the comment under your pull request to deploy test or vanilla Magento instance:

  • @magento give me test instance - deploy test instance based on PR changes
  • @magento give me 2.4-develop instance - deploy vanilla Magento instance

For more details, please, review the Magento Contributor Guide documentation.

@Dnd-Mafer
Copy link
Contributor

@lbajsarowicz what if you use if confition with continue instead of switch when comparing $methodType ?

@lbajsarowicz
Copy link
Contributor Author

@lbajsarowicz what if you use if confition with continue instead of switch when comparing $methodType ?

Replaced in both occurrences. Still keeping the number of occurrences in mind.

@ghost ghost assigned dmytro-ch Apr 22, 2020
@ghost ghost assigned lbajsarowicz Apr 22, 2020
Copy link
Contributor

@dmytro-ch dmytro-ch left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The refactored logic is already covered by corresponding test \Magento\Framework\Interception\Test\Unit\Code\InterfaceValidatorTest.

@dmytro-ch dmytro-ch added the Auto-Tests: Covered All changes in Pull Request is covered by auto-tests label Apr 24, 2020
@magento-engcom-team
Copy link
Contributor

Hi @dmytro-ch, thank you for the review.
ENGCOM-7482 has been created to process this Pull Request

@engcom-Delta
Copy link
Contributor

Notice: QA not applicable

@engcom-Echo engcom-Echo self-assigned this Apr 27, 2020
@slavvka slavvka added this to the 2.4.1 milestone Apr 28, 2020
@slavvka slavvka added Priority: P3 May be fixed according to the position in the backlog. Severity: S2 Major restrictions or short-term circumventions are required until a fix is available. labels Apr 28, 2020
@ghost ghost assigned orlangur and lbajsarowicz May 14, 2020
@VladimirZaets VladimirZaets added the QA: Ready to add to Regression Scope Should be analyzed and added to Regression Testing Scope(if applicable) label May 20, 2020
@sdzhepa sdzhepa removed the QA: Ready to add to Regression Scope Should be analyzed and added to Regression Testing Scope(if applicable) label Jun 4, 2020
@magento-engcom-team magento-engcom-team merged commit b6e71ff into magento:2.4-develop Jun 5, 2020
@m2-assistant
Copy link

m2-assistant bot commented Jun 5, 2020

Hi @lbajsarowicz, thank you for your contribution!
Please, complete Contribution Survey, it will take less than a minute.
Your feedback will help us to improve contribution process.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Auto-Tests: Covered All changes in Pull Request is covered by auto-tests Award: category of expertise Award: special achievement Component: Interception Partner: Mediotype partners-contribution Pull Request is created by Magento Partner Priority: P3 May be fixed according to the position in the backlog. Progress: accept Release Line: 2.4 Severity: S2 Major restrictions or short-term circumventions are required until a fix is available.
Projects
Archived in project
Development

Successfully merging this pull request may close these issues.