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

Avoid PersistentCollection::isEmpty() to fully load the collection. #912

Merged
merged 1 commit into from
Feb 8, 2014

Conversation

BenMorel
Copy link
Contributor

This is a suggested simple performance improvement for PersistentCollection.

At the moment, isEmpty() always fully loads the collection. By changing the code to rely on count(), which makes advantage of extra lazy fetch when the collection is not loaded, this saves the overhead of loading the entire collection when one just wants to check whether the collection is empty.

@doctrinebot
Copy link

Hello,

thank you for creating this pull request. I have automatically opened an issue
on our Jira Bug Tracker for you. See the issue link:

http://www.doctrine-project.org/jira/browse/DDC-2921

We use Jira to track the state of pull requests and the versions they got
included in.

@Ocramius
Copy link
Member

@BenMorel needs a test :-)

@BenMorel
Copy link
Contributor Author

@Ocramius I thought this would be covered by existing tests TBH, as it's not changing existing functionality.
But no problem, I will add one later!

@Ocramius
Copy link
Member

@BenMorel future changes shall not re-introduce the additional query ;)

@BenMorel
Copy link
Contributor Author

True, I understand now ;-)
Thank you, will do that ASAP.

@Ocramius
Copy link
Member

Ocramius commented Feb 6, 2014

@BenMorel ping?

@BenMorel
Copy link
Contributor Author

BenMorel commented Feb 6, 2014

@Ocramius I didn't find the time yet, will try to do this over the week-end!

@Ocramius
Copy link
Member

Ocramius commented Feb 6, 2014

Thanks!

@BenMorel
Copy link
Contributor Author

BenMorel commented Feb 8, 2014

@Ocramius I added a functional test to ensure that the collection is not initialized after calling ìsEmpty(), and also that isEmpty() produces the correct result.

Also, fixed a wrong documentation type (array instead of Collection) in the PersistentCollection constructor.

$this->initialize();

return $this->coll->isEmpty();
return $this->count() == 0;
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 this could be even more improved, by adding a check whether any NEW entities are present (which would avoid count() thus save sql query).

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Not sure to understand what you mean, Michael: when you load an entity and one property is a PersistentCollection, you can't know whether it contains any entity before doing an SQL query, even if no NEW entities have been added to it?

Copy link
Member

Choose a reason for hiding this comment

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

@BenMorel it would probably just be the following:

public function isEmpty()
{
    return $this->coll->isEmpty() && $this->count() === 0;
}

That would indeed save 1 query if a PersistentCollection#add() happened before calling PersistentCollection#count()

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Oh, I get it now. Good point. Let me have a look at that.

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Ok this is changed, exactly as you suggested @Ocramius !
@Majkl578 Can you please review and let us know if this is what you meant?

Copy link
Contributor

Choose a reason for hiding this comment

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

Exactly what I meant. 👍

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Great then, thanks for the suggestion, and thanks @beberlei for merging at the speed of light!

@beberlei
Copy link
Member

beberlei commented Feb 8, 2014

This is a genious change :) very nice

beberlei added a commit that referenced this pull request Feb 8, 2014
Avoid PersistentCollection::isEmpty() to fully load the collection.
@beberlei beberlei merged commit 31a2870 into doctrine:master Feb 8, 2014
@BenMorel BenMorel deleted the collection-count branch February 8, 2014 15:20
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

5 participants