mirror of
https://github.com/groue/GRDB.swift
synced 2026-11-27 04:14:21 +00:00
Improve performance by removing useless retain/release
This commit is contained in:
1 parent
8bc52b29e7
commit
f00dda7f43
3 files changed
+71
-33
No files matched your search
+40
-18
@@ -7,12 +7,14 @@ public final class Row {
|
||||
|
||||
/// Builds an empty row.
|
||||
public init() {
|
||||
statementRef = nil
|
||||
sqliteStatement = nil
|
||||
impl = EmptyRowImpl()
|
||||
}
|
||||
|
||||
/// Builds a row from a dictionary of values.
|
||||
public init(_ dictionary: [String: DatabaseValueConvertible?]) {
|
||||
statementRef = nil
|
||||
sqliteStatement = nil
|
||||
impl = DictionaryRowImpl(dictionary: dictionary)
|
||||
}
|
||||
@@ -27,10 +29,15 @@ public final class Row {
|
||||
return impl.copy(self)
|
||||
}
|
||||
|
||||
deinit {
|
||||
statementRef?.release()
|
||||
}
|
||||
|
||||
|
||||
// MARK: - Not Public
|
||||
|
||||
let impl: RowImpl
|
||||
let statementRef: Unmanaged<SelectStatement>?
|
||||
let sqliteStatement: SQLiteStatement
|
||||
|
||||
/// Builds a row from the an SQLite statement.
|
||||
@@ -39,8 +46,10 @@ public final class Row {
|
||||
/// access to the SQLite statement. Iteration of the statement does modify
|
||||
/// the row.
|
||||
init(statement: SelectStatement) {
|
||||
let statementRef = Unmanaged.passRetained(statement)
|
||||
self.statementRef = statementRef
|
||||
self.sqliteStatement = statement.sqliteStatement
|
||||
self.impl = StatementRowImpl(statement: statement)
|
||||
self.impl = StatementRowImpl(statementRef: statementRef, sqliteStatement: statement.sqliteStatement)
|
||||
}
|
||||
|
||||
/// Builds a row from the *current state* of the SQLite statement.
|
||||
@@ -48,9 +57,10 @@ public final class Row {
|
||||
/// The row is implemented on top of StatementCopyRowImpl, which *copies*
|
||||
/// the values from the SQLite statement so that further iteration of the
|
||||
/// statement does not modify the row.
|
||||
init(copiedFromStatement statement: SelectStatement) {
|
||||
init(copiedFromStatementRef statementRef: Unmanaged<SelectStatement>, sqliteStatement: SQLiteStatement) {
|
||||
self.statementRef = nil
|
||||
self.sqliteStatement = nil
|
||||
self.impl = StatementCopyRowImpl(statement: statement)
|
||||
self.impl = StatementCopyRowImpl(statementRef: statementRef, sqliteStatement: sqliteStatement)
|
||||
}
|
||||
}
|
||||
|
||||
@@ -437,7 +447,9 @@ extension Row {
|
||||
public static func fetch(statement: SelectStatement, arguments: StatementArguments? = nil) -> DatabaseSequence<Row> {
|
||||
// Metal rows can be reused. And reusing them yields better performance.
|
||||
let row = Row(statement: statement)
|
||||
return statement.fetchSequence(arguments: arguments) { row }
|
||||
return statement.fetchSequence(arguments: arguments, retaining: row) { rowRef in
|
||||
return Unmanaged<Row>.fromOpaque(unsafeUnwrap(rowRef).toOpaque()).takeUnretainedValue()
|
||||
}
|
||||
}
|
||||
|
||||
/// Returns an array of rows fetched from a prepared statement.
|
||||
@@ -451,8 +463,10 @@ extension Row {
|
||||
/// - returns: An array of rows.
|
||||
@warn_unused_result
|
||||
public static func fetchAll(statement: SelectStatement, arguments: StatementArguments? = nil) -> [Row] {
|
||||
let sqliteStatement = statement.sqliteStatement
|
||||
let statementRef = Unmanaged.passUnretained(statement)
|
||||
let sequence = statement.fetchSequence(arguments: arguments) {
|
||||
Row(copiedFromStatement: statement)
|
||||
Row(copiedFromStatementRef: statementRef, sqliteStatement: sqliteStatement)
|
||||
}
|
||||
return Array(sequence)
|
||||
}
|
||||
@@ -468,8 +482,10 @@ extension Row {
|
||||
/// - returns: An optional row.
|
||||
@warn_unused_result
|
||||
public static func fetchOne(statement: SelectStatement, arguments: StatementArguments? = nil) -> Row? {
|
||||
let sqliteStatement = statement.sqliteStatement
|
||||
let statementRef = Unmanaged.passUnretained(statement)
|
||||
let sequence = statement.fetchSequence(arguments: arguments) {
|
||||
Row(copiedFromStatement: statement)
|
||||
Row(copiedFromStatementRef: statementRef, sqliteStatement: sqliteStatement)
|
||||
}
|
||||
return sequence.generate().next()
|
||||
}
|
||||
@@ -682,15 +698,15 @@ private struct DictionaryRowImpl : RowImpl {
|
||||
}
|
||||
|
||||
|
||||
/// See Row.init(copiedFromStatement:)
|
||||
/// See Row.init(copiedFromStatementRef:sqliteStatement:)
|
||||
private struct StatementCopyRowImpl : RowImpl {
|
||||
let databaseValues: [DatabaseValue]
|
||||
let columnNames: [String]
|
||||
|
||||
init(statement: SelectStatement) {
|
||||
let sqliteStatement = statement.sqliteStatement
|
||||
self.databaseValues = (0..<Int32(statement.columnCount)).map { DatabaseValue(sqliteStatement: sqliteStatement, index: $0) }
|
||||
self.columnNames = statement.columnNames
|
||||
init(statementRef: Unmanaged<SelectStatement>, sqliteStatement: SQLiteStatement) {
|
||||
let sqliteStatement = sqliteStatement
|
||||
self.databaseValues = (0..<sqlite3_column_count(sqliteStatement)).map { DatabaseValue(sqliteStatement: sqliteStatement, index: $0) }
|
||||
self.columnNames = statementRef.takeUnretainedValue().columnNames
|
||||
}
|
||||
|
||||
var count: Int {
|
||||
@@ -724,12 +740,15 @@ private struct StatementCopyRowImpl : RowImpl {
|
||||
|
||||
/// See Row.init(statement:)
|
||||
private struct StatementRowImpl : RowImpl {
|
||||
let statement: SelectStatement
|
||||
let statementRef: Unmanaged<SelectStatement>
|
||||
let sqliteStatement: SQLiteStatement
|
||||
let lowercaseColumnIndexes: [String: Int]
|
||||
|
||||
init(statement: SelectStatement) {
|
||||
self.statement = statement
|
||||
self.sqliteStatement = statement.sqliteStatement
|
||||
init(statementRef: Unmanaged<SelectStatement>, sqliteStatement: SQLiteStatement) {
|
||||
self.statementRef = statementRef
|
||||
self.sqliteStatement = sqliteStatement
|
||||
let lowercaseColumnNames = (0..<sqlite3_column_count(sqliteStatement)).map { String.fromCString(sqlite3_column_name(sqliteStatement, Int32($0)))!.lowercaseString }
|
||||
self.lowercaseColumnIndexes = Dictionary(lowercaseColumnNames.enumerate().map { ($1, $0) }.reverse())
|
||||
}
|
||||
|
||||
var count: Int {
|
||||
@@ -750,17 +769,20 @@ private struct StatementRowImpl : RowImpl {
|
||||
}
|
||||
|
||||
func columnName(atIndex index: Int) -> String {
|
||||
return statement.columnNames[index]
|
||||
return statementRef.takeUnretainedValue().columnNames[index]
|
||||
}
|
||||
|
||||
// This method MUST be case-insensitive, and returns the index of the
|
||||
// leftmost column that matches *name*.
|
||||
func indexOfColumn(named name: String) -> Int? {
|
||||
return statement.indexOfColumn(named: name)
|
||||
if let index = lowercaseColumnIndexes[name] {
|
||||
return index
|
||||
}
|
||||
return lowercaseColumnIndexes[name.lowercaseString]
|
||||
}
|
||||
|
||||
func copy(row: Row) -> Row {
|
||||
return Row(copiedFromStatement: statement)
|
||||
return Row(copiedFromStatementRef: statementRef, sqliteStatement: sqliteStatement)
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
+29
-14
@@ -282,7 +282,8 @@ public final class SelectStatement : Statement {
|
||||
|
||||
/// The column names, ordered from left to right.
|
||||
public lazy var columnNames: [String] = {
|
||||
(0..<self.columnCount).map { String.fromCString(sqlite3_column_name(self.sqliteStatement, Int32($0)))! }
|
||||
let sqliteStatement = self.sqliteStatement
|
||||
return (0..<self.columnCount).map { String.fromCString(sqlite3_column_name(sqliteStatement, Int32($0)))! }
|
||||
}()
|
||||
|
||||
/// Cache for indexOfColumn(). Keys are lowercase.
|
||||
@@ -296,21 +297,29 @@ public final class SelectStatement : Statement {
|
||||
return columnIndexes[name.lowercaseString]
|
||||
}
|
||||
|
||||
/// The DatabaseSequence builder.
|
||||
/// Creates a DatabaseSequence
|
||||
@warn_unused_result
|
||||
func fetchSequence<T>(arguments arguments: StatementArguments?, element: () -> T) -> DatabaseSequence<T> {
|
||||
func fetchSequence<Element>(arguments arguments: StatementArguments?, element: () -> Element) -> DatabaseSequence<Element> {
|
||||
// Force arguments validity. See UpdateStatement.execute(), and Database.execute()
|
||||
try! prepareWithArguments(arguments)
|
||||
return DatabaseSequence(statement: self, element: element)
|
||||
return DatabaseSequence(statement: self, retainedRource: nil) { _ in element() }
|
||||
}
|
||||
|
||||
/// Creates a DatabaseSequence
|
||||
@warn_unused_result
|
||||
func fetchSequence<Element>(arguments arguments: StatementArguments?, retaining resource: AnyObject, element: (Unmanaged<AnyObject>?) -> Element) -> DatabaseSequence<Element> {
|
||||
// Force arguments validity. See UpdateStatement.execute(), and Database.execute()
|
||||
try! prepareWithArguments(arguments)
|
||||
return DatabaseSequence(statement: self, retainedRource: resource, element: element)
|
||||
}
|
||||
}
|
||||
|
||||
/// A sequence of elements fetched from the database.
|
||||
public struct DatabaseSequence<T>: SequenceType {
|
||||
private let generateImpl: () throws -> DatabaseGenerator<T>
|
||||
public struct DatabaseSequence<Element>: SequenceType {
|
||||
private let generateImpl: () throws -> DatabaseGenerator<Element>
|
||||
|
||||
// Statement sequence
|
||||
private init(statement: SelectStatement, element: () -> T) {
|
||||
private init(statement: SelectStatement, retainedRource: AnyObject?, element: (Unmanaged<AnyObject>?) -> Element) {
|
||||
self.generateImpl = {
|
||||
// Check that generator is built on a valid queue.
|
||||
statement.database.preconditionValidQueue()
|
||||
@@ -318,12 +327,14 @@ public struct DatabaseSequence<T>: SequenceType {
|
||||
// Support multiple sequence iterations
|
||||
try statement.reset()
|
||||
|
||||
return DatabaseGenerator(statementRef: Unmanaged.passRetained(statement)) { (sqliteStatement: SQLiteStatement, statementRef: Unmanaged<SelectStatement>) -> T? in
|
||||
let statementRef = Unmanaged.passRetained(statement)
|
||||
let resourceRef = retainedRource.flatMap { Unmanaged.passRetained($0) }
|
||||
return DatabaseGenerator(statementRef: statementRef, resourceRef: resourceRef) { (sqliteStatement, statementRef, resourceRef) in
|
||||
switch sqlite3_step(sqliteStatement) {
|
||||
case SQLITE_DONE:
|
||||
return nil
|
||||
case SQLITE_ROW:
|
||||
return element()
|
||||
return element(resourceRef)
|
||||
case let errorCode:
|
||||
let statement = statementRef.takeUnretainedValue()
|
||||
try! { throw DatabaseError(code: errorCode, message: statement.database.lastErrorMessage, sql: statement.sql, arguments: statement.arguments) }()
|
||||
@@ -344,13 +355,13 @@ public struct DatabaseSequence<T>: SequenceType {
|
||||
}
|
||||
}
|
||||
|
||||
private init(generateImpl: () throws -> DatabaseGenerator<T>) {
|
||||
private init(generateImpl: () throws -> DatabaseGenerator<Element>) {
|
||||
self.generateImpl = generateImpl
|
||||
}
|
||||
|
||||
/// Return a *generator* over the elements of this *sequence*.
|
||||
@warn_unused_result
|
||||
public func generate() -> DatabaseGenerator<T> {
|
||||
public func generate() -> DatabaseGenerator<Element> {
|
||||
return try! generateImpl()
|
||||
}
|
||||
}
|
||||
@@ -358,23 +369,27 @@ public struct DatabaseSequence<T>: SequenceType {
|
||||
/// A generator of elements fetched from the database.
|
||||
public class DatabaseGenerator<Element>: GeneratorType {
|
||||
private let statementRef: Unmanaged<SelectStatement>?
|
||||
private let resourceRef: Unmanaged<AnyObject>?
|
||||
private let sqliteStatement: SQLiteStatement
|
||||
private let element: ((SQLiteStatement, Unmanaged<SelectStatement>) -> Element?)?
|
||||
private let element: ((SQLiteStatement, Unmanaged<SelectStatement>, Unmanaged<AnyObject>?) -> Element?)?
|
||||
|
||||
init(statementRef: Unmanaged<SelectStatement>, element: (SQLiteStatement, Unmanaged<SelectStatement>) -> Element?) {
|
||||
init(statementRef: Unmanaged<SelectStatement>, resourceRef: Unmanaged<AnyObject>?, element: (SQLiteStatement, Unmanaged<SelectStatement>, Unmanaged<AnyObject>?) -> Element?) {
|
||||
self.statementRef = statementRef
|
||||
self.resourceRef = resourceRef
|
||||
self.sqliteStatement = statementRef.takeUnretainedValue().sqliteStatement
|
||||
self.element = element
|
||||
}
|
||||
|
||||
init() {
|
||||
self.statementRef = nil
|
||||
self.resourceRef = nil
|
||||
self.sqliteStatement = nil
|
||||
self.element = nil
|
||||
}
|
||||
|
||||
deinit {
|
||||
statementRef?.release()
|
||||
resourceRef?.release()
|
||||
}
|
||||
|
||||
@warn_unused_result
|
||||
@@ -382,7 +397,7 @@ public class DatabaseGenerator<Element>: GeneratorType {
|
||||
guard let element = element else {
|
||||
return nil
|
||||
}
|
||||
return element(sqliteStatement, unsafeUnwrap(statementRef))
|
||||
return element(sqliteStatement, unsafeUnwrap(statementRef), resourceRef)
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
@@ -68,7 +68,8 @@ extension RowConvertible {
|
||||
public static func fetch(statement: SelectStatement, arguments: StatementArguments? = nil) -> DatabaseSequence<Self> {
|
||||
let row = Row(statement: statement)
|
||||
let database = statement.database
|
||||
return statement.fetchSequence(arguments: arguments) {
|
||||
return statement.fetchSequence(arguments: arguments, retaining: row) { rowRef in
|
||||
let row = Unmanaged<Row>.fromOpaque(unsafeUnwrap(rowRef).toOpaque()).takeUnretainedValue()
|
||||
var value = self.init(row)
|
||||
value.awakeFromFetch(row: row, database: database)
|
||||
return value
|
||||
|
||||
Reference in new issue
Block a user