Skip to content
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

Performance improvements #38642

Open
wants to merge 3 commits into
base: 2.4-develop
Choose a base branch
from

Conversation

qsolutions-pl
Copy link
Contributor

@qsolutions-pl qsolutions-pl commented Apr 19, 2024

Description (*)

This PR reduces config load time by 50%,
image
but it adds +100MB to memory as I've added the file cache

What was completed in this PR:

  • reduced recursive function in the Deployment Config
  • reduced the configuration files being read twice by the framework

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)
  • README.md files for modified modules are updated and included in the pull request if any README.md predefined sections require an update
  • All automated tests passed successfully (all builds are green)

Resolved issues:

  1. resolves [Issue] Performance improvements #38651: Performance improvements

Copy link

m2-assistant bot commented Apr 19, 2024

Hi @qsolutions-pl. Thank you for your contribution!
Here are some useful tips on 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

❗ Automated tests can be triggered manually with an appropriate comment:

  • @magento run all tests - run or re-run all required tests against the PR changes
  • @magento run <test-build(s)> - run or re-run specific test build(s)
    For example: @magento run Unit Tests

<test-build(s)> is a comma-separated list of build names.

Allowed build names are:
  1. Database Compare
  2. Functional Tests CE
  3. Functional Tests EE
  4. Functional Tests B2B
  5. Integration Tests
  6. Magento Health Index
  7. Sample Data Tests CE
  8. Sample Data Tests EE
  9. Sample Data Tests B2B
  10. Static Tests
  11. Unit Tests
  12. WebAPI Tests
  13. Semantic Version Checker

You can find more information about the builds here
ℹ️ Run only required test builds during development. Run all test builds before sending your pull request for review.


For more details, review the Code Contributions documentation.
Join Magento Community Engineering Slack and ask your questions in #github channel.

$newPath = $path . '/' . $key;
} else {
$newPath = $key;
}
Copy link
Member

Choose a reason for hiding this comment

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

It can be improved with
$newPath = $path ? $path . '/' . $key : $key;

* @throws \Magento\Framework\Exception\RuntimeException
*/
public function getFileCache(array $cache, string $filePath, \Magento\Framework\Filesystem\DriverInterface $fileDriver): array
{
Copy link
Member

@TuVanDev TuVanDev Apr 19, 2024

Choose a reason for hiding this comment

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

It appears that the getFileCache method is only used within the Reader class, it should be declared as a private scope.

̶I̶t̶ ̶a̶p̶p̶e̶a̶r̶s̶ ̶t̶h̶a̶t̶ ̶t̶h̶e̶ ̶̶g̶e̶t̶F̶i̶l̶e̶C̶a̶c̶h̶e̶̶ ̶d̶o̶e̶s̶ ̶n̶o̶t̶ ̶t̶h̶r̶o̶w̶ ̶\̶M̶a̶g̶e̶n̶t̶o̶\̶F̶r̶a̶m̶e̶w̶o̶r̶k̶\̶E̶x̶c̶e̶p̶t̶i̶o̶n̶\̶F̶i̶l̶e̶S̶y̶s̶t̶e̶m̶E̶x̶c̶e̶p̶t̶i̶o̶n̶.̶ ̶C̶a̶n̶ ̶y̶o̶u̶ ̶c̶o̶n̶f̶i̶r̶m̶ ̶t̶h̶i̶s̶?̶ Please ignore this since the $fileDriver->isExists() function call may throw this exception.

}
}
return $cache;
}
}
Copy link
Member

Choose a reason for hiding this comment

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

It would be beneficial if all namespaces were imported:
\Magento\Framework\Filesystem\DriverInterface
\Magento\Framework\Exception\FileSystemException
\Magento\Framework\Exception\RuntimeException

Copy link
Contributor

Choose a reason for hiding this comment

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

What would be the benefit, though?

Copy link
Member

Choose a reason for hiding this comment

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

@bgorski It is not related to performance, but rather improving readability and consistency in the code. As you can see, the codebase was recently refactored to import namespaces. Specifically, all namespaces in this class were imported.

Copy link
Member

Choose a reason for hiding this comment

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

Additionally, it also avoids the line length rule since the following line exceeds 120 characters:

    public function getFileCache(array $cache, string $filePath, \Magento\Framework\Filesystem\DriverInterface $fileDriver): array

@TuVanDev TuVanDev removed their assignment Apr 19, 2024
@m2-community-project m2-community-project bot moved this from Review in Progress to Pending Review in Pull Requests Dashboard Apr 19, 2024
@qsolutions-pl
Copy link
Contributor Author

@magento run all tests

if (null === $flattenResult) {
$flattenResult = [];
}
$paramsKey = CRC32(json_encode($params ?? ''));
Copy link
Contributor

@kandy kandy Apr 19, 2024

Choose a reason for hiding this comment

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

Suggested change
$paramsKey = CRC32(json_encode($params ?? ''));
$paramsKey = empty($params) ? '2d06800538d394c2' : hash('xxh3', json_encode($params));

it shold be even better from perfromance point of view. see https://php.watch/versions/8.1/xxHash

Copy link
Contributor Author

Choose a reason for hiding this comment

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

image
thanks to this, we get 10% improvement

Great tip, thanks!

Copy link
Member

Choose a reason for hiding this comment

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

Because $params is expected to be an array, we could avoid to use the empty function and compare to [] or use !$params instead (it's fine tunning, it won't bring incredible improvement)

