Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
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).