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

Feature: Faster Eager Loading #550

Merged

Conversation

brianmuse
Copy link
Contributor

This PR improves the logic of eager loading, especially when eager loading a lot of related models.

The existing code would loop through each model that was being eager loaded onto and then (nested) loop through all the possible related models to add those relationships.

The new approach first maps all the related models by their keys so that in the second loop they are easily accessible without traversing the entire array.

My big O notation is rusty, but I think this is roughly--
Previously: O(n*m)
Now: O(m) + O(n*log(m))

..but actually a little bit better cause we also improved the $used_model array to be a map as well.

I first noticed that this took a while when dealing with an eager load that loaded ~200 models onto 10 results. Anecdotally on my local machine, this took that from a 400ms task to about 150ms (it still took 150ms to instantiate the 200 models, but that's a separate issue).

@Rican7
Copy link
Collaborator

Rican7 commented Aug 11, 2016

Just going to reference robinpowered#11 here.

@Rican7
Copy link
Collaborator

Rican7 commented Aug 11, 2016

This LGTM 👍

@jpfuentes2
Copy link
Owner

Love it; nice work!

@jpfuentes2 jpfuentes2 merged commit 7220379 into jpfuentes2:master Aug 13, 2016
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.

3 participants