Skip to content

bpo-42064: Optimise sqlite3 state access, part 1 #27273

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 6 commits into from
Jul 29, 2021
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
21 changes: 16 additions & 5 deletions Modules/_sqlite/clinic/cursor.c.h
Original file line number Diff line number Diff line change
Expand Up @@ -249,14 +249,25 @@ PyDoc_STRVAR(pysqlite_cursor_close__doc__,
"Closes the cursor.");

#define PYSQLITE_CURSOR_CLOSE_METHODDEF \
{"close", (PyCFunction)pysqlite_cursor_close, METH_NOARGS, pysqlite_cursor_close__doc__},
{"close", (PyCFunction)(void(*)(void))pysqlite_cursor_close, METH_METHOD|METH_FASTCALL|METH_KEYWORDS, pysqlite_cursor_close__doc__},

static PyObject *
pysqlite_cursor_close_impl(pysqlite_Cursor *self);
pysqlite_cursor_close_impl(pysqlite_Cursor *self, PyTypeObject *cls);

static PyObject *
pysqlite_cursor_close(pysqlite_Cursor *self, PyObject *Py_UNUSED(ignored))
pysqlite_cursor_close(pysqlite_Cursor *self, PyTypeObject *cls, PyObject *const *args, Py_ssize_t nargs, PyObject *kwnames)
{
return pysqlite_cursor_close_impl(self);
PyObject *return_value = NULL;
static const char * const _keywords[] = { NULL};
static _PyArg_Parser _parser = {":close", _keywords, 0};

if (!_PyArg_ParseStackAndKeywords(args, nargs, kwnames, &_parser
)) {
goto exit;
}
return_value = pysqlite_cursor_close_impl(self, cls);

exit:
return return_value;
}
/*[clinic end generated code: output=8f70eac5f8aa8d97 input=a9049054013a1b77]*/
/*[clinic end generated code: output=7b216aba2439f5cf input=a9049054013a1b77]*/
37 changes: 19 additions & 18 deletions Modules/_sqlite/connection.c
Original file line number Diff line number Diff line change
Expand Up @@ -66,8 +66,8 @@ new_statement_cache(pysqlite_Connection *self, int maxsize)
if (args[0] == NULL) {
return NULL;
}
pysqlite_state *state = pysqlite_get_state(NULL);
PyObject *inner = PyObject_Vectorcall(state->lru_cache, args, 1, NULL);
PyObject *lru_cache = self->state->lru_cache;
PyObject *inner = PyObject_Vectorcall(lru_cache, args, 1, NULL);
Py_DECREF(args[0]);
if (inner == NULL) {
return NULL;
Expand Down Expand Up @@ -106,6 +106,9 @@ pysqlite_connection_init_impl(pysqlite_Connection *self,
return -1;
}

pysqlite_state *state = pysqlite_get_state_by_type(Py_TYPE(self));
self->state = state;

const char *database = PyBytes_AsString(database_obj);

self->initialized = 1;
Expand All @@ -131,7 +134,7 @@ pysqlite_connection_init_impl(pysqlite_Connection *self,
Py_DECREF(database_obj); // needed bco. the AC FSConverter

if (rc != SQLITE_OK) {
_pysqlite_seterror(self->db);
_pysqlite_seterror(state, self->db);
return -1;
}

Expand Down Expand Up @@ -177,7 +180,6 @@ pysqlite_connection_init_impl(pysqlite_Connection *self,
self->function_pinboard_progress_handler = NULL;
self->function_pinboard_authorizer_cb = NULL;

pysqlite_state *state = pysqlite_get_state(NULL);
self->Warning = state->Warning;
self->Error = state->Error;
self->InterfaceError = state->InterfaceError;
Expand Down Expand Up @@ -330,15 +332,14 @@ pysqlite_connection_cursor_impl(pysqlite_Connection *self, PyObject *factory)
return NULL;
}

pysqlite_state *state = pysqlite_get_state(NULL);
if (factory == NULL) {
factory = (PyObject *)state->CursorType;
factory = (PyObject *)self->state->CursorType;
}

cursor = PyObject_CallOneArg(factory, (PyObject *)self);
if (cursor == NULL)
return NULL;
if (!PyObject_TypeCheck(cursor, state->CursorType)) {
if (!PyObject_TypeCheck(cursor, self->state->CursorType)) {
PyErr_Format(PyExc_TypeError,
"factory must return a cursor, not %.100s",
Py_TYPE(cursor)->tp_name);
Expand Down Expand Up @@ -383,15 +384,15 @@ pysqlite_connection_close_impl(pysqlite_Connection *self)
*/
int pysqlite_check_connection(pysqlite_Connection* con)
{
pysqlite_state *state = pysqlite_get_state(NULL);
if (!con->initialized) {
pysqlite_state *state = pysqlite_get_state_by_type(Py_TYPE(con));
PyErr_SetString(state->ProgrammingError,
"Base Connection.__init__ not called.");
return 0;
}

if (!con->db) {
PyErr_SetString(state->ProgrammingError,
PyErr_SetString(con->state->ProgrammingError,
"Cannot operate on a closed database.");
return 0;
} else {
Expand Down Expand Up @@ -422,20 +423,20 @@ pysqlite_connection_commit_impl(pysqlite_Connection *self)
rc = sqlite3_prepare_v2(self->db, "COMMIT", 7, &statement, NULL);
Py_END_ALLOW_THREADS
if (rc != SQLITE_OK) {
_pysqlite_seterror(self->db);
_pysqlite_seterror(self->state, self->db);
goto error;
}

rc = pysqlite_step(statement);
if (rc != SQLITE_DONE) {
_pysqlite_seterror(self->db);
_pysqlite_seterror(self->state, self->db);
}

Py_BEGIN_ALLOW_THREADS
rc = sqlite3_finalize(statement);
Py_END_ALLOW_THREADS
if (rc != SQLITE_OK && !PyErr_Occurred()) {
_pysqlite_seterror(self->db);
_pysqlite_seterror(self->state, self->db);
}

}
Expand Down Expand Up @@ -472,20 +473,20 @@ pysqlite_connection_rollback_impl(pysqlite_Connection *self)
rc = sqlite3_prepare_v2(self->db, "ROLLBACK", 9, &statement, NULL);
Py_END_ALLOW_THREADS
if (rc != SQLITE_OK) {
_pysqlite_seterror(self->db);
_pysqlite_seterror(self->state, self->db);
goto error;
}

rc = pysqlite_step(statement);
if (rc != SQLITE_DONE) {
_pysqlite_seterror(self->db);
_pysqlite_seterror(self->state, self->db);
}

Py_BEGIN_ALLOW_THREADS
rc = sqlite3_finalize(statement);
Py_END_ALLOW_THREADS
if (rc != SQLITE_OK && !PyErr_Occurred()) {
_pysqlite_seterror(self->db);
_pysqlite_seterror(self->state, self->db);
}

}
Expand Down Expand Up @@ -1672,7 +1673,7 @@ pysqlite_connection_backup_impl(pysqlite_Connection *self,
Py_END_ALLOW_THREADS

if (bck_handle == NULL) {
_pysqlite_seterror(bck_conn);
_pysqlite_seterror(self->state, bck_conn);
return NULL;
}

Expand Down Expand Up @@ -1710,7 +1711,7 @@ pysqlite_connection_backup_impl(pysqlite_Connection *self,
Py_END_ALLOW_THREADS

if (rc != SQLITE_OK) {
_pysqlite_seterror(bck_conn);
_pysqlite_seterror(self->state, bck_conn);
return NULL;
}

Expand Down Expand Up @@ -1762,7 +1763,7 @@ pysqlite_connection_create_collation_impl(pysqlite_Connection *self,
if (callable != Py_None) {
Py_DECREF(callable);
}
_pysqlite_seterror(self->db);
_pysqlite_seterror(self->state, self->db);
return NULL;
}

Expand Down
1 change: 1 addition & 0 deletions Modules/_sqlite/connection.h
Original file line number Diff line number Diff line change
Expand Up @@ -36,6 +36,7 @@ typedef struct
{
PyObject_HEAD
sqlite3* db;
pysqlite_state *state;

/* the type detection mode. Only 0, PARSE_DECLTYPES, PARSE_COLNAMES or a
* bitwise combination thereof makes sense */
Expand Down
50 changes: 28 additions & 22 deletions Modules/_sqlite/cursor.c
Original file line number Diff line number Diff line change
Expand Up @@ -130,7 +130,8 @@ cursor_dealloc(pysqlite_Cursor *self)
}

static PyObject *
_pysqlite_get_converter(const char *keystr, Py_ssize_t keylen)
_pysqlite_get_converter(pysqlite_state *state, const char *keystr,
Py_ssize_t keylen)
{
PyObject *key;
PyObject *upcase_key;
Expand All @@ -147,7 +148,6 @@ _pysqlite_get_converter(const char *keystr, Py_ssize_t keylen)
return NULL;
}

pysqlite_state *state = pysqlite_get_state(NULL);
retval = PyDict_GetItemWithError(state->converters, upcase_key);
Py_DECREF(upcase_key);

Expand Down Expand Up @@ -187,7 +187,9 @@ pysqlite_build_row_cast_map(pysqlite_Cursor* self)
type_start = pos + 1;
}
else if (*pos == ']' && type_start != NULL) {
converter = _pysqlite_get_converter(type_start, pos - type_start);
pysqlite_state *state = self->connection->state;
converter = _pysqlite_get_converter(state, type_start,
pos - type_start);
if (!converter && PyErr_Occurred()) {
Py_CLEAR(self->row_cast_map);
return -1;
Expand All @@ -206,7 +208,9 @@ pysqlite_build_row_cast_map(pysqlite_Cursor* self)
* 'NUMBER(10)' to be treated as 'NUMBER', for example.
* In other words, it will work as people expect it to work.*/
if (*pos == ' ' || *pos == '(' || *pos == 0) {
converter = _pysqlite_get_converter(decltype, pos - decltype);
pysqlite_state *state = self->connection->state;
converter = _pysqlite_get_converter(state, decltype,
pos - decltype);
if (!converter && PyErr_Occurred()) {
Py_CLEAR(self->row_cast_map);
return -1;
Expand Down Expand Up @@ -404,22 +408,21 @@ _pysqlite_fetch_one_row(pysqlite_Cursor* self)
*/
static int check_cursor(pysqlite_Cursor* cur)
{
pysqlite_state *state = pysqlite_get_state(NULL);

if (!cur->initialized) {
pysqlite_state *state = pysqlite_get_state_by_type(Py_TYPE(cur));
PyErr_SetString(state->ProgrammingError,
"Base Cursor.__init__ not called.");
return 0;
}

if (cur->closed) {
PyErr_SetString(state->ProgrammingError,
PyErr_SetString(cur->connection->state->ProgrammingError,
"Cannot operate on a closed cursor.");
return 0;
}

if (cur->locked) {
PyErr_SetString(state->ProgrammingError,
PyErr_SetString(cur->connection->state->ProgrammingError,
"Recursive use of cursors not allowed.");
return 0;
}
Expand All @@ -439,7 +442,7 @@ begin_transaction(pysqlite_Connection *self)
Py_END_ALLOW_THREADS

if (rc != SQLITE_OK) {
_pysqlite_seterror(self->db);
_pysqlite_seterror(self->state, self->db);
goto error;
}

Expand All @@ -449,7 +452,7 @@ begin_transaction(pysqlite_Connection *self)
Py_END_ALLOW_THREADS

if (rc != SQLITE_OK && !PyErr_Occurred()) {
_pysqlite_seterror(self->db);
_pysqlite_seterror(self->state, self->db);
}

error:
Expand All @@ -470,7 +473,6 @@ get_statement_from_cache(pysqlite_Cursor *self, PyObject *operation)
static PyObject *
_pysqlite_query_execute(pysqlite_Cursor* self, int multiple, PyObject* operation, PyObject* second_argument)
{
pysqlite_state *state = pysqlite_get_state(NULL);
PyObject* parameters_list = NULL;
PyObject* parameters_iter = NULL;
PyObject* parameters = NULL;
Expand Down Expand Up @@ -568,6 +570,7 @@ _pysqlite_query_execute(pysqlite_Cursor* self, int multiple, PyObject* operation
}
}

pysqlite_state *state = self->connection->state;
while (1) {
parameters = PyIter_Next(parameters_iter);
if (!parameters) {
Expand All @@ -576,7 +579,7 @@ _pysqlite_query_execute(pysqlite_Cursor* self, int multiple, PyObject* operation

pysqlite_statement_mark_dirty(self->statement);

pysqlite_statement_bind_parameters(self->statement, parameters);
pysqlite_statement_bind_parameters(state, self->statement, parameters);
if (PyErr_Occurred()) {
goto error;
}
Expand All @@ -592,12 +595,12 @@ _pysqlite_query_execute(pysqlite_Cursor* self, int multiple, PyObject* operation
}
}
(void)pysqlite_statement_reset(self->statement);
_pysqlite_seterror(self->connection->db);
_pysqlite_seterror(state, self->connection->db);
goto error;
}

if (pysqlite_build_row_cast_map(self) != 0) {
_PyErr_FormatFromCause(self->connection->OperationalError,
_PyErr_FormatFromCause(state->OperationalError,
"Error while building row_cast_map");
goto error;
}
Expand Down Expand Up @@ -651,7 +654,7 @@ _pysqlite_query_execute(pysqlite_Cursor* self, int multiple, PyObject* operation

if (rc == SQLITE_ROW) {
if (multiple) {
PyErr_SetString(self->connection->ProgrammingError,
PyErr_SetString(state->ProgrammingError,
"executemany() can only execute DML "
"statements.");
goto error;
Expand Down Expand Up @@ -773,6 +776,7 @@ pysqlite_cursor_executescript(pysqlite_Cursor *self, PyObject *script_obj)
}
Py_DECREF(result);

pysqlite_state *state = self->connection->state;
while (1) {
const char *tail;

Expand All @@ -784,7 +788,7 @@ pysqlite_cursor_executescript(pysqlite_Cursor *self, PyObject *script_obj)
&tail);
Py_END_ALLOW_THREADS
if (rc != SQLITE_OK) {
_pysqlite_seterror(self->connection->db);
_pysqlite_seterror(state, self->connection->db);
goto error;
}

Expand All @@ -799,13 +803,13 @@ pysqlite_cursor_executescript(pysqlite_Cursor *self, PyObject *script_obj)

if (rc != SQLITE_DONE) {
(void)sqlite3_finalize(statement);
_pysqlite_seterror(self->connection->db);
_pysqlite_seterror(state, self->connection->db);
goto error;
}

rc = sqlite3_finalize(statement);
if (rc != SQLITE_OK) {
_pysqlite_seterror(self->connection->db);
_pysqlite_seterror(state, self->connection->db);
goto error;
}

Expand Down Expand Up @@ -874,7 +878,7 @@ pysqlite_cursor_iternext(pysqlite_Cursor *self)
if (rc != SQLITE_DONE && rc != SQLITE_ROW) {
(void)pysqlite_statement_reset(self->statement);
Py_DECREF(next_row);
_pysqlite_seterror(self->connection->db);
_pysqlite_seterror(self->connection->state, self->connection->db);
return NULL;
}

Expand Down Expand Up @@ -1023,15 +1027,17 @@ pysqlite_cursor_setoutputsize_impl(pysqlite_Cursor *self, PyObject *size,
/*[clinic input]
_sqlite3.Cursor.close as pysqlite_cursor_close

cls: defining_class

Closes the cursor.
[clinic start generated code]*/

static PyObject *
pysqlite_cursor_close_impl(pysqlite_Cursor *self)
/*[clinic end generated code: output=b6055e4ec6fe63b6 input=08b36552dbb9a986]*/
pysqlite_cursor_close_impl(pysqlite_Cursor *self, PyTypeObject *cls)
/*[clinic end generated code: output=a08ab3d772f45438 input=28ba9b532ab46ba0]*/
{
if (!self->connection) {
pysqlite_state *state = pysqlite_get_state(NULL);
pysqlite_state *state = pysqlite_get_state_by_cls(cls);
PyErr_SetString(state->ProgrammingError,
"Base Cursor.__init__ not called.");
return NULL;
Expand Down
Loading