Skip to content

Core data optimization #1

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

Open
wants to merge 7 commits into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions .gitignore
Original file line number Diff line number Diff line change
@@ -1,2 +1,3 @@
*.xcuserdatad

RealmVsSwiftData.xcodeproj/project.xcworkspace/xcshareddata/swiftpm/Package.resolved

This file was deleted.

8 changes: 8 additions & 0 deletions RealmVsSwiftData/DB/CoreData/CoreDatabase.swift
Original file line number Diff line number Diff line change
Expand Up @@ -27,17 +27,25 @@ extension CoreDatabase {
var fileURL: URL? {
container.configurations.first?.url
}

var context: ModelContext {
return ModelContext(container)
}

func create(_ items: [T]) throws {
let context = ModelContext(container)
items.forEach { $0.insertWithAllDependencies(into: context) }
try context.save()

context.reset()
}

func create(_ item: T) throws {
let context = ModelContext(container)
item.insertWithAllDependencies(into: context)
try context.save()

context.reset()
}

func read(predicate: Filtering?, sortBy sortDescriptors: [NSSortDescriptor]) throws -> [T] {
Expand Down
13 changes: 11 additions & 2 deletions RealmVsSwiftData/DB/CoreData/Users/CoreUser.swift
Original file line number Diff line number Diff line change
Expand Up @@ -28,9 +28,18 @@ final class CoreUser: NSManagedObject, User {
}
self.init(firstName: firstName, surname: surname, age: Int.random(in: 0..<99))
}

convenience init(moc: ModelContext? = nil) {
guard let firstName = firstNames.randomElement(),
let surname = surnames.randomElement() else {
//throw UserError.nameNotFound
fatalError("Name not found")
}
self.init(context: moc, firstName: firstName, surname: surname, age: Int.random(in: 0..<99))
}

convenience init(firstName: String, surname: String, age: Int) {
self.init(context: nil)
convenience init(context: ModelContext? = nil, firstName: String, surname: String, age: Int) {
self.init(context: context)
self.id = UUID()
self.firstName = firstName
self.surname = surname
Expand Down
58 changes: 58 additions & 0 deletions RealmVsSwiftData/PerformanceTesting/CoreUsersTests.swift
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@ func coreUsersPerformanceTests(with usersCount: Int = 100_000) {

try! db.deleteAll()

/*
var users = [CoreUser]()
logExecutionTime("User instantiation") {
users = (0..<usersCount).compactMap { _ in CoreUser() }
Expand All @@ -24,7 +25,64 @@ func coreUsersPerformanceTests(with usersCount: Int = 100_000) {
logExecutionTime("Create users") {
try! db.create(users)
}
*/
/*
let importer: (Int) -> Void = { limit in
var users = [CoreUser]()
logExecutionTime("User instantiation") {
users = (0..<limit).compactMap { _ in CoreUser() }
}

logExecutionTime("Create users") {
try! db.create(users)
}
}

let chunk = 10_000
if usersCount <= chunk {
importer(usersCount)

} else {
logExecutionTime("Total \( usersCount ) User instantiation + Create") {
let limit = usersCount / chunk
for i in (0 ..< limit) {
print("Chunk count: \( i )")
importer(chunk)
}
}
}
*/
let importer2: (Int) -> Void = { limit in
let context = db.context
context.performAndWait {
logExecutionTime("User instantiation") {
(0..<limit).forEach { _ in
let _ = CoreUser(moc: context)
}
}

logExecutionTime("Save users") {
try! context.save()
}
}

context.reset()
}

let chunk = 100_000
if usersCount <= chunk {
importer2(usersCount)

} else {
logExecutionTime("Total \( usersCount ) User instantiation + Create") {
let limit = usersCount / chunk
for i in (0 ..< limit) {
print("Chunk count: \( i )")
importer2(chunk)
}
}
}

logExecutionTime("Fetch users named `Jane` in age order") {
let predicate = NSPredicate(format: "firstName = %@", "Jane")
let janes = try! db.read(predicate: predicate, sortBy: NSSortDescriptor(key: "age", ascending: true))
Expand Down
94 changes: 94 additions & 0 deletions core-data-results-14Pro/14Pro-core-data-01-context-reset.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1,94 @@

💽 ===============
💽 CoreData: 100 Simple Objects
===============
💽 User instantiation: 0.0015
💽 Create users: 0.0099
0 users named `Jane`
💽 Fetch users named `Jane` in age order: 0.0009
0 users named `Jane` being renamed to `Wendy`
0 users renamed to `Wendy`
💽 Rename users named `Jane` to `Wendy`: 0.0005
💽 DB file size: 0.03 MB
💽 Delete all users: 0.0007

💽 ===============
💽 CoreData: 1.000 Simple Objects
===============
CoreData: debug: PostSaveMaintenance: incremental_vacuum with freelist_count - 2 and pages_to_free 0
💽 User instantiation: 0.0054
💽 Create users: 0.0146
0 users named `Jane`
💽 Fetch users named `Jane` in age order: 0.0006
0 users named `Jane` being renamed to `Wendy`
0 users renamed to `Wendy`
💽 Rename users named `Jane` to `Wendy`: 0.0004
💽 DB file size: 0.03 MB
💽 Delete all users: 0.0012

💽 ===============
💽 CoreData: 10.000 Simple Objects
===============
CoreData: debug: PostSaveMaintenance: incremental_vacuum with freelist_count - 19 and pages_to_free 12
💽 User instantiation: 0.0293
💽 Create users: 0.0767
3 users named `Jane`
💽 Fetch users named `Jane` in age order: 0.0022
3 users named `Jane` being renamed to `Wendy`
3 users renamed to `Wendy`
💽 Rename users named `Jane` to `Wendy`: 0.0007
💽 DB file size: 0.03 MB
💽 Delete all users: 0.0061

💽 ===============
💽 CoreData: 100.000 Simple Objects
===============
CoreData: debug: PostSaveMaintenance: incremental_vacuum with freelist_count - 179 and pages_to_free 172
💽 User instantiation: 0.2063
CoreData: debug: PostSaveMaintenance: fileSize 9344192 greater than prune threshold
CoreData: annotation: PostSaveMaintenance: wal_checkpoint(TRUNCATE)
💽 Create users: 0.9180
22 users named `Jane`
💽 Fetch users named `Jane` in age order: 0.0306
22 users named `Jane` being renamed to `Wendy`
22 users renamed to `Wendy`
💽 Rename users named `Jane` to `Wendy`: 0.0082
💽 DB file size: 7.26 MB
💽 Delete all users: 0.1025

💽 ===============
💽 CoreData: 1.000.000 Simple Objects
===============
CoreData: debug: PostSaveMaintenance: incremental_vacuum with freelist_count - 1850 and pages_to_free 1841
💽 User instantiation: 2.1899
CoreData: debug: PostSaveMaintenance: fileSize 76417792 greater than prune threshold
CoreData: annotation: PostSaveMaintenance: wal_checkpoint(TRUNCATE)
💽 Create users: 30.8261
197 users named `Jane`
💽 Fetch users named `Jane` in age order: 0.5982
197 users named `Jane` being renamed to `Wendy`
197 users renamed to `Wendy`
💽 Rename users named `Jane` to `Wendy`: 0.0688
💽 DB file size: 72.45 MB
CoreData: debug: PostSaveMaintenance: fileSize 68412632 greater than prune threshold
CoreData: debug: PostSaveMaintenance: incremental_vacuum with freelist_count - 18518 and pages_to_free 18489
CoreData: annotation: PostSaveMaintenance: wal_checkpoint(TRUNCATE)
💽 Delete all users: 3.0144
BUG IN CLIENT OF libsqlite3.dylib: database integrity compromised by API violation: vnode unlinked while in use: /private/var/mobile/Containers/Data/Application/51359F89-478B-4F3A-86B7-209D07C6D36F/Library/Application Support/default.store
invalidated open fd: 15 (0x11)
BUG IN CLIENT OF libsqlite3.dylib: database integrity compromised by API violation: vnode unlinked while in use: /private/var/mobile/Containers/Data/Application/51359F89-478B-4F3A-86B7-209D07C6D36F/Library/Application Support/default.store
invalidated open fd: 15 (0x11)
BUG IN CLIENT OF libsqlite3.dylib: database integrity compromised by API violation: vnode unlinked while in use: /private/var/mobile/Containers/Data/Application/51359F89-478B-4F3A-86B7-209D07C6D36F/Library/Application Support/CoreUserDB
invalidated open fd: 15 (0x11)
BUG IN CLIENT OF libsqlite3.dylib: database integrity compromised by API violation: vnode unlinked while in use: /private/var/mobile/Containers/Data/Application/51359F89-478B-4F3A-86B7-209D07C6D36F/Library/Application Support/CoreUserDB
BUG IN CLIENT OF libsqlite3.dylib: database integrity compromised by API violation: vnode unlinked while in use: /private/var/mobile/Containers/Data/Application/51359F89-478B-4F3A-86B7-209D07C6D36F/Library/Application Support/CoreUserDB
invalidated open fd: 10 (0x11)
BUG IN CLIENT OF libsqlite3.dylib: database integrity compromised by API violation: vnode unlinked while in use: /private/var/mobile/Containers/Data/Application/51359F89-478B-4F3A-86B7-209D07C6D36F/Library/Application Support/CoreUserDB
invalidated open fd: 13 (0x11)
BUG IN CLIENT OF libsqlite3.dylib: database integrity compromised by API violation: vnode unlinked while in use: /private/var/mobile/Containers/Data/Application/51359F89-478B-4F3A-86B7-209D07C6D36F/Library/Application Support/CoreUserDB
invalidated open fd: 3 (0x11)
BUG IN CLIENT OF libsqlite3.dylib: database integrity compromised by API violation: vnode unlinked while in use: /private/var/mobile/Containers/Data/Application/51359F89-478B-4F3A-86B7-209D07C6D36F/Library/Application Support/CoreUserDB
invalidated open fd: 8 (0x11)
invalidated open fd: 6 (0x11)
BUG IN CLIENT OF libsqlite3.dylib: database integrity compromised by API violation: vnode unlinked while in use: /private/var/mobile/Containers/Data/Application/51359F89-478B-4F3A-86B7-209D07C6D36F/Library/Application Support/CoreStudentDB
invalidated open fd: 16 (0x11)
97 changes: 97 additions & 0 deletions core-data-results-14Pro/14Pro-core-data-01.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1,97 @@

💽 ===============
💽 CoreData: 100 Simple Objects
===============
💽 User instantiation: 0.0008
💽 Create users: 0.0129
0 users named `Jane`
💽 Fetch users named `Jane` in age order: 0.0004
0 users named `Jane` being renamed to `Wendy`
0 users renamed to `Wendy`
💽 Rename users named `Jane` to `Wendy`: 0.0003
💽 DB file size: 0.03 MB
💽 Delete all users: 0.0003

💽 ===============
💽 CoreData: 1.000 Simple Objects
===============
CoreData: debug: PostSaveMaintenance: incremental_vacuum with freelist_count - 2 and pages_to_free 0
💽 User instantiation: 0.0035
💽 Create users: 0.0102
0 users named `Jane`
💽 Fetch users named `Jane` in age order: 0.0002
0 users named `Jane` being renamed to `Wendy`
0 users renamed to `Wendy`
💽 Rename users named `Jane` to `Wendy`: 0.0002
💽 DB file size: 0.03 MB
💽 Delete all users: 0.0009

💽 ===============
💽 CoreData: 10.000 Simple Objects
===============
CoreData: debug: PostSaveMaintenance: incremental_vacuum with freelist_count - 18 and pages_to_free 11
💽 User instantiation: 0.0248
💽 Create users: 0.0745
2 users named `Jane`
💽 Fetch users named `Jane` in age order: 0.0006
2 users named `Jane` being renamed to `Wendy`
2 users renamed to `Wendy`
💽 Rename users named `Jane` to `Wendy`: 0.0007
💽 DB file size: 0.03 MB
💽 Delete all users: 0.0059

💽 ===============
💽 CoreData: 100.000 Simple Objects
===============
CoreData: debug: PostSaveMaintenance: incremental_vacuum with freelist_count - 180 and pages_to_free 173
💽 User instantiation: 0.2107
CoreData: debug: PostSaveMaintenance: fileSize 9315352 greater than prune threshold
CoreData: annotation: PostSaveMaintenance: wal_checkpoint(TRUNCATE)
💽 Create users: 0.8991
23 users named `Jane`
💽 Fetch users named `Jane` in age order: 0.0053
23 users named `Jane` being renamed to `Wendy`
23 users renamed to `Wendy`
💽 Rename users named `Jane` to `Wendy`: 0.0081
💽 DB file size: 7.22 MB
💽 Delete all users: 0.0867

💽 ===============
💽 CoreData: 1.000.000 Simple Objects
===============
CoreData: debug: PostSaveMaintenance: incremental_vacuum with freelist_count - 1839 and pages_to_free 1830
💽 User instantiation: 2.2695
CoreData: debug: PostSaveMaintenance: fileSize 76430152 greater than prune threshold
CoreData: annotation: PostSaveMaintenance: wal_checkpoint(TRUNCATE)
💽 Create users: 39.7839
188 users named `Jane`
💽 Fetch users named `Jane` in age order: 0.0687
188 users named `Jane` being renamed to `Wendy`
188 users renamed to `Wendy`
💽 Rename users named `Jane` to `Wendy`: 0.0802
💽 DB file size: 72.46 MB
CoreData: debug: PostSaveMaintenance: fileSize 68350832 greater than prune threshold
CoreData: debug: PostSaveMaintenance: incremental_vacuum with freelist_count - 18520 and pages_to_free 18491
CoreData: annotation: PostSaveMaintenance: wal_checkpoint(TRUNCATE)
💽 Delete all users: 3.8762



BUG IN CLIENT OF libsqlite3.dylib: database integrity compromised by API violation: vnode unlinked while in use: /private/var/mobile/Containers/Data/Application/F146F45A-91EE-4E8E-BB57-97674A9B0702/Library/Application Support/default.store
invalidated open fd: 15 (0x11)
BUG IN CLIENT OF libsqlite3.dylib: database integrity compromised by API violation: vnode unlinked while in use: /private/var/mobile/Containers/Data/Application/F146F45A-91EE-4E8E-BB57-97674A9B0702/Library/Application Support/default.store
invalidated open fd: 15 (0x11)
BUG IN CLIENT OF libsqlite3.dylib: database integrity compromised by API violation: vnode unlinked while in use: /private/var/mobile/Containers/Data/Application/F146F45A-91EE-4E8E-BB57-97674A9B0702/Library/Application Support/CoreUserDB
invalidated open fd: 15 (0x11)
BUG IN CLIENT OF libsqlite3.dylib: database integrity compromised by API violation: vnode unlinked while in use: /private/var/mobile/Containers/Data/Application/F146F45A-91EE-4E8E-BB57-97674A9B0702/Library/Application Support/CoreUserDB
invalidated open fd: 13 (0x11)
BUG IN CLIENT OF libsqlite3.dylib: database integrity compromised by API violation: vnode unlinked while in use: /private/var/mobile/Containers/Data/Application/F146F45A-91EE-4E8E-BB57-97674A9B0702/Library/Application Support/CoreUserDB
invalidated open fd: 8 (0x11)
BUG IN CLIENT OF libsqlite3.dylib: database integrity compromised by API violation: vnode unlinked while in use: /private/var/mobile/Containers/Data/Application/F146F45A-91EE-4E8E-BB57-97674A9B0702/Library/Application Support/CoreUserDB
BUG IN CLIENT OF libsqlite3.dylib: database integrity compromised by API violation: vnode unlinked while in use: /private/var/mobile/Containers/Data/Application/F146F45A-91EE-4E8E-BB57-97674A9B0702/Library/Application Support/CoreUserDB
BUG IN CLIENT OF libsqlite3.dylib: database integrity compromised by API violation: vnode unlinked while in use: /private/var/mobile/Containers/Data/Application/F146F45A-91EE-4E8E-BB57-97674A9B0702/Library/Application Support/CoreUserDB
invalidated open fd: 6 (0x11)
invalidated open fd: 10 (0x11)
invalidated open fd: 3 (0x11)
BUG IN CLIENT OF libsqlite3.dylib: database integrity compromised by API violation: vnode unlinked while in use: /private/var/mobile/Containers/Data/Application/F146F45A-91EE-4E8E-BB57-97674A9B0702/Library/Application Support/CoreStudentDB
invalidated open fd: 16 (0x11)
Loading