Repository navigation
perf(core): avoid per-owner scans on the owning side of M:N - #8392
Merged
Merged
Conversation
On drivers without pivot tables (MongoDB), hydrating the owning side of a M:N relation filtered the full children list once per owner and then sorted with indexOf. Index the loaded children once and read each owner's references directly. The resulting order is unchanged: the reference order by default, or the order of loaded children when a custom order is used.
Codecov Report✅ All modified and coverable lines are covered by tests. Additional details and impacted files@@ Coverage Diff @@
## master #8392 +/- ##
========================================
Coverage 99.66% 99.66%
========================================
Files 293 293
Lines 32000 32000
Branches 8621 9185 +564
========================================
Hits 31894 31894
Misses 102 102
Partials 4 4 ☔ View full report in Codecov by Harness. 🚀 New features to boost your workflow:
|
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
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.
Follow-up to #8391, which handled the inverse side. This is the owning-side branch of
initializeManyToMany, which I left out of that PR. Like #8391, it only runs on drivers without pivot tables (MongoDB).For each owner, the old code ran
children.filter(child => collection.contains(child))over every loaded child. Without a custom order it then sorted the result withorder.indexOf. The new code builds aMapfrom each loaded child to its position once. For each owner it reads the owner's own references and keeps the ones that were loaded. The order stays the same:Where it matters
Below is
em.find(Book, {}, { populate: ['tags'] })against a local MongoDB 8.2, using the test entities.Book.tagshasorderBy: { name: 'asc' }, so this exercises the custom-order path. Each book has 3 tags. Median of 15 runs; before and after were run alternately, twice each:Up to a few thousand books, the difference is within noise. The gain shows up when both the owner count and the loaded children grow.
I also benchmarked the loop on its own, with the output compared at every size. With the default order it is faster from 10 owners × 30 children (1.5×) up to 175× at 1000 × 5000. With a custom order, the smallest case (10 × 30) is about 1 µs slower because of the extra sort (0.7×). It breaks even around 20 × 100, and is 86× faster at 1000 × 5000.
Tests
I ran the Mongo test files against a local
mongod. The failures matchmaster: they are the suites that need MySQL, Postgres and other databases I don't have running locally.To check the new code is really covered:
With the change in place they all pass.
oxfmt,oxlintandtsc --noEmit -p packages/coreare clean.https://claude.ai/code/session_01W9Mk18mBNfkPfgg3hYweKq