Performance fix:

Rewrote parameter type checks for text protocol in cpython.
This commit is contained in:
Georg Richter
2023-04-17 09:51:41 +02:00
parent 658cc0015c
commit b0366fa108
4 changed files with 33 additions and 6 deletions

View File

@ -319,6 +319,7 @@ extern PyTypeObject Mariadb_Fieldinfo_Type;
extern PyTypeObject MrdbConnection_Type;
extern PyTypeObject MrdbCursor_Type;
PyObject *ListOrTuple_GetItem(PyObject *obj, Py_ssize_t index);
int Mariadb_traverse(PyObject *self,
visitproc visit,
void *arg);

View File

@ -294,11 +294,10 @@ class Cursor(mariadb._mariadb.cursor):
if self._force_binary:
self._text = False
for val in data:
if isinstance(val, (bytes, bytearray, datetime.datetime,
datetime.date, datetime.time)):
self._text = False
break
# if one of the provided parameters has byte or datetime value,
# we don't use text protocol
if self._check_text_types() == True:
self._text = False
if self._text:
# in text mode we need to substitute parameters

View File

@ -891,7 +891,7 @@ mariadb_get_column_info(PyObject *obj, MrdbParamInfo *paraminfo)
return 1;
}
static PyObject *ListOrTuple_GetItem(PyObject *obj, Py_ssize_t index)
PyObject *ListOrTuple_GetItem(PyObject *obj, Py_ssize_t index)
{
if (CHECK_TYPE(obj, &PyList_Type))
{

View File

@ -19,6 +19,7 @@
#include <mariadb_python.h>
#include <docs/cursor.h>
#include <datetime.h>
static void
MrdbCursor_dealloc(MrdbCursor *self);
@ -39,6 +40,9 @@ MrdbCursor_InitResultSet(MrdbCursor *self);
static PyObject *
MrdbCursor_execute_text(MrdbCursor *self, PyObject *args);
static PyObject *
MrdbCursor_check_text_types(MrdbCursor *self);
static PyObject *
MrdbCursor_fetchrows(MrdbCursor *self, PyObject *args);
@ -138,6 +142,9 @@ static PyMethodDef MrdbCursor_Methods[] =
METH_NOARGS,
cursor_next__doc__},
/* internal helper functions */
{"_check_text_types", (PyCFunction) MrdbCursor_check_text_types,
METH_NOARGS,
NULL},
{"_seek", (PyCFunction)MrdbCursor_seek,
METH_VARARGS,
NULL},
@ -1273,3 +1280,23 @@ MrdbCursor_fetchrows(MrdbCursor *self, PyObject *args)
return List;
}
static PyObject *
MrdbCursor_check_text_types(MrdbCursor *self)
{
PyDateTime_IMPORT;
if (!self || !self->data || !self->parseinfo.paramcount)
{
Py_RETURN_NONE;
}
for (uint32_t i= 0; i < self->parseinfo.paramcount; i++)
{
PyObject *obj= ListOrTuple_GetItem(self->data, i);
if (PyBytes_Check(obj) ||
PyByteArray_Check(obj) ||
PyDate_Check(obj))
Py_RETURN_TRUE;
}
Py_RETURN_NONE;
}