From ee98f2a169775048ea1c5bed547166ad49038528 Mon Sep 17 00:00:00 2001 From: "Erlend E. Aasland" Date: Mon, 10 Feb 2025 01:03:27 +0100 Subject: [PATCH 1/2] gh-129926: Speed up sqlite3.Row item access --- Modules/_sqlite/row.c | 10 +++++----- 1 file changed, 5 insertions(+), 5 deletions(-) diff --git a/Modules/_sqlite/row.c b/Modules/_sqlite/row.c index 94565a01d18f24..b34e09988cf550 100644 --- a/Modules/_sqlite/row.c +++ b/Modules/_sqlite/row.c @@ -113,9 +113,8 @@ equal_ignore_case(PyObject *left, PyObject *right) if (eq) { /* equal or error */ return eq; } - if (!PyUnicode_Check(left) || !PyUnicode_Check(right)) { - return 0; - } + assert(PyUnicode_Check(left)); + assert(PyUnicode_Check(right)); if (!PyUnicode_IS_ASCII(left) || !PyUnicode_IS_ASCII(right)) { return 0; } @@ -154,6 +153,7 @@ pysqlite_row_subscript(PyObject *op, PyObject *idx) PyErr_Format(PyExc_IndexError, "No item with key %R", idx); return NULL; } + assert(PyTuple_Check(self->description)); Py_ssize_t nitems = PyTuple_GET_SIZE(self->description); for (Py_ssize_t i = 0; i < nitems; i++) { @@ -166,8 +166,7 @@ pysqlite_row_subscript(PyObject *op, PyObject *idx) } if (eq) { /* found item */ - PyObject *item = PyTuple_GetItem(self->data, i); - return Py_XNewRef(item); + return PyTuple_GET_ITEM(self->data, i); } } @@ -208,6 +207,7 @@ pysqlite_row_keys_impl(pysqlite_Row *self) return list; } + assert(PyTuple_Check(self->description)); Py_ssize_t nitems = PyTuple_GET_SIZE(self->description); for (Py_ssize_t i = 0; i < nitems; i++) { PyObject *descr = PyTuple_GET_ITEM(self->description, i); From 2ae9c189917b5cc0fd896c672997abec4f4bb7e7 Mon Sep 17 00:00:00 2001 From: "Erlend E. Aasland" Date: Mon, 31 Mar 2025 00:15:19 +0200 Subject: [PATCH 2/2] Address review --- Modules/_sqlite/row.c | 4 +++- 1 file changed, 3 insertions(+), 1 deletion(-) diff --git a/Modules/_sqlite/row.c b/Modules/_sqlite/row.c index b34e09988cf550..81a3cacddde9da 100644 --- a/Modules/_sqlite/row.c +++ b/Modules/_sqlite/row.c @@ -166,7 +166,9 @@ pysqlite_row_subscript(PyObject *op, PyObject *idx) } if (eq) { /* found item */ - return PyTuple_GET_ITEM(self->data, i); + PyObject *item = PyTuple_GET_ITEM(self->data, i); + assert(item != NULL); + return Py_NewRef(item); } }