diff --git a/CHANGELOG.md b/CHANGELOG.md index 85ee7d253..00aa23373 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -62,6 +62,7 @@ GRDB adheres to [Semantic Versioning](https://semver.org/), with one exception: ## Next Release - [#683](https://github.com/groue/GRDB.swift/pull/683): String concatenation +- [#685](https://github.com/groue/GRDB.swift/pull/685) by [@gjeck](https://github.com/gjeck): Add cache for TableRecord.defaultDatabaseTableName ## 4.8.1 diff --git a/GRDB/Record/TableRecord.swift b/GRDB/Record/TableRecord.swift index d7ea7513d..5871b2300 100644 --- a/GRDB/Record/TableRecord.swift +++ b/GRDB/Record/TableRecord.swift @@ -1,3 +1,5 @@ +import Foundation + /// Types that adopt TableRecord declare a particular relationship with /// a database table. /// @@ -59,18 +61,24 @@ extension TableRecord { /// - HTTPRequest -> "httpRequest" /// - TOEFL -> "toefl" internal static var defaultDatabaseTableName: String { + if let cached = defaultDatabaseTableNameCache.object(forKey: "\(Self.self)" as NSString) { + return cached as String + } let typeName = "\(Self.self)".replacingOccurrences(of: "(.)\\b.*$", with: "$1", options: [.regularExpression]) let initial = typeName.replacingOccurrences(of: "^([A-Z]+).*$", with: "$1", options: [.regularExpression]) + let tableName: String switch initial.count { case typeName.count: - return initial.lowercased() + tableName = initial.lowercased() case 0: - return typeName + tableName = typeName case 1: - return initial.lowercased() + typeName.dropFirst() + tableName = initial.lowercased() + typeName.dropFirst() default: - return initial.dropLast().lowercased() + typeName.dropFirst(initial.count - 1) + tableName = initial.dropLast().lowercased() + typeName.dropFirst(initial.count - 1) } + defaultDatabaseTableNameCache.setObject(tableName as NSString, forKey: "\(Self.self)" as NSString) + return tableName } /// The default name of the database table used to build requests. @@ -153,3 +161,8 @@ extension TableRecord { .reduce(0, +) } } + +/// Calculating `defaultDatabaseTableName` is somewhat expensive due to the regular expression evaluation +/// +/// This cache mitigates the cost of the calculation by storing the name for later retrieval +private let defaultDatabaseTableNameCache = NSCache() diff --git a/README.md b/README.md index d8944f60e..53ad82bed 100644 --- a/README.md +++ b/README.md @@ -8185,7 +8185,7 @@ Sample Code **Thanks** - [Pierlis](http://pierlis.com), where we write great software. -- [@alextrob](https://github.com/alextrob), [@bellebethcooper](https://github.com/bellebethcooper), [@bfad](https://github.com/bfad), [@cfilipov](https://github.com/cfilipov), [@charlesmchen-signal](https://github.com/charlesmchen-signal), [@Chiliec](https://github.com/Chiliec), [@chrisballinger](https://github.com/chrisballinger), [@darrenclark](https://github.com/darrenclark), [@davidkraus](https://github.com/davidkraus), [@fpillet](http://github.com/fpillet), [@gusrota](https://github.com/gusrota), [@hartbit](https://github.com/hartbit), [@kdubb](https://github.com/kdubb), [@kluufger](https://github.com/kluufger), [@KyleLeneau](https://github.com/KyleLeneau), [@Marus](https://github.com/Marus), [@MaxDesiatov](https://github.com/MaxDesiatov), [@michaelkirk-signal](https://github.com/michaelkirk-signal), [@pakko972](https://github.com/pakko972), [@peter-ss](https://github.com/peter-ss), [@pierlo](https://github.com/pierlo), [@pocketpixels](https://github.com/pocketpixels), [@robcas3](https://github.com/robcas3), [@runhum](https://github.com/runhum), [@schveiguy](https://github.com/schveiguy), [@SD10](https://github.com/SD10), [@sobri909](https://github.com/sobri909), [@sroddy](https://github.com/sroddy), [@swiftlyfalling](https://github.com/swiftlyfalling), [@Timac](https://github.com/Timac), [@valexa](https://github.com/valexa), and [@zmeyc](https://github.com/zmeyc) for their contributions, help, and feedback on GRDB. +- [@alextrob](https://github.com/alextrob), [@bellebethcooper](https://github.com/bellebethcooper), [@bfad](https://github.com/bfad), [@cfilipov](https://github.com/cfilipov), [@charlesmchen-signal](https://github.com/charlesmchen-signal), [@Chiliec](https://github.com/Chiliec), [@chrisballinger](https://github.com/chrisballinger), [@darrenclark](https://github.com/darrenclark), [@davidkraus](https://github.com/davidkraus), [@fpillet](http://github.com/fpillet), [@gjeck](https://github.com/gjeck), [@gusrota](https://github.com/gusrota), [@hartbit](https://github.com/hartbit), [@kdubb](https://github.com/kdubb), [@kluufger](https://github.com/kluufger), [@KyleLeneau](https://github.com/KyleLeneau), [@Marus](https://github.com/Marus), [@MaxDesiatov](https://github.com/MaxDesiatov), [@michaelkirk-signal](https://github.com/michaelkirk-signal), [@pakko972](https://github.com/pakko972), [@peter-ss](https://github.com/peter-ss), [@pierlo](https://github.com/pierlo), [@pocketpixels](https://github.com/pocketpixels), [@robcas3](https://github.com/robcas3), [@runhum](https://github.com/runhum), [@schveiguy](https://github.com/schveiguy), [@SD10](https://github.com/SD10), [@sobri909](https://github.com/sobri909), [@sroddy](https://github.com/sroddy), [@swiftlyfalling](https://github.com/swiftlyfalling), [@Timac](https://github.com/Timac), [@valexa](https://github.com/valexa), and [@zmeyc](https://github.com/zmeyc) for their contributions, help, and feedback on GRDB. - [@aymerick](https://github.com/aymerick) and [@kali](https://github.com/kali) because SQL. - [ccgus/fmdb](https://github.com/ccgus/fmdb) for its excellency.