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

NEXT-11081 - Improve performance of RepositoryIterator and remove a possible next request #3602

Open
wants to merge 1 commit into
base: trunk
Choose a base branch
from

Conversation

JoshuaBehrens
Copy link
Contributor

0. Context

This is a follow-up pull request to #1311 . The reasoning did not change but I rebased, add a test and moved the change from the CartRuleLoader into the RuleLoader as this has been moved.

1. Why is this change necessary?

The RuleLoader already makes this educated guess that there won't be a next result when the current iteration doesn't fill the required criteria limit. Why don't do this in general?

2. What does this change do, exactly?

Add an other state to the already stateful RepositoryIterator that knows when to stop.
During test writing I noticed, that path coverage needs a different entity definition, that is iterated differently. But to reset an iterator you need to know how to reset the criteria. So I added a reset method as well.

3. Describe each step to reproduce the issue or behaviour.

  1. Load rules
  2. Load a lot of rules
  3. This seems to take more time than I want to
  4. Aha! The last response is empty anyway?
  5. Let's just skip it
  6. Load products
  7. Load a lot of products
  8. This seems to take more time than I want to
  9. Wait a minute. I have done this before. Let's copy pasta

4. Please link to the relevant issues (if any).

https://issues.shopware.com/issues/NEXT-11081

5. Checklist

  • I have rebased my changes to remove merge conflicts
  • I have written tests and verified that they fail without my change
  • I have created a changelog file with all necessary information about my changes
  • I have written or adjusted the documentation according to my changes
  • This change has comments for package types, values, functions, and non-obvious lines of code
  • I have read the contribution requirements and fulfil them.

Comment on lines +113 to +123
$builder = new ProductBuilder(new IdsCollection(), 'product1');
$builder->price(1);
$productRepository->create([$builder->build()], $context);

$builder = new ProductBuilder(new IdsCollection(), 'product2');
$builder->price(1);
$productRepository->create([$builder->build()], $context);

$builder = new ProductBuilder(new IdsCollection(), 'product3');
$builder->price(1);
$productRepository->create([$builder->build()], $context);
Copy link
Member

Choose a reason for hiding this comment

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

create the products with one create call. to make the test faster

$criteria->setLimit(2);

$searchesCount = 0;
$eventDispatcher->addListener(EntitySearchedEvent::class, function (EntitySearchedEvent $event) use (&$searchesCount): void {
Copy link
Member

Choose a reason for hiding this comment

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

this is evil as nobody will remove your addListener. use $this->addEventListener that does that for you

$iterator = new RepositoryIterator($productRepository, $context, $criteria);

while ($iterator->fetchIds() !== null) {
// fetch all ids and count searches
Copy link
Member

Choose a reason for hiding this comment

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

I would collect the data and assert if we really got so many results back.

@@ -56,6 +58,23 @@ public function __construct(
$this->context = clone $context;
}

public function reset(): void
Copy link
Member

Choose a reason for hiding this comment

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

Maybe name it rewind ? https://www.php.net/manual/en/function.rewind.php That's what php use for such things.

A simple php doc would also be nice:

Rewinds the Iterator to the beginning

@@ -71,19 +90,29 @@ public function getTotal(): int
*/
public function fetchIds(): ?array
{
if ($this->endReached) {
Copy link
Member

Choose a reason for hiding this comment

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

What about

$repositoryIterator->loop(function(array $entities) {
            
});

$repositoryIterator->loopIds(function(array $ids) {
            
});

In this case we don't have to leak all of this internal "rewind" etc

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Good thought. How would one break this loop? Return false? In my suggestions of the getIterator implementation you have the option to just not continue using the iterator. But I like the closure approach. Feels not very php though

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Area: Core Work in progress Proposal is work in progress
Projects
None yet
Development

Successfully merging this pull request may close these issues.

None yet

4 participants