Skip to content

Improve new empty item creation by using clone when possible #36369

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

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

Conversation

igorwulff
Copy link
Contributor

@igorwulff igorwulff commented Oct 25, 2022

This patch improves creation time of new objects by using clone whenever possible. I'm still testing this, but would like to get feedback on this. I've been adding a timer that compared the old and new result in the same transaction, when navigating through the site there are some gains to be found. Especially when cached are more cold (like block_html), admin areas like category view show the biggest gains:

I can track per transaction type, these are just logged results for now from across frontend and admin area:
TIMER:-19.572875 ms COUNTER: 2741
TIMER:-1.616937 ms COUNTER: 16
TIMER:-7.571272 ms COUNTER: 1506
TIMER:0 ms COUNTER: 0
TIMER:-1.572855 ms COUNTER: 16
TIMER:-23.672388 ms COUNTER: 801
TIMER:-3.223335 ms COUNTER: 11
TIMER:-13.592401 ms COUNTER: 75
TIMER:-2.116431 ms COUNTER: 11
TIMER:-7.662507 ms COUNTER: 74
TIMER:-4.198262 ms COUNTER: 49
TIMER:-72.5475 ms COUNTER: 5855
TIMER:-4.133812 ms COUNTER: 11
TIMER:-4.779091 ms COUNTER: 49
TIMER:-2.301819 ms COUNTER: 11
TIMER:-8.377065 ms COUNTER: 813
TIMER:-6.833618 ms COUNTER: 813
TIMER:-2.434347 ms COUNTER: 11
TIMER:-3.818474 ms COUNTER: 49
TIMER:-10.169898 ms COUNTER: 814
TIMER:-3.050243 ms COUNTER: 11
TIMER:-3.200632 ms COUNTER: 49
TIMER:-5.976394 ms COUNTER: 813
TIMER:-2.748932 ms COUNTER: 11
TIMER:-3.96872 ms COUNTER: 49
TIMER:-106.360966 ms COUNTER: 5855
TIMER:-3.895319 ms COUNTER: 11
TIMER:-3.334799 ms COUNTER: 49
TIMER:-2.288225 ms COUNTER: 11
TIMER:-60.319299 ms COUNTER: 5855
TIMER:-3.454251 ms COUNTER: 11
TIMER:-3.306505 ms COUNTER: 49
TIMER:-2.563021 ms COUNTER: 11
TIMER:-88.340151 ms COUNTER: 5856
TIMER:-3.875317 ms COUNTER: 11
TIMER:-4.289538 ms COUNTER: 49
TIMER:-2.396556 ms COUNTER: 11
TIMER:-8.741497 ms COUNTER: 813
TIMER:-2.273909 ms COUNTER: 11
TIMER:-3.113306 ms COUNTER: 49
TIMER:-9.474795 ms COUNTER: 674
TIMER:-4.063281 ms COUNTER: 49

Description (*)

Related Pull Requests

Fixed Issues (if relevant)

  1. Fixes magento/magento2#<issue_number>

Manual testing scenarios (*)

  1. ...
  2. ...

Questions or comments

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)

This patch improves creation time of new objects by using clone whenever possible. I'm still testing this, but would like to get feedback on this:

Old version with PLP with all caches enabled:
COUNTER:129
Timer:17.240343 ms

Old version with PLP empty caches:
COUNTER:4470
Timer: 381.315609 ms

Old version with PLP no block cache:
COUNTER:2398
Timer:202.855686 ms

New version with PLP no block cache:
COUNTER:2398
Timer: 2.540631 ms

    
Old version Category Admin page:
COUNTER:5919
Timer: 1423.777139 ms

New version Category Admin page:
COUNTER:5919
Timer: 8.040596ms
@m2-assistant
Copy link

m2-assistant bot commented Oct 25, 2022

Hi @igorwulff. Thank you for your contribution
Here are 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

❗ 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 Magento Contributor Guide documentation.

⚠️ According to the Magento Contribution requirements, all Pull Requests must go through the Community Contributions Triage process. Community Contributions Triage is a public meeting.

🕙 You can find the schedule on the Magento Community Calendar page.

📞 The triage of Pull Requests happens in the queue order. If you want to speed up the delivery of your contribution, join the Community Contributions Triage session to discuss the appropriate ticket.

✏️ Feel free to post questions/proposals/feedback related to the Community Contributions Triage process to the corresponding Slack Channel

@ihor-sviziev
Copy link
Contributor

@igorwulff I don't think it's the right thing since clone might cause copying data from an existing object to new one, while earlier, it was always creating a new one.

@ihor-sviziev ihor-sviziev added Priority: P2 A defect with this priority could have functionality issues which are not to expectations. Area: Performance labels Oct 26, 2022
@igorwulff
Copy link
Contributor Author

@igorwulff I don't think it's the right thing since clone might cause copying data from an existing object to new one, while earlier, it was always creating a new one.

You are right. I will think about possible alternative ways to see if and how this can be done.

Update collection for a local instance cache which we can use to clone new empty objects.
Comment on lines 620 to 624
if ($this->newEmptyItem !== null
&& $this->_itemObjectClass !== $className
) {
$this->newEmptyItem === null;
}
Copy link
Contributor

Choose a reason for hiding this comment

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

I think it's redundant code because this is mean that one collection could have multiple items with different classes.

Copy link
Contributor Author

Choose a reason for hiding this comment

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

@Den4ik I've removed the redundant code.

Revert changes to setItemObjectClass method and removed the additional checks in there that should not be needed.
@igorwulff
Copy link
Contributor Author

@magento run all tests

@magento-automated-testing
Copy link

The requested builds are added to the queue. You should be able to see them here within a few minutes. Please re-request them if they don't show in a reasonable amount of time.

@igorwulff
Copy link
Contributor Author

@magento run all tests

@magento-automated-testing
Copy link

The requested builds are added to the queue. You should be able to see them here within a few minutes. Please re-request them if they don't show in a reasonable amount of time.

@igorwulff
Copy link
Contributor Author

@magento run WebAPI Tests

@magento-automated-testing
Copy link

The requested builds are added to the queue. You should be able to see them here within a few minutes. Please re-request them if they don't show in a reasonable amount of time.

@igorwulff
Copy link
Contributor Author

Hi @Den4ik can you clarify for me if the issues in the tests are related/caused by the changes in this PR?

Some object utilize xml properies to load more data and parameters into them, like the indexers through the constructor.
Checking if the empty item is just an instanceof \Magento\Framework\Model\AbstractModel might be enough to circumvent this potential problem.
@igorwulff
Copy link
Contributor Author

@magento run all tests

@magento-automated-testing
Copy link

The requested builds are added to the queue. You should be able to see them here within a few minutes. Please re-request them if they don't show in a reasonable amount of time.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Area: Performance Priority: P2 A defect with this priority could have functionality issues which are not to expectations. Progress: pending review
Projects
None yet
Development

Successfully merging this pull request may close these issues.

3 participants