From 151a45725107557e4e65bd8073337ed49c5ea524 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Gwendal=20Roue=CC=81?= Date: Sat, 28 Aug 2021 12:25:35 +0200 Subject: [PATCH] Inline StatementColumnConvertible decoding --- GRDB/Core/Row.swift | 6 ++++++ GRDB/Core/StatementColumnConvertible.swift | 4 ++++ .../Foundation/DatabaseDateComponents.swift | 2 ++ GRDB/Core/Support/Foundation/Date.swift | 3 +++ GRDB/Core/Support/Foundation/Decimal.swift | 9 ++++++--- .../Support/Foundation/SQLiteDateParser.swift | 7 ++++++- GRDB/Core/Support/Foundation/UUID.swift | 2 ++ .../StandardLibrary/StandardLibrary.swift | 18 ++++++++++++++++++ GRDB/Utils/Utils.swift | 1 + 9 files changed, 48 insertions(+), 4 deletions(-) diff --git a/GRDB/Core/Row.swift b/GRDB/Core/Row.swift index 57aad090f..b223bb713 100644 --- a/GRDB/Core/Row.swift +++ b/GRDB/Core/Row.swift @@ -166,6 +166,7 @@ extension Row { // MARK: - Extracting Values /// Fatal errors if index is out of bounds + @inline(__always) @usableFromInline /* private */ func _checkedIndex(_ index: Int, file: StaticString = #file, line: UInt = #line) -> Int { GRDBPrecondition(index >= 0 && index < count, "row index out of range", file: file, line: line) @@ -241,6 +242,7 @@ extension Row { /// This method exists as an optimization opportunity for types that adopt /// StatementColumnConvertible. It *may* trigger SQLite built-in conversions /// (see ). + @inline(__always) @inlinable public subscript(_ index: Int) -> Value? { try! decodeIfPresent(Value.self, atIndex: index) @@ -269,6 +271,7 @@ extension Row { /// This method exists as an optimization opportunity for types that adopt /// StatementColumnConvertible. It *may* trigger SQLite built-in conversions /// (see ). + @inline(__always) @inlinable public subscript(_ index: Int) -> Value { try! decode(Value.self, atIndex: index) @@ -787,6 +790,7 @@ extension Row { /// This method exists as an optimization opportunity for types that adopt /// StatementColumnConvertible. It *may* trigger SQLite built-in conversions /// (see ). + @inline(__always) @inlinable func decodeIfPresent( _ type: Value.Type = Value.self, @@ -807,6 +811,7 @@ extension Row { /// This method exists as an optimization opportunity for types that adopt /// StatementColumnConvertible. It *may* trigger SQLite built-in conversions /// (see ). + @inline(__always) @inlinable func decode( _ type: Value.Type = Value.self, @@ -828,6 +833,7 @@ extension Row { /// This method exists as an optimization opportunity for types that adopt /// StatementColumnConvertible. It *may* trigger SQLite built-in conversions /// (see ). + @inline(__always) @inlinable func decodeIfPresent( _ type: Value.Type = Value.self, diff --git a/GRDB/Core/StatementColumnConvertible.swift b/GRDB/Core/StatementColumnConvertible.swift index cd95a98af..e579f71f4 100644 --- a/GRDB/Core/StatementColumnConvertible.swift +++ b/GRDB/Core/StatementColumnConvertible.swift @@ -45,6 +45,7 @@ public protocol StatementColumnConvertible { // MARK: - Conversions extension DatabaseValueConvertible where Self: StatementColumnConvertible { + @inline(__always) @inlinable static func fastDecode( fromStatement sqliteStatement: SQLiteStatement, @@ -64,6 +65,7 @@ extension DatabaseValueConvertible where Self: StatementColumnConvertible { return value } + @inline(__always) @inlinable static func fastDecode( fromRow row: Row, @@ -80,6 +82,7 @@ extension DatabaseValueConvertible where Self: StatementColumnConvertible { return try row.fastDecode(Self.self, atUncheckedIndex: index) } + @inline(__always) @inlinable static func fastDecodeIfPresent( fromStatement sqliteStatement: SQLiteStatement, @@ -100,6 +103,7 @@ extension DatabaseValueConvertible where Self: StatementColumnConvertible { return value } + @inline(__always) @inlinable static func fastDecodeIfPresent( fromRow row: Row, diff --git a/GRDB/Core/Support/Foundation/DatabaseDateComponents.swift b/GRDB/Core/Support/Foundation/DatabaseDateComponents.swift index 57f930d94..a8a634ccf 100644 --- a/GRDB/Core/Support/Foundation/DatabaseDateComponents.swift +++ b/GRDB/Core/Support/Foundation/DatabaseDateComponents.swift @@ -69,6 +69,8 @@ public struct DatabaseDateComponents: DatabaseValueConvertible, StatementColumnC /// - parameters: /// - sqliteStatement: A pointer to an SQLite statement. /// - index: The column index. + @inline(__always) + @inlinable public init?(sqliteStatement: SQLiteStatement, index: Int32) { guard let cString = sqlite3_column_text(sqliteStatement, index) else { return nil diff --git a/GRDB/Core/Support/Foundation/Date.swift b/GRDB/Core/Support/Foundation/Date.swift index d97052197..505632efe 100644 --- a/GRDB/Core/Support/Foundation/Date.swift +++ b/GRDB/Core/Support/Foundation/Date.swift @@ -58,6 +58,7 @@ extension Date: DatabaseValueConvertible { return nil } + @usableFromInline init?(databaseDateComponents: DatabaseDateComponents) { guard databaseDateComponents.format.hasYMDComponents else { // Refuse to turn hours without any date information into Date: @@ -117,6 +118,8 @@ extension Date: StatementColumnConvertible { /// - parameters: /// - sqliteStatement: A pointer to an SQLite statement. /// - index: The column index. + @inline(__always) + @inlinable public init?(sqliteStatement: SQLiteStatement, index: Int32) { switch sqlite3_column_type(sqliteStatement, index) { case SQLITE_INTEGER, SQLITE_FLOAT: diff --git a/GRDB/Core/Support/Foundation/Decimal.swift b/GRDB/Core/Support/Foundation/Decimal.swift index dd89cc73f..56f1533b8 100644 --- a/GRDB/Core/Support/Foundation/Decimal.swift +++ b/GRDB/Core/Support/Foundation/Decimal.swift @@ -17,7 +17,7 @@ extension Decimal: DatabaseValueConvertible { return self.init(double) case let .string(string): // Must match NSNumber.fromDatabaseValue(_:) - return self.init(string: string, locale: posixLocale) + return self.init(string: string, locale: _posixLocale) default: return nil } @@ -26,6 +26,8 @@ extension Decimal: DatabaseValueConvertible { /// Decimal adopts StatementColumnConvertible extension Decimal: StatementColumnConvertible { + @inline(__always) + @inlinable public init?(sqliteStatement: SQLiteStatement, index: Int32) { switch sqlite3_column_type(sqliteStatement, index) { case SQLITE_INTEGER: @@ -35,12 +37,13 @@ extension Decimal: StatementColumnConvertible { case SQLITE_TEXT: self.init( string: String(cString: sqlite3_column_text(sqliteStatement, index)!), - locale: posixLocale) + locale: _posixLocale) default: return nil } } } -private let posixLocale = Locale(identifier: "en_US_POSIX") +@usableFromInline +let _posixLocale = Locale(identifier: "en_US_POSIX") #endif diff --git a/GRDB/Core/Support/Foundation/SQLiteDateParser.swift b/GRDB/Core/Support/Foundation/SQLiteDateParser.swift index b98f288b3..74db8e5fd 100644 --- a/GRDB/Core/Support/Foundation/SQLiteDateParser.swift +++ b/GRDB/Core/Support/Foundation/SQLiteDateParser.swift @@ -2,13 +2,18 @@ import Foundation // inspired by: http://jordansmith.io/performant-date-parsing/ -final class SQLiteDateParser { +@usableFromInline +struct SQLiteDateParser { + @usableFromInline + init() { } + func components(from dateString: String) -> DatabaseDateComponents? { dateString.withCString { cString in components(cString: cString, length: strlen(cString)) } } + @usableFromInline func components(cString: UnsafePointer, length: Int) -> DatabaseDateComponents? { assert(strlen(cString) == length) diff --git a/GRDB/Core/Support/Foundation/UUID.swift b/GRDB/Core/Support/Foundation/UUID.swift index 0368b8817..3e88a7aa6 100644 --- a/GRDB/Core/Support/Foundation/UUID.swift +++ b/GRDB/Core/Support/Foundation/UUID.swift @@ -49,6 +49,8 @@ extension UUID: DatabaseValueConvertible { } extension UUID: StatementColumnConvertible { + @inline(__always) + @inlinable public init?(sqliteStatement: SQLiteStatement, index: Int32) { switch sqlite3_column_type(sqliteStatement, index) { case SQLITE_TEXT: diff --git a/GRDB/Core/Support/StandardLibrary/StandardLibrary.swift b/GRDB/Core/Support/StandardLibrary/StandardLibrary.swift index bbad16951..06c458829 100644 --- a/GRDB/Core/Support/StandardLibrary/StandardLibrary.swift +++ b/GRDB/Core/Support/StandardLibrary/StandardLibrary.swift @@ -98,6 +98,8 @@ extension Int: DatabaseValueConvertible, StatementColumnConvertible { /// - parameters: /// - sqliteStatement: A pointer to an SQLite statement. /// - index: The column index. + @inline(__always) + @inlinable public init?(sqliteStatement: SQLiteStatement, index: Int32) { let int64 = sqlite3_column_int64(sqliteStatement, index) guard let v = Int(exactly: int64) else { return nil } @@ -127,6 +129,8 @@ extension Int8: DatabaseValueConvertible, StatementColumnConvertible { /// - parameters: /// - sqliteStatement: A pointer to an SQLite statement. /// - index: The column index. + @inline(__always) + @inlinable public init?(sqliteStatement: SQLiteStatement, index: Int32) { let int64 = sqlite3_column_int64(sqliteStatement, index) guard let v = Int8(exactly: int64) else { return nil } @@ -156,6 +160,8 @@ extension Int16: DatabaseValueConvertible, StatementColumnConvertible { /// - parameters: /// - sqliteStatement: A pointer to an SQLite statement. /// - index: The column index. + @inline(__always) + @inlinable public init?(sqliteStatement: SQLiteStatement, index: Int32) { let int64 = sqlite3_column_int64(sqliteStatement, index) guard let v = Int16(exactly: int64) else { return nil } @@ -185,6 +191,8 @@ extension Int32: DatabaseValueConvertible, StatementColumnConvertible { /// - parameters: /// - sqliteStatement: A pointer to an SQLite statement. /// - index: The column index. + @inline(__always) + @inlinable public init?(sqliteStatement: SQLiteStatement, index: Int32) { let int64 = sqlite3_column_int64(sqliteStatement, index) guard let v = Int32(exactly: int64) else { return nil } @@ -250,6 +258,8 @@ extension UInt: DatabaseValueConvertible, StatementColumnConvertible { /// - parameters: /// - sqliteStatement: A pointer to an SQLite statement. /// - index: The column index. + @inline(__always) + @inlinable public init?(sqliteStatement: SQLiteStatement, index: Int32) { let int64 = sqlite3_column_int64(sqliteStatement, index) guard let v = UInt(exactly: int64) else { return nil } @@ -279,6 +289,8 @@ extension UInt8: DatabaseValueConvertible, StatementColumnConvertible { /// - parameters: /// - sqliteStatement: A pointer to an SQLite statement. /// - index: The column index. + @inline(__always) + @inlinable public init?(sqliteStatement: SQLiteStatement, index: Int32) { let int64 = sqlite3_column_int64(sqliteStatement, index) guard let v = UInt8(exactly: int64) else { return nil } @@ -308,6 +320,8 @@ extension UInt16: DatabaseValueConvertible, StatementColumnConvertible { /// - parameters: /// - sqliteStatement: A pointer to an SQLite statement. /// - index: The column index. + @inline(__always) + @inlinable public init?(sqliteStatement: SQLiteStatement, index: Int32) { let int64 = sqlite3_column_int64(sqliteStatement, index) guard let v = UInt16(exactly: int64) else { return nil } @@ -337,6 +351,8 @@ extension UInt32: DatabaseValueConvertible, StatementColumnConvertible { /// - parameters: /// - sqliteStatement: A pointer to an SQLite statement. /// - index: The column index. + @inline(__always) + @inlinable public init?(sqliteStatement: SQLiteStatement, index: Int32) { let int64 = sqlite3_column_int64(sqliteStatement, index) guard let v = UInt32(exactly: int64) else { return nil } @@ -366,6 +382,8 @@ extension UInt64: DatabaseValueConvertible, StatementColumnConvertible { /// - parameters: /// - sqliteStatement: A pointer to an SQLite statement. /// - index: The column index. + @inline(__always) + @inlinable public init?(sqliteStatement: SQLiteStatement, index: Int32) { let int64 = sqlite3_column_int64(sqliteStatement, index) guard let v = UInt64(exactly: int64) else { return nil } diff --git a/GRDB/Utils/Utils.swift b/GRDB/Utils/Utils.swift index 615e28032..d6ed09d4c 100644 --- a/GRDB/Utils/Utils.swift +++ b/GRDB/Utils/Utils.swift @@ -36,6 +36,7 @@ extension Optional: _OptionalProtocol { } // MARK: - Internal /// Reserved for GRDB: do not use. +@inlinable func GRDBPrecondition( _ condition: @autoclosure() -> Bool, _ message: @autoclosure() -> String = "",