@qsolutions-pl
Copy link
Contributor Author

@magento run all tests

@ihor-sviziev
Copy link
Contributor

Could you please provide some absolute numbers? Is that like 50% of 10ms, or 50% of 200ms?

@Jakhotiya
Copy link
Contributor

@qsolutions-pl Is it true that it will add 100+MB's of memory? That's a lot of memory for config cache? How did you measure it?
My concern is, typically the frontend requests take anywhere between 40MB to 120MB . For frontend servers, we internally limit memory_limit to 128MB. It allows php-fpm to handle 35 requests on a 8GB server.
Hence I want to understand whether this PR really adds 100MB?

Also, since we are caching deployment config, does changing env.php require a config cache flush. Right now changes made in env.php work immidietly.

"reduced the configuration files being read twice by the framework"
env.php and config.php are cached php opcache. Reading data in more than once does not have significant impact on performance? Was this tested with opcache ON and 500+MB's allowed for opcache. Also max_accelerated_files should be at least 10000.

Thank you in advance for contributing this.

@qsolutions-pl
Copy link
Contributor Author

qsolutions-pl commented Apr 20, 2024

@Jakhotiya
There are actually 2 files that are changed

  • DeploymentConfig.php (the biggest improvement)
  • Reader.php class (5-10ms improvement)
    I have to check but it seems additional memory is consumed by the second class as I store the loaded configuration files in an array so the framework does not have to process them twice or more (as it tends to).
    I have some ideas for improvement for the Reader.php class but I want to first prepare and finish the code related to flatten configuration so we can skip the "flattenParams" all together.
    However, I would like the community to jump on this PR as it seems that we can improve the entire performance with just few lines of code.
    I will spend some more time on this and see where it gets me. I might revert the changes in Reader.php class.
    I've also explained how to truly optimize the configuration load here:
    https://www.linkedin.com/pulse/need-speed-improvement-magento-adobe-commerce-mageos-things-winkler-cub2f/?trackingId=MpxYYMSHSyOvkztHNOn88A%3D%3D

@qsolutions-pl
Copy link
Contributor Author

@ihor-sviziev do you know why tests are failing?

@bgorski
Copy link
Contributor

bgorski commented Apr 20, 2024

@magento run Database Compare, Functional Tests B2B, Functional Tests CE, Functional Tests EE, Integration Tests, Static Tests, WebAPI Tests

@qsolutions-pl
Copy link
Contributor Author

I've implemented this on 1 production site, no issues, and here are the results:
image

image

@dudzio12
Copy link
Member

@qsolutions-pl Could you explain those 100M? I'm wondering why it's that much I would expect less memory usage. I read your post on linkedin and you are talking about env.php and config.php files, but those files are clearly not that big.
I'm trying to calculate how memory usage will change after implementation in my projects. 👍

@igorwulff
Copy link
Contributor

Hi @qsolutions-pl I did some tests. For a single store with a lot of configurations I see Magento being busy for 8ms or so when having the cache warmed and fpc and block cache disabled (which happens the most times), on a fast laptop CPU, so this could be a bit more gain on a server cpu. I added hrtime timers on the specific code pieces to measure how much time they were really costing. Is the gain mainly coming with config cache gone or disabled?

@Den4ik Den4ik self-assigned this Apr 22, 2024
@Den4ik Den4ik self-requested a review April 22, 2024 11:22
@Den4ik
Copy link
Contributor

Den4ik commented Apr 22, 2024

@magento run Database Compare

@ihor-sviziev
Copy link
Contributor

ihor-sviziev commented Apr 22, 2024

@qsolutions-pl looks like with your changes, magento requires DB connection while running any CLI command and because of that, magento can't be installed.
Please try it locally.

@furan917
Copy link
Contributor

furan917 commented Apr 22, 2024

@ihor-sviziev What makes you say its @qsolutions-pl changes that causes that? Does the code changes you see suggest a change to a reliance on the DB? (Thats not me being cherky, genuinely curious)

When I pull down a fully clean version of latest, it seems to present the exact same issue, though its not all commands for example the consumer list and cache status/flush commands still function on clean and this pr.

@engcom-Charlie
Copy link
Contributor

@magento create issue

@m2-assistant m2-assistant bot mentioned this pull request Apr 23, 2024
5 tasks
@engcom-Charlie engcom-Charlie added the Priority: P2 A defect with this priority could have functionality issues which are not to expectations. label Apr 23, 2024
@qsolutions-pl
Copy link
Contributor Author

Just a small update on my end:
My local copy has the entire configuration in the config.php on purpose as I have a project that has over 10000 with unique configuration values.
The larger that file is, config.php the slower it's processing becomes.
This file is loaded at least 10 times and processed multiple times for some reason and there is no need to do it.
Basically it browser through the tree till it reaches a configuration value.
I'll dig into this more once I have some time, for now I've reduced large store loading time by over 200ms.
Please do not close this issue, lets continue this discussion.

@m2-community-project m2-community-project bot moved this from Review in Progress to Changes Requested in Pull Requests Dashboard May 8, 2024
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Priority: P2 A defect with this priority could have functionality issues which are not to expectations. Progress: needs update
Projects
Pull Requests Dashboard
  
Changes Requested
Development

Successfully merging this pull request may close these issues.

[Issue] Performance improvements