Skip to content

Move reference priming to Cursor #1068

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

Merged
merged 1 commit into from
Apr 5, 2015
Merged

Move reference priming to Cursor #1068

merged 1 commit into from
Apr 5, 2015

Conversation

alcaeus
Copy link
Member

@alcaeus alcaeus commented Mar 31, 2015

This PR moves reference priming from the Query::execute method to the cursor which iterates over the results (as described in #1065). This increases performance when iterating over part of a large query result (e.g. by setting skip and limit on the returned cursor before iterating) as it will not prime references for the entire result set.
Note that using prime for findAndUpdate and findAndRemove will prime references when executing the query since it will not return a cursor.
This also fixes a side effect of #520 where using getSingleResult() on a query without limit in combination with prime would prime references for all documents returned in the query instead of just the one returned by getSingleResult().

Feedback is greatly appreciated, I'd really appreciate it if some people using reference priming could run their test suite against the branch and post some results.

NB: I'm not too happy with having to duplicate functionality in Cursor and EagerCursor, unfortunately I can't think of another way since the two classes don't share an interface.

@malarzm malarzm added the Idea label Apr 2, 2015
@jwage jwage added this to the 1.0.0-BETA13 milestone Apr 5, 2015
@jwage
Copy link
Member

jwage commented Apr 5, 2015

Nice. Looks good to me! 👍

jwage added a commit that referenced this pull request Apr 5, 2015
Move reference priming to Cursor
@jwage jwage merged commit 328c1eb into doctrine:master Apr 5, 2015
@jwage jwage added Feature and removed Idea labels Apr 5, 2015
@alcaeus alcaeus deleted the cursor-priming branch April 7, 2015 07:02
@smolinari
Copy link

👍 Thanks for the improvement!

Scott

@blockjon
Copy link

Could this PR have caused #1108 ?

@jmikola
Copy link
Member

jmikola commented May 29, 2015

@blockjon: I just looked at this PR diff and I suspect that the new call to primeReferences() from within current() (here) may be related to #1108. Since MongoCursor iteration uses the standard Iterator interface methods, the order of execution will be rewind(), valid(), and then current(). Here, it looks like we're invoking rewind() once again on each first call to current(). So even if no reference priming is requested, it looks as if the query gets re-executed at least one additional time (after the foreach on configured primers, each of which rewind on their own, too!).

Even considering the original priming code, I don't see any reason that this should be re-executing the cursor for each primer. There is no guarantee that the query is going to return the same results on successive executions. Although it would come at an expense of memory, I think that priming would best be restricted to EagerCursors, where all of the documents are cached in an array and no re-execution takes place (rewind() simply resets the array pointer).

@jwage, @alcaeus, @malarzm: Thoughts?

@malarzm
Copy link
Member

malarzm commented May 29, 2015

@jmikola sounds reasonable. So in QueryBuilder ODM will enforce creating query returning EagerCursor if prime() is called, right?

@jmikola
Copy link
Member

jmikola commented May 29, 2015

I think it'd be reasonable to say that:

  • prime(true) implies eagerCursor(true) (if eager cursor was never set).
  • prime(true) throws if eagerCursor(false) was previously called
  • eagerCursor(false) throws if prime(true) was previously called

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Projects
None yet
Development

Successfully merging this pull request may close these issues.

6 participants