Skip to content

Commit

Permalink
Performance fix:
Browse files Browse the repository at this point in the history
Rewrote parameter type checks for text protocol in cpython.
  • Loading branch information
9EOR9 committed Apr 17, 2023
1 parent 658cc00 commit b0366fa
Show file tree
Hide file tree
Showing 4 changed files with 33 additions and 6 deletions.
1 change: 1 addition & 0 deletions include/mariadb_python.h
Expand Up @@ -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);
Expand Down
9 changes: 4 additions & 5 deletions mariadb/cursors.py
Expand Up @@ -294,11 +294,10 @@ def execute(self, statement: str, data: Sequence = (), buffered=None):
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
Expand Down
2 changes: 1 addition & 1 deletion mariadb/mariadb_codecs.c
Expand Up @@ -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))
{
Expand Down
27 changes: 27 additions & 0 deletions mariadb/mariadb_cursor.c
Expand Up @@ -19,6 +19,7 @@

#include <mariadb_python.h>
#include <docs/cursor.h>
#include <datetime.h>

static void
MrdbCursor_dealloc(MrdbCursor *self);
Expand All @@ -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);

Expand Down Expand Up @@ -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},
Expand Down Expand Up @@ -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;
}

0 comments on commit b0366fa

Please sign in to comment.