DatabaseRegion cleanup and documentation

Goal: improve understanding of observation performance.
Context: https://github.com/groue/GRDB.swift/issues/441
This commit is contained in:
Gwendal Roué committed 2018-11-17 12:46:01 +01:00
1 parent 1108ba4bf2
commit d3fd838c6c
1 file changed
+29 -22
+29 -22
View File
@@ -47,7 +47,10 @@ public struct DatabaseRegion: CustomStringConvertible, Equatable {
/// Returns whether the region is empty.
public var isEmpty: Bool {
guard let tableRegions = tableRegions else { return false }
guard let tableRegions = tableRegions else {
// full database
return false
}
return tableRegions.isEmpty
}
@@ -122,7 +125,7 @@ public struct DatabaseRegion: CustomStringConvertible, Equatable {
guard let otherTableRegions = other.tableRegions else { return .fullDatabase }
var tableRegionsUnion: [String: TableRegion] = [:]
let tableNames = Set(tableRegions.map { $0.key }).union(Set(otherTableRegions.map { $0.key }))
let tableNames = Set(tableRegions.keys).union(Set(otherTableRegions.keys))
for table in tableNames {
let tableRegion = tableRegions[table]
let otherTableRegion = otherTableRegions[table]
@@ -154,7 +157,7 @@ extension DatabaseRegion {
/// Returns whether the content in the region would be impacted if the
/// database were modified by an event of this kind.
public func isModified(byEventsOfKind eventKind: DatabaseEventKind) -> Bool {
return !intersection(eventKind.modifiedRegion).isEmpty
return intersection(eventKind.modifiedRegion).isEmpty == false
}
/// Returns whether the content in the region is impacted by this event.
@@ -164,31 +167,27 @@ extension DatabaseRegion {
/// region.isModified(byEventsOfKind:)
public func isModified(by event: DatabaseEvent) -> Bool {
guard let tableRegions = tableRegions else {
// Full database: all changes are impactful
return true
}
switch tableRegions.count {
case 1:
// The precondition applies here:
if tableRegions.count == 1 {
// Fast path when the region contains a single table.
//
// The region contains a single table. Due to the
// filtering of events performed in observes(eventsOfKind:), the
// event argument is guaranteed to be about the fetched table.
// We thus only have to check for rowIds.
assert(event.tableName == tableRegions[tableRegions.startIndex].key) // sanity check
guard let rowIds = tableRegions[tableRegions.startIndex].value.rowIds else {
return true
}
return rowIds.contains(event.rowID)
default:
// We can apply the precondition: due to the filtering of events
// performed in observes(eventsOfKind:), the event argument is
// guaranteed to be about the fetched table. We thus only have to
// check for rowIds.
assert(event.tableName == tableRegions[tableRegions.startIndex].key) // sanity check in debug mode
let tableRegion = tableRegions[tableRegions.startIndex].value
return tableRegion.contains(rowID: event.rowID)
} else {
// Slow path when several tables are observed.
guard let tableRegion = tableRegions[event.tableName] else {
// Shouldn't happen if the precondition is met.
fatalError("precondition failure: event was not filtered out in observes(eventsOfKind:) by region.isModified(byEventsOfKind:)")
}
guard let rowIds = tableRegion.rowIds else {
return true
}
return rowIds.contains(event.rowID)
return tableRegion.contains(rowID: event.rowID)
}
}
}
@@ -201,8 +200,8 @@ extension DatabaseRegion {
case (nil, nil):
return true
case (let ltableRegions?, let rtableRegions?):
let ltableNames = Set(ltableRegions.map { $0.key })
let rtableNames = Set(rtableRegions.map { $0.key })
let ltableNames = Set(ltableRegions.keys)
let rtableNames = Set(rtableRegions.keys)
guard ltableNames == rtableNames else {
return false
}
@@ -295,6 +294,14 @@ private struct TableRegion: Equatable {
return TableRegion(columns: columnsUnion, rowIds: rowIdsUnion)
}
@inline(__always)
func contains(rowID: Int64) -> Bool {
guard let rowIds = rowIds else {
return true
}
return rowIds.contains(rowID)
}
}
// MARK: - DatabaseRegionConvertible