From d5a41bd11bc81d1db38bab120545376a07bf6efb Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Gwendal=20Roue=CC=81?= Date: Mon, 13 Mar 2023 19:32:30 +0100 Subject: [PATCH 1/5] Disable SQLITE_STATIC optimization when there are too many arguments --- CHANGELOG.md | 4 ++ GRDB/Core/Statement.swift | 30 +++++++--- .../Documentation.docc/Extension/Statement.md | 2 +- Tests/GRDBTests/UpdateStatementTests.swift | 60 +++++++++++++++---- 4 files changed, 74 insertions(+), 22 deletions(-) diff --git a/CHANGELOG.md b/CHANGELOG.md index c74e5a3ae..47ff87645 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -107,6 +107,10 @@ GRDB adheres to [Semantic Versioning](https://semver.org/), with one exception: --- +## Next Release + +- **Fixed**: Avoid the risk for pathologically deep stack trace, or stack overflow, introduced by [#1348](https://github.com/groue/GRDB.swift/pull/1348). + ## 6.9.1 Released March 12, 2023 • [diff](https://github.com/groue/GRDB.swift/compare/v6.8.0...v6.9.1) diff --git a/GRDB/Core/Statement.swift b/GRDB/Core/Statement.swift index 31c48597a..dfa19320e 100644 --- a/GRDB/Core/Statement.swift +++ b/GRDB/Core/Statement.swift @@ -513,18 +513,19 @@ public final class Statement { /// /// When arguments are set at the moment of execution, with an non-nil /// `arguments` parameter, it is assumed that the statement won't be - /// reused with the same arguments. Execution is performed with - /// temporary SQLite bindings that avoid copying strings and blobs - /// arguments. + /// reused with the same arguments. When the number of arguments is + /// small, execution is performed with temporary SQLite bindings that + /// avoid copying strings and blobs arguments. /// /// For more information, see [`SQLITE_STATIC` and `SQLITE_TRANSIENT`](https://www.sqlite.org/c3ref/c_static.html). /// Compare: /// /// ```swift - /// // Uses SQLITE_STATIC + /// // Uses SQLITE_STATIC if there are few arguments, + /// // SQLITE_TRANSIENT otherwise. /// try statement.execute(arguments: ["Barbara"]) /// - /// // Uses SQLITE_TRANSIENT + /// // Always uses SQLITE_TRANSIENT /// try statement.setArguments(["Arthur"]) /// try statement.execute() /// ``` @@ -537,10 +538,21 @@ public final class Statement { /// - throws: A ``DatabaseError`` whenever an SQLite error occurs. public func execute(arguments: StatementArguments? = nil) throws { if let arguments { - // Assume that the statement won't be reused with the same - // arguments, and perform an optimized execution with temporary - // bindings in order to avoid copying strings and blobs. - try withArguments(arguments) { + // Assume that the statement won't be reused with the same arguments. + // + // Avoid a stack overflow, and don't perform an unbounded nesting + // of `withBinding(to:at:do:)` methods: only use temporary bindings + // for less than 20 arguments. This number 20 is completely + // arbitrary! + // See + if sqliteArgumentCount <= 20 { + // Perform an optimized execution with temporary bindings + // in order to avoid copying strings and blobs. + try withArguments(arguments) { + try executeAllSteps() + } + } else { + try setArguments(arguments) try executeAllSteps() } } else { diff --git a/GRDB/Documentation.docc/Extension/Statement.md b/GRDB/Documentation.docc/Extension/Statement.md index 0491f43db..5d6172f05 100644 --- a/GRDB/Documentation.docc/Extension/Statement.md +++ b/GRDB/Documentation.docc/Extension/Statement.md @@ -82,7 +82,7 @@ let player = try Player.fetchOne(selectStatement, arguments: ["Arthur"]) > Both perform exactly the same action, and most applications should not care about the difference. Yet: > > - ``setArguments(_:)`` performs a copy of string and blob arguments. It uses the low-level [`SQLITE_TRANSIENT`](https://www.sqlite.org/c3ref/c_static.html) option, and fits well the reuse of a given statement with the same arguments. -> - ``execute(arguments:)`` avoids a temporary allocation for string and blob arguments. It uses the low-level [`SQLITE_STATIC`](https://www.sqlite.org/c3ref/c_static.html) option, and fits well the reuse of a given statement with various arguments. +> - ``execute(arguments:)`` avoids a temporary allocation for string and blob arguments if the number of arguments is small. Instead of `SQLITE_TRANSIENT`, it uses the low-level [`SQLITE_STATIC`](https://www.sqlite.org/c3ref/c_static.html) option. This fits well the reuse of a given statement with various arguments. > > Don't make a blind choice, and monitor your app performance if it really matters! diff --git a/Tests/GRDBTests/UpdateStatementTests.swift b/Tests/GRDBTests/UpdateStatementTests.swift index 82f094c6d..20f1d28d1 100644 --- a/Tests/GRDBTests/UpdateStatementTests.swift +++ b/Tests/GRDBTests/UpdateStatementTests.swift @@ -442,8 +442,9 @@ class UpdateStatementTests : GRDBTestCase { } } - // To be compared with testTemporaryBindings() and testNonTemporaryBindings() - func testArgumentsReuse() throws { + // MARK: - SQLITE_STATIC vs SQLITE_TRANSIENT + + func test_SQLITE_STATIC_then_SQLITE_TRANSIENT() throws { let dbQueue = try makeDatabaseQueue() try dbQueue.write { db in try db.execute(literal: """ @@ -453,11 +454,11 @@ class UpdateStatementTests : GRDBTestCase { func test(value: some DatabaseValueConvertible) throws { defer { try! db.execute(sql: "DELETE FROM t") } - // Execute with temporary bindings + // Execute with temporary bindings (SQLITE_STATIC) let statement = try db.makeStatement(sql: "INSERT INTO t VALUES (?)") try statement.execute(arguments: [value]) - // Execute with non temporary bindings + // Execute with non temporary bindings (SQLITE_TRANSIENT) try statement.execute() // Since bindings are not temporary, they are not cleared, @@ -469,7 +470,7 @@ class UpdateStatementTests : GRDBTestCase { // Test that we have inserted the value thrice. try XCTAssertEqual( DatabaseValue.fetchSet(db, sql: "SELECT a FROM t"), - [value.databaseValue, value.databaseValue, value.databaseValue]) + [value.databaseValue]) } try test(value: "Foo") @@ -481,8 +482,7 @@ class UpdateStatementTests : GRDBTestCase { } } - // To be compared with testNonTemporaryBindings() and testArgumentsReuse() - func testTemporaryBindings() throws { + func test_SQLITE_STATIC() throws { let dbQueue = try makeDatabaseQueue() try dbQueue.write { db in try db.execute(literal: """ @@ -492,7 +492,7 @@ class UpdateStatementTests : GRDBTestCase { func test(value: some DatabaseValueConvertible) throws { defer { try! db.execute(sql: "DELETE FROM t") } - // Execute with temporary bindings + // Execute with temporary bindings (SQLITE_STATIC) let statement = try db.makeStatement(sql: "INSERT INTO t VALUES (?)") try statement.execute(arguments: [value]) @@ -516,8 +516,44 @@ class UpdateStatementTests : GRDBTestCase { } } - // To be compared with testTemporaryBindings() and testArgumentsReuse() - func testNonTemporaryBindings() throws { + func test_SQLITE_TRANSIENT_due_to_high_number_of_arguments() throws { + let dbQueue = try makeDatabaseQueue() + try dbQueue.write { db in + try db.execute(literal: """ + -- 21 columns + CREATE TABLE t(a,b,c,d,e,f,g,h,i,j,k,l,m,n,o,p,q,r,s,t,u); + """) + + func test(value: some DatabaseValueConvertible) throws { + defer { try! db.execute(sql: "DELETE FROM t") } + + // Execute with non temporary bindings (SQLITE_TRANSIENT), + // because there are more than 20 arguments + let statement = try db.makeStatement(sql: "INSERT INTO t VALUES (\(databaseQuestionMarks(count: 21)))") + try statement.execute(arguments: StatementArguments(Array(repeating: value, count: 21))) + + // Since bindings are not temporary, they are not cleared, + // so insert the value again. + sqlite3_reset(statement.sqliteStatement) + sqlite3_step(statement.sqliteStatement) + sqlite3_reset(statement.sqliteStatement) + + // Test that we have inserted the value twice. + try XCTAssertEqual( + DatabaseValue.fetchSet(db, sql: "SELECT a FROM t"), + [value.databaseValue]) + } + + try test(value: "Foo") + try test(value: "") + try test(value: "Hello".data(using: .utf8)!) + try test(value: Data()) + try test(value: 42) + try test(value: 1.23) + } + } + + func test_SQLITE_TRANSIENT() throws { let dbQueue = try makeDatabaseQueue() try dbQueue.write { db in try db.execute(literal: """ @@ -527,7 +563,7 @@ class UpdateStatementTests : GRDBTestCase { func test(value: some DatabaseValueConvertible) throws { defer { try! db.execute(sql: "DELETE FROM t") } - // Execute with non temporary bindings + // Execute with non temporary bindings (SQLITE_TRANSIENT) let statement = try db.makeStatement(sql: "INSERT INTO t VALUES (?)") try statement.setArguments([value]) try statement.execute() @@ -541,7 +577,7 @@ class UpdateStatementTests : GRDBTestCase { // Test that we have inserted the value twice. try XCTAssertEqual( DatabaseValue.fetchSet(db, sql: "SELECT a FROM t"), - [value.databaseValue, value.databaseValue]) + [value.databaseValue]) } try test(value: "Foo") From 37fd4c18b0b757f94817e0bad81a54347e4175b8 Mon Sep 17 00:00:00 2001 From: Jon Lazar Date: Mon, 13 Mar 2023 15:26:07 -0400 Subject: [PATCH 2/5] Update Migrations.md Fix closure parameter syntax so that the example compiles. --- GRDB/Documentation.docc/Migrations.md | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/GRDB/Documentation.docc/Migrations.md b/GRDB/Documentation.docc/Migrations.md index 3286237f2..4ee44da1f 100644 --- a/GRDB/Documentation.docc/Migrations.md +++ b/GRDB/Documentation.docc/Migrations.md @@ -33,7 +33,7 @@ migrator.registerMigration("Add books and author.birthYear") { db in t.column("title", .text).notNull() } - try db.alter(table: "author") { t + try db.alter(table: "author") { t in t.add(column: "birthYear", .integer) } } From 0f942c014c267c3b60b86c7976c5032d59eaf2e8 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Gwendal=20Roue=CC=81?= Date: Tue, 14 Mar 2023 08:10:56 +0100 Subject: [PATCH 3/5] Cleanup --- Tests/GRDBTests/UpdateStatementTests.swift | 22 +++++++++++++--------- 1 file changed, 13 insertions(+), 9 deletions(-) diff --git a/Tests/GRDBTests/UpdateStatementTests.swift b/Tests/GRDBTests/UpdateStatementTests.swift index 20f1d28d1..c5feaf226 100644 --- a/Tests/GRDBTests/UpdateStatementTests.swift +++ b/Tests/GRDBTests/UpdateStatementTests.swift @@ -447,7 +447,7 @@ class UpdateStatementTests : GRDBTestCase { func test_SQLITE_STATIC_then_SQLITE_TRANSIENT() throws { let dbQueue = try makeDatabaseQueue() try dbQueue.write { db in - try db.execute(literal: """ + try db.execute(sql: """ CREATE TABLE t(a); """) @@ -485,7 +485,7 @@ class UpdateStatementTests : GRDBTestCase { func test_SQLITE_STATIC() throws { let dbQueue = try makeDatabaseQueue() try dbQueue.write { db in - try db.execute(literal: """ + try db.execute(sql: """ CREATE TABLE t(a); """) @@ -517,11 +517,15 @@ class UpdateStatementTests : GRDBTestCase { } func test_SQLITE_TRANSIENT_due_to_high_number_of_arguments() throws { + // SQLITE_STATIC optimization is disabled for more than 20 arguments. + let argumentCount = 21 + let dbQueue = try makeDatabaseQueue() try dbQueue.write { db in - try db.execute(literal: """ - -- 21 columns - CREATE TABLE t(a,b,c,d,e,f,g,h,i,j,k,l,m,n,o,p,q,r,s,t,u); + // "a0, a1, a2, ..." + let columns = (0.. Date: Tue, 14 Mar 2023 08:12:38 +0100 Subject: [PATCH 4/5] CHANGELOG --- CHANGELOG.md | 1 + 1 file changed, 1 insertion(+) diff --git a/CHANGELOG.md b/CHANGELOG.md index 47ff87645..597fbaca3 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -109,6 +109,7 @@ GRDB adheres to [Semantic Versioning](https://semver.org/), with one exception: ## Next Release +- **Fixed**: [#1349](https://github.com/groue/GRDB.swift/pull/1349) by [@JonLz](https://github.com/JonLz): Fix Migrations.md example so it compiles - **Fixed**: Avoid the risk for pathologically deep stack trace, or stack overflow, introduced by [#1348](https://github.com/groue/GRDB.swift/pull/1348). ## 6.9.1 From e6c3a8d07aefa6d1a4f2424faded4394c1cb7e7b Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Gwendal=20Roue=CC=81?= Date: Tue, 14 Mar 2023 08:32:58 +0100 Subject: [PATCH 5/5] v6.9.2 --- CHANGELOG.md | 8 +++++--- GRDB.swift.podspec | 2 +- README.md | 4 ++-- Support/Info.plist | 2 +- 4 files changed, 9 insertions(+), 7 deletions(-) diff --git a/CHANGELOG.md b/CHANGELOG.md index 597fbaca3..e4187848c 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -7,7 +7,7 @@ GRDB adheres to [Semantic Versioning](https://semver.org/), with one exception: #### 6.x Releases -- `6.9.x` Releases - [6.9.0](#690) - [6.9.1](#691) +- `6.9.x` Releases - [6.9.0](#690) - [6.9.1](#691) - [6.9.2](#692) - `6.8.x` Releases - [6.8.0](#680) - `6.7.x` Releases - [6.7.0](#670) - `6.6.x` Releases - [6.6.0](#660) - [6.6.1](#661) @@ -107,14 +107,16 @@ GRDB adheres to [Semantic Versioning](https://semver.org/), with one exception: --- -## Next Release +## 6.9.2 + +Released March 14, 2023 • [diff](https://github.com/groue/GRDB.swift/compare/v6.9.1...v6.9.2) - **Fixed**: [#1349](https://github.com/groue/GRDB.swift/pull/1349) by [@JonLz](https://github.com/JonLz): Fix Migrations.md example so it compiles - **Fixed**: Avoid the risk for pathologically deep stack trace, or stack overflow, introduced by [#1348](https://github.com/groue/GRDB.swift/pull/1348). ## 6.9.1 -Released March 12, 2023 • [diff](https://github.com/groue/GRDB.swift/compare/v6.8.0...v6.9.1) +Released March 12, 2023 • [diff](https://github.com/groue/GRDB.swift/compare/v6.9.0...v6.9.1) - **Documentation Updates**: Moved more README chapters into DocC: - [`RowAdapter`](https://swiftpackageindex.com/groue/grdb.swift/documentation/grdb/rowadapter) diff --git a/GRDB.swift.podspec b/GRDB.swift.podspec index a10d5233e..84e7c2a19 100644 --- a/GRDB.swift.podspec +++ b/GRDB.swift.podspec @@ -1,6 +1,6 @@ Pod::Spec.new do |s| s.name = 'GRDB.swift' - s.version = '6.9.1' + s.version = '6.9.2' s.license = { :type => 'MIT', :file => 'LICENSE' } s.summary = 'A toolkit for SQLite databases, with a focus on application development.' diff --git a/README.md b/README.md index 8db474960..ae14c2c43 100644 --- a/README.md +++ b/README.md @@ -11,13 +11,13 @@ --- -**Latest release**: March 12, 2023 • [version 6.9.1](https://github.com/groue/GRDB.swift/tree/v6.9.1) • [CHANGELOG](CHANGELOG.md) • [Migrating From GRDB 5 to GRDB 6](Documentation/GRDB6MigrationGuide.md) +**Latest release**: March 14, 2023 • [version 6.9.2](https://github.com/groue/GRDB.swift/tree/v6.9.2) • [CHANGELOG](CHANGELOG.md) • [Migrating From GRDB 5 to GRDB 6](Documentation/GRDB6MigrationGuide.md) **Requirements**: iOS 11.0+ / macOS 10.13+ / tvOS 11.0+ / watchOS 4.0+ • SQLite 3.19.3+ • Swift 5.7+ / Xcode 14+ | Swift version | GRDB version | | -------------- | ----------------------------------------------------------- | -| **Swift 5.7+** | **v6.9.1** | +| **Swift 5.7+** | **v6.9.2** | | Swift 5.3 | [v5.26.1](https://github.com/groue/GRDB.swift/tree/v5.26.1) | | Swift 5.2 | [v5.12.0](https://github.com/groue/GRDB.swift/tree/v5.12.0) | | Swift 5.1 | [v4.14.0](https://github.com/groue/GRDB.swift/tree/v4.14.0) | diff --git a/Support/Info.plist b/Support/Info.plist index 7ea5ae46c..585756663 100644 --- a/Support/Info.plist +++ b/Support/Info.plist @@ -15,7 +15,7 @@ CFBundlePackageType FMWK CFBundleShortVersionString - 6.9.1 + 6.9.2 CFBundleSignature ???? CFBundleVersion