Skip to content

Commit f901fd5

Browse files
author
Erlend E. Aasland
committed
bpo-44165: optimise sqlite3 statement preparation by passing string size
1 parent 834498e commit f901fd5

File tree

3 files changed

+13
-7
lines changed

3 files changed

+13
-7
lines changed

Modules/_sqlite/connection.c

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -428,7 +428,7 @@ pysqlite_connection_commit_impl(pysqlite_Connection *self)
428428
if (!sqlite3_get_autocommit(self->db)) {
429429

430430
Py_BEGIN_ALLOW_THREADS
431-
rc = sqlite3_prepare_v2(self->db, "COMMIT", -1, &statement, NULL);
431+
rc = sqlite3_prepare_v2(self->db, "COMMIT", 7, &statement, NULL);
432432
Py_END_ALLOW_THREADS
433433
if (rc != SQLITE_OK) {
434434
_pysqlite_seterror(self->db);
@@ -478,7 +478,7 @@ pysqlite_connection_rollback_impl(pysqlite_Connection *self)
478478
pysqlite_do_all_statements(self, ACTION_RESET, 1);
479479

480480
Py_BEGIN_ALLOW_THREADS
481-
rc = sqlite3_prepare_v2(self->db, "ROLLBACK", -1, &statement, NULL);
481+
rc = sqlite3_prepare_v2(self->db, "ROLLBACK", 9, &statement, NULL);
482482
Py_END_ALLOW_THREADS
483483
if (rc != SQLITE_OK) {
484484
_pysqlite_seterror(self->db);

Modules/_sqlite/cursor.c

Lines changed: 10 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -681,6 +681,7 @@ pysqlite_cursor_executescript(pysqlite_Cursor *self, PyObject *script_obj)
681681
const char* script_cstr;
682682
sqlite3_stmt* statement;
683683
int rc;
684+
Py_ssize_t script_length;
684685
PyObject* result;
685686

686687
if (!check_cursor(self)) {
@@ -690,7 +691,7 @@ pysqlite_cursor_executescript(pysqlite_Cursor *self, PyObject *script_obj)
690691
self->reset = 0;
691692

692693
if (PyUnicode_Check(script_obj)) {
693-
script_cstr = PyUnicode_AsUTF8(script_obj);
694+
script_cstr = PyUnicode_AsUTF8AndSize(script_obj, &script_length);
694695
if (!script_cstr) {
695696
return NULL;
696697
}
@@ -706,13 +707,16 @@ pysqlite_cursor_executescript(pysqlite_Cursor *self, PyObject *script_obj)
706707
}
707708
Py_DECREF(result);
708709

710+
Py_ssize_t size_incl_null = script_length + 1;
709711
while (1) {
712+
const char *tail;
713+
710714
Py_BEGIN_ALLOW_THREADS
711715
rc = sqlite3_prepare_v2(self->connection->db,
712716
script_cstr,
713-
-1,
717+
size_incl_null,
714718
&statement,
715-
&script_cstr);
719+
&tail);
716720
Py_END_ALLOW_THREADS
717721
if (rc != SQLITE_OK) {
718722
_pysqlite_seterror(self->connection->db);
@@ -740,9 +744,11 @@ pysqlite_cursor_executescript(pysqlite_Cursor *self, PyObject *script_obj)
740744
goto error;
741745
}
742746

743-
if (*script_cstr == (char)0) {
747+
if (*tail == (char)0) {
744748
break;
745749
}
750+
size_incl_null -= (tail - script_cstr);
751+
script_cstr = tail;
746752
}
747753

748754
error:

Modules/_sqlite/statement.c

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -96,7 +96,7 @@ int pysqlite_statement_create(pysqlite_Statement* self, pysqlite_Connection* con
9696
Py_BEGIN_ALLOW_THREADS
9797
rc = sqlite3_prepare_v2(connection->db,
9898
sql_cstr,
99-
-1,
99+
sql_cstr_len + 1,
100100
&self->st,
101101
&tail);
102102
Py_END_ALLOW_THREADS

0 commit comments

Comments
 (0)