Add ManagedObjectCache#16
Merged
Merged
Conversation
3fae799 to
f91dbbb
Compare
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.
Fix quadratic performance in
NSManagedObjectContext.insert(_:model:)batch insertsProblem
Batch inserts degrade quadratically with batch size. Inserting a real-world dataset
(11,421 objects with relationships) took ~46 seconds, while the same data inserted
as 360 small groups took ~2.5s — the opposite of what batching should do.
Two causes, both in the find-or-create path:
insert(_ values:model:)issues one fetch request per object to find-or-create it.Core Data fetches evaluate their predicate against every pending (unsaved) object
in the context, so as a batch accumulates inserted-but-unsaved objects, each
subsequent find gets slower — O(n²) overall.
setRelationshipresolves every relationship target with another per-objectcontext.find, which pays the same pending-changes penalty.Fix
insert(_ values:model:)now:target IDs — with a single
INfetch request per entity, into aManagedObjectCache([EntityName: [ObjectID: NSManagedObject]]), before anythingis inserted (pending set is empty, so these fetches are clean SQL).
proves the object doesn't exist — no fallback fetch. (This mattered: with the
fallback in place, the empty-database case still spent ~29s in fetches.)
values in the same batch resolve regardless of their order in the array.
setValues/setRelationshipoverloads, then saves once.
The single-value
insert(_ value:model:)path is unchanged; the originalsetValues/setRelationshipsignatures delegate through an empty cache.Results
360-site catalog, 11,421 objects with to-many relationships (M-series Mac):
Post-fix single-batch breakdown: prefetch 0.03s · create 0.02s · setValues 0.08s · save 0.12s.
Behavior notes
(entity, id)pairs in one batch resolve to the same managed object withlast-write-wins, matching the previous loop's behavior.
toOnereferencing a value later in the array could resolve to
nil).CoreModelError.invalidEntityup front instead offailing inside a fetch request.
Testing