From 549f7ec66ee8253ad7b493ea2123c8defab56bae Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Gwendal=20Roue=CC=81?= Date: Fri, 14 Jan 2022 18:25:30 +0100 Subject: [PATCH] Cleanup and cursor profiling tests --- GRDB/Core/Statement.swift | 6 -- Tests/GRDBTests/DatabaseCursorTests.swift | 117 ++++++++++++++++++++++ 2 files changed, 117 insertions(+), 6 deletions(-) diff --git a/GRDB/Core/Statement.swift b/GRDB/Core/Statement.swift index 0c447ebe1..7edf22c25 100644 --- a/GRDB/Core/Statement.swift +++ b/GRDB/Core/Statement.swift @@ -293,7 +293,6 @@ public final class Statement { /// /// This method is unable to deal with statements that need a specific /// authorizer. See `forEachStep(_:)`. - @inline(__always) @usableFromInline func step(_ body: (SQLiteStatement) throws -> Element) throws -> Element? { // This check takes 0 time when profiled. It is, practically speaking, free. @@ -330,7 +329,6 @@ public final class Statement { /// 2. `Array(T.fetchCursor(...))` calls `Cursor.forEach(...)` /// 3. `DatabaseCursor.forEach(...)` calls `Statement.forEachStep(...)` /// 4. `Statement.forEachStep(...)` deals with the eventual authorizer. - @inline(__always) @usableFromInline func forEachStep(_ body: (SQLiteStatement) throws -> Void) throws { guard sqlite3_stmt_busy(sqliteStatement) == 0 else { @@ -348,7 +346,6 @@ public final class Statement { /// Implementation detail of `forEach(_:)`. /// Does not check for sqlite3_stmt_busy and eventual authorizer. - @inline(__always) @usableFromInline func uncheckedForEach(_ body: (SQLiteStatement) throws -> Void) throws { while true { @@ -421,7 +418,6 @@ public protocol DatabaseCursor: _DatabaseCursor { } extension DatabaseCursor { - @inline(__always) @inlinable public func next() throws -> Element? { if _isDone { @@ -437,7 +433,6 @@ extension DatabaseCursor { // Specific implementation of `forEach` in order to deal with // . // See `Statement.forEachStep(_:)` for more information. - @inline(__always) @inlinable public func forEach(_ body: (Element) throws -> Void) throws { try statement.forEachStep { try body(_element(sqliteStatement: $0)) } @@ -471,7 +466,6 @@ final class StatementCursor: DatabaseCursor { try? statement.reset() } - @inline(__always) @usableFromInline func _element(sqliteStatement: SQLiteStatement) throws { } } diff --git a/Tests/GRDBTests/DatabaseCursorTests.swift b/Tests/GRDBTests/DatabaseCursorTests.swift index c74f53e55..0eec5784c 100644 --- a/Tests/GRDBTests/DatabaseCursorTests.swift +++ b/Tests/GRDBTests/DatabaseCursorTests.swift @@ -166,4 +166,121 @@ class DatabaseCursorTests: GRDBTestCase { // sqlite3_close_v2(connection) // } } + + // For profiling tests + let profilingSQL = """ + WITH RECURSIVE + cnt(x) AS (VALUES(1) UNION ALL SELECT x+1 FROM cnt WHERE x<10000000) + SELECT x FROM cnt + """ + + // Profiling test + func testRowCursorStep() throws { + let dbQueue = try makeDatabaseQueue() + try dbQueue.read { db in + let cursor: RowCursor = try Row.fetchCursor(db, sql: profilingSQL) + while let _ = try cursor.next() { } + } + } + + // Profiling test + func testRowCursorForEach() throws { + let dbQueue = try makeDatabaseQueue() + try dbQueue.read { db in + let cursor: RowCursor = try Row.fetchCursor(db, sql: profilingSQL) + try cursor.forEach { _ in } + } + } + + // Profiling test + func testDatabaseValueCursorStep() throws { + let dbQueue = try makeDatabaseQueue() + try dbQueue.read { db in + let cursor: DatabaseValueCursor = try Int.fetchCursor(db, sql: profilingSQL) + while let _ = try cursor.next() { } + } + } + + // Profiling test + func testDatabaseValueCursorForEach() throws { + let dbQueue = try makeDatabaseQueue() + try dbQueue.read { db in + let cursor: DatabaseValueCursor = try Int.fetchCursor(db, sql: profilingSQL) + try cursor.forEach { _ in } + } + } + + // Profiling test + func testNullableDatabaseValueCursorStep() throws { + let dbQueue = try makeDatabaseQueue() + try dbQueue.read { db in + let cursor: NullableDatabaseValueCursor = try Optional.fetchCursor(db, sql: profilingSQL) + while let _ = try cursor.next() { } + } + } + + // Profiling test + func testNullableDatabaseValueCursorForEach() throws { + let dbQueue = try makeDatabaseQueue() + try dbQueue.read { db in + let cursor: NullableDatabaseValueCursor = try Optional.fetchCursor(db, sql: profilingSQL) + try cursor.forEach { _ in } + } + } + + // Profiling test + func testFastDatabaseValueCursorStep() throws { + let dbQueue = try makeDatabaseQueue() + try dbQueue.read { db in + let cursor: FastDatabaseValueCursor = try Int.fetchCursor(db, sql: profilingSQL) + while let _ = try cursor.next() { } + } + } + + // Profiling test + func testFastDatabaseValueCursorForEach() throws { + let dbQueue = try makeDatabaseQueue() + try dbQueue.read { db in + let cursor: FastDatabaseValueCursor = try Int.fetchCursor(db, sql: profilingSQL) + try cursor.forEach { _ in } + } + } + + // Profiling test + func testFastNullableDatabaseValueCursorStep() throws { + let dbQueue = try makeDatabaseQueue() + try dbQueue.read { db in + let cursor: FastNullableDatabaseValueCursor = try Optional.fetchCursor(db, sql: profilingSQL) + while let _ = try cursor.next() { } + } + } + + // Profiling test + func testFastNullableDatabaseValueCursorForEach() throws { + let dbQueue = try makeDatabaseQueue() + try dbQueue.read { db in + let cursor: FastNullableDatabaseValueCursor = try Optional.fetchCursor(db, sql: profilingSQL) + try cursor.forEach { _ in } + } + } + + // Profiling test + func testRecordCursorStep() throws { + struct S: FetchableRecord { init(row: Row) { } } + let dbQueue = try makeDatabaseQueue() + try dbQueue.read { db in + let cursor: RecordCursor = try S.fetchCursor(db, sql: profilingSQL) + while let _ = try cursor.next() { } + } + } + + // Profiling test + func testRecordCursorForEach() throws { + struct S: FetchableRecord { init(row: Row) { } } + let dbQueue = try makeDatabaseQueue() + try dbQueue.read { db in + let cursor: RecordCursor = try S.fetchCursor(db, sql: profilingSQL) + try cursor.forEach { _ in } + } + } }