mirror of
https://github.com/groue/GRDB.swift
synced 2026-11-22 04:00:39 +00:00
Merge pull request #1881 from groue/dev/fix-1873
Fix an accidentally quadratic behavior in SQLite statement compilation
This commit is contained in:
5 files changed
+180
-5
No files matched your search
@@ -41,7 +41,8 @@
|
||||
/// - ``isModified(byEventsOfKind:)``
|
||||
/// - ``isModified(by:)``
|
||||
public struct DatabaseRegion: Sendable {
|
||||
private let tableRegions: [CaseInsensitiveIdentifier: TableRegion]?
|
||||
/// Nil means that the region is the full database.
|
||||
private var tableRegions: [CaseInsensitiveIdentifier: TableRegion]?
|
||||
|
||||
private init(tableRegions: [CaseInsensitiveIdentifier: TableRegion]?) {
|
||||
self.tableRegions = tableRegions
|
||||
@@ -77,19 +78,26 @@ public struct DatabaseRegion: Sendable {
|
||||
self.init(tableRegions: [table: TableRegion(columns: nil, rowIds: nil)])
|
||||
}
|
||||
|
||||
/// Full columns in a table: (some columns in a table) × (all rows)
|
||||
/// Some columns from all rows in a table.
|
||||
init(table: String, columns: Set<String>) {
|
||||
let table = CaseInsensitiveIdentifier(rawValue: table)
|
||||
let columns = Set(columns.map(CaseInsensitiveIdentifier.init))
|
||||
self.init(tableRegions: [table: TableRegion(columns: columns, rowIds: nil)])
|
||||
}
|
||||
|
||||
/// Full rows in a table: (all columns in a table) × (some rows)
|
||||
/// All columns from some rows in a table.
|
||||
init(table: String, rowIds: Set<Int64>) {
|
||||
let table = CaseInsensitiveIdentifier(rawValue: table)
|
||||
self.init(tableRegions: [table: TableRegion(columns: nil, rowIds: rowIds)])
|
||||
}
|
||||
|
||||
/// Some columns from some rows in a table.
|
||||
init(table: String, columns: Set<String>, rowIds: Set<Int64>) {
|
||||
let table = CaseInsensitiveIdentifier(rawValue: table)
|
||||
let columns = Set(columns.map(CaseInsensitiveIdentifier.init))
|
||||
self.init(tableRegions: [table: TableRegion(columns: columns, rowIds: rowIds)])
|
||||
}
|
||||
|
||||
/// Returns the intersection of this region and the given one.
|
||||
///
|
||||
/// This method is not public because there is no known public use case for
|
||||
@@ -162,6 +170,22 @@ public struct DatabaseRegion: Sendable {
|
||||
self = union(other)
|
||||
}
|
||||
|
||||
/// Inserts all columns and rows from the given table.
|
||||
mutating func insert(table: String) {
|
||||
guard tableRegions != nil else { return }
|
||||
let table = CaseInsensitiveIdentifier(rawValue: table)
|
||||
tableRegions![table, default: TableRegion()].insertAllRowsAndColumns()
|
||||
}
|
||||
|
||||
/// Inserts the specified column and all rows from the given table.
|
||||
mutating func insert(table: String, column: String) {
|
||||
assert(!column.isEmpty)
|
||||
guard tableRegions != nil else { return }
|
||||
let table = CaseInsensitiveIdentifier(rawValue: table)
|
||||
let column = CaseInsensitiveIdentifier(rawValue: column)
|
||||
tableRegions![table, default: TableRegion(columns: [column])].insertAllRowsAndColumn(column)
|
||||
}
|
||||
|
||||
/// Returns a region suitable for database observation
|
||||
func observableRegion(_ db: Database) throws -> DatabaseRegion {
|
||||
// SQLite does not expose schema changes to the
|
||||
@@ -370,6 +394,18 @@ private struct TableRegion: Equatable {
|
||||
return TableRegion(columns: columnsUnion, rowIds: rowIdsUnion)
|
||||
}
|
||||
|
||||
mutating func insertAllRowsAndColumns() {
|
||||
rowIds = nil
|
||||
columns = nil
|
||||
}
|
||||
|
||||
mutating func insertAllRowsAndColumn(_ column: CaseInsensitiveIdentifier) {
|
||||
rowIds = nil
|
||||
if columns != nil {
|
||||
columns!.insert(column)
|
||||
}
|
||||
}
|
||||
|
||||
func contains(rowID: Int64) -> Bool {
|
||||
guard let rowIds else {
|
||||
return true
|
||||
|
||||
@@ -117,10 +117,10 @@ final class StatementAuthorizer {
|
||||
guard let columnName = cString2.map(String.init) else { return SQLITE_OK }
|
||||
if columnName.isEmpty {
|
||||
// SELECT COUNT(*) FROM table
|
||||
selectedRegion.formUnion(DatabaseRegion(table: tableName))
|
||||
selectedRegion.insert(table: tableName)
|
||||
} else {
|
||||
// SELECT column FROM table
|
||||
selectedRegion.formUnion(DatabaseRegion(table: tableName, columns: [columnName]))
|
||||
selectedRegion.insert(table: tableName, column: columnName)
|
||||
}
|
||||
return SQLITE_OK
|
||||
|
||||
|
||||
@@ -142,6 +142,64 @@ class DatabaseRegionTests : GRDBTestCase {
|
||||
XCTAssertEqual(unions.map(\.description), ["foo(a)[1]", "foo(a,b)[1,2]", "foo(a,b)[1,2]", "foo(b)[2]"])
|
||||
}
|
||||
|
||||
func testInsertTable() {
|
||||
let regions = [
|
||||
DatabaseRegion.fullDatabase,
|
||||
DatabaseRegion(),
|
||||
DatabaseRegion(table: "foo"),
|
||||
DatabaseRegion(table: "foo", columns: ["a", "b"]),
|
||||
DatabaseRegion(table: "foo", columns: ["b", "c"]),
|
||||
DatabaseRegion(table: "foo", rowIds: [1, 2]),
|
||||
DatabaseRegion(table: "foo", columns: ["b"], rowIds: [2, 3]),
|
||||
DatabaseRegion(table: "bar")]
|
||||
|
||||
var unions: [DatabaseRegion] = []
|
||||
for var region in regions {
|
||||
region.insert(table: "foo")
|
||||
unions.append(region)
|
||||
}
|
||||
|
||||
XCTAssertEqual(unions.map(\.description), [
|
||||
"full database",
|
||||
"foo(*)",
|
||||
"foo(*)",
|
||||
"foo(*)",
|
||||
"foo(*)",
|
||||
"foo(*)",
|
||||
"foo(*)",
|
||||
"bar(*),foo(*)",
|
||||
])
|
||||
}
|
||||
|
||||
func testInsertTableColumn() {
|
||||
let regions = [
|
||||
DatabaseRegion.fullDatabase,
|
||||
DatabaseRegion(),
|
||||
DatabaseRegion(table: "foo"),
|
||||
DatabaseRegion(table: "foo", columns: ["a", "b"]),
|
||||
DatabaseRegion(table: "foo", columns: ["b", "c"]),
|
||||
DatabaseRegion(table: "foo", rowIds: [1, 2]),
|
||||
DatabaseRegion(table: "foo", columns: ["b"], rowIds: [2, 3]),
|
||||
DatabaseRegion(table: "bar")]
|
||||
|
||||
var unions: [DatabaseRegion] = []
|
||||
for var region in regions {
|
||||
region.insert(table: "foo", column: "a")
|
||||
unions.append(region)
|
||||
}
|
||||
|
||||
XCTAssertEqual(unions.map(\.description), [
|
||||
"full database",
|
||||
"foo(a)",
|
||||
"foo(*)",
|
||||
"foo(a,b)",
|
||||
"foo(a,b,c)",
|
||||
"foo(*)",
|
||||
"foo(a,b)",
|
||||
"bar(*),foo(a)",
|
||||
])
|
||||
}
|
||||
|
||||
func testRegionIntersection() {
|
||||
let regions = [
|
||||
DatabaseRegion.fullDatabase,
|
||||
|
||||
@@ -35,6 +35,8 @@
|
||||
56B6D0E62618BF78003CC455 /* FetchRecordOptimizedTests.swift in Sources */ = {isa = PBXBuildFile; fileRef = 56B6D0E42618BF78003CC455 /* FetchRecordOptimizedTests.swift */; };
|
||||
56B6D0EA2618C00C003CC455 /* InsertRecordOptimizedTests.swift in Sources */ = {isa = PBXBuildFile; fileRef = 56B6D0E92618C00C003CC455 /* InsertRecordOptimizedTests.swift */; };
|
||||
56B6D0EB2618C00C003CC455 /* InsertRecordOptimizedTests.swift in Sources */ = {isa = PBXBuildFile; fileRef = 56B6D0E92618C00C003CC455 /* InsertRecordOptimizedTests.swift */; };
|
||||
56B7DECA3063F1B50018462B /* StatementPreparationPerformanceTests.swift in Sources */ = {isa = PBXBuildFile; fileRef = 56B7DEC83063F1B30018462B /* StatementPreparationPerformanceTests.swift */; };
|
||||
56B7DECB3063F1B50018462B /* StatementPreparationPerformanceTests.swift in Sources */ = {isa = PBXBuildFile; fileRef = 56B7DEC83063F1B30018462B /* StatementPreparationPerformanceTests.swift */; };
|
||||
56D3BE711F4EB1A00034C6D2 /* FetchRecordStructTests.swift in Sources */ = {isa = PBXBuildFile; fileRef = 56D3BE701F4EB1900034C6D2 /* FetchRecordStructTests.swift */; };
|
||||
56D3BE721F4EB1A00034C6D2 /* FetchRecordStructTests.swift in Sources */ = {isa = PBXBuildFile; fileRef = 56D3BE701F4EB1900034C6D2 /* FetchRecordStructTests.swift */; };
|
||||
56D507831F6D7B2E00AE1C5B /* InsertRecordStructTests.swift in Sources */ = {isa = PBXBuildFile; fileRef = 56D507821F6D7A4500AE1C5B /* InsertRecordStructTests.swift */; };
|
||||
@@ -100,6 +102,7 @@
|
||||
56A6F4AB29BBC8E200E22662 /* ArgumentsTests.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = ArgumentsTests.swift; sourceTree = "<group>"; };
|
||||
56B6D0E42618BF78003CC455 /* FetchRecordOptimizedTests.swift */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.swift; path = FetchRecordOptimizedTests.swift; sourceTree = "<group>"; };
|
||||
56B6D0E92618C00C003CC455 /* InsertRecordOptimizedTests.swift */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.swift; path = InsertRecordOptimizedTests.swift; sourceTree = "<group>"; };
|
||||
56B7DEC83063F1B30018462B /* StatementPreparationPerformanceTests.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = StatementPreparationPerformanceTests.swift; sourceTree = "<group>"; };
|
||||
56BB86121BA9886D001F9168 /* InsertRecordClassTests.swift */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.swift; path = InsertRecordClassTests.swift; sourceTree = "<group>"; };
|
||||
56BB862D1BA98933001F9168 /* GRDBPerformanceComparisonTests-Bridging.h */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.h; path = "GRDBPerformanceComparisonTests-Bridging.h"; sourceTree = "<group>"; };
|
||||
56CA22211BB41565009A04C5 /* PerformanceTests.swift */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.swift; path = PerformanceTests.swift; sourceTree = "<group>"; };
|
||||
@@ -158,6 +161,7 @@
|
||||
56BB86101BA9886D001F9168 /* Performance */ = {
|
||||
isa = PBXGroup;
|
||||
children = (
|
||||
56B7DEC83063F1B30018462B /* StatementPreparationPerformanceTests.swift */,
|
||||
56BB862D1BA98933001F9168 /* GRDBPerformanceComparisonTests-Bridging.h */,
|
||||
56DE7B2D1C42B23B00861EB8 /* PerformanceModel.xcdatamodeld */,
|
||||
56A6F4AB29BBC8E200E22662 /* ArgumentsTests.swift */,
|
||||
@@ -338,6 +342,7 @@
|
||||
isa = PBXSourcesBuildPhase;
|
||||
buildActionMask = 2147483647;
|
||||
files = (
|
||||
56B7DECA3063F1B50018462B /* StatementPreparationPerformanceTests.swift in Sources */,
|
||||
56D3BE711F4EB1A00034C6D2 /* FetchRecordStructTests.swift in Sources */,
|
||||
565BC5E82517645D00D2B53E /* Generation.swift in Sources */,
|
||||
560C98231C0E23BB00BF8471 /* InsertRecordClassTests.swift in Sources */,
|
||||
@@ -365,6 +370,7 @@
|
||||
565BC5E92517645D00D2B53E /* Generation.swift in Sources */,
|
||||
56439B361F4CA1DC0066043F /* FetchRecordClassTests.swift in Sources */,
|
||||
56439B381F4CA1DC0066043F /* FetchNamedValuesTests.swift in Sources */,
|
||||
56B7DECB3063F1B50018462B /* StatementPreparationPerformanceTests.swift in Sources */,
|
||||
56439B391F4CA1DC0066043F /* InsertPositionalValuesTests.swift in Sources */,
|
||||
56D507841F6D7B2F00AE1C5B /* InsertRecordStructTests.swift in Sources */,
|
||||
5690AFD92120589A001530EA /* InsertRecordEncodableTests.swift in Sources */,
|
||||
|
||||
@@ -0,0 +1,75 @@
|
||||
import XCTest
|
||||
import GRDB
|
||||
|
||||
// Inspired by <https://github.com/groue/GRDB.swift/pull/1873>
|
||||
class StatementPreparationPerformanceTests: XCTestCase {
|
||||
let iterationCount = 10_000
|
||||
|
||||
func testStatementPreparationPerformance_wide() throws {
|
||||
let dbQueue = try makeTestDatabase()
|
||||
dbQueue.inDatabase { db in
|
||||
measure {
|
||||
for _ in 0..<iterationCount {
|
||||
_ = try! db.makeStatement(sql: "SELECT * FROM wide")
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
func testStatementPreparationPerformance_join() throws {
|
||||
let dbQueue = try makeTestDatabase()
|
||||
dbQueue.inDatabase { db in
|
||||
measure {
|
||||
for _ in 0..<iterationCount {
|
||||
_ = try! db.makeStatement(sql: """
|
||||
SELECT author.*, book.*, review.*
|
||||
FROM author
|
||||
JOIN book ON book.authorId = author.id
|
||||
JOIN review ON review.bookId = book.id
|
||||
""")
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
func testStatementPreparationPerformance_narrow() throws {
|
||||
let dbQueue = try makeTestDatabase()
|
||||
dbQueue.inDatabase { db in
|
||||
measure {
|
||||
for _ in 0..<iterationCount {
|
||||
_ = try! db.makeStatement(sql: "SELECT id FROM wide")
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
private func makeTestDatabase() throws -> DatabaseQueue {
|
||||
let dbQueue = try DatabaseQueue()
|
||||
try dbQueue.write { db in
|
||||
try db.execute(sql: createTableSQL(
|
||||
table: "wide",
|
||||
columns: ["id INTEGER PRIMARY KEY"] + integerColumns(count: 119)))
|
||||
try db.execute(sql: createTableSQL(
|
||||
table: "author",
|
||||
columns: ["id INTEGER PRIMARY KEY"] + integerColumns(count: 39)))
|
||||
try db.execute(sql: createTableSQL(
|
||||
table: "book",
|
||||
columns: ["id INTEGER PRIMARY KEY", "authorId INTEGER"]
|
||||
+ integerColumns(count: 38)))
|
||||
try db.execute(sql: createTableSQL(
|
||||
table: "review",
|
||||
columns: ["id INTEGER PRIMARY KEY", "bookId INTEGER"]
|
||||
+ integerColumns(count: 38)))
|
||||
}
|
||||
|
||||
return dbQueue
|
||||
}
|
||||
|
||||
private func createTableSQL(table: String, columns: [String]) -> String {
|
||||
"CREATE TABLE \(table) (\(columns.joined(separator: ", ")))"
|
||||
}
|
||||
|
||||
private func integerColumns(count: Int) -> [String] {
|
||||
(1...count).map { "column\($0) INTEGER" }
|
||||
}
|
||||
}
|
||||
Reference in new issue
Block a user