mirror of
https://github.com/psycopg/psycopg2.git
synced 2025-03-03 23:55:46 +03:00
Optimize UTF8 and Latin1 decoding
Cache a pointer to a fast decoding function when the connection encoding is set so skip a repeated codec lookup for every string.
This commit is contained in:
parent
584c7e6890
commit
121cf3b8f8
|
@ -122,6 +122,9 @@ struct connectionObject {
|
||||||
int autocommit;
|
int autocommit;
|
||||||
|
|
||||||
PyObject *cursor_factory; /* default cursor factory from cursor() */
|
PyObject *cursor_factory; /* default cursor factory from cursor() */
|
||||||
|
|
||||||
|
/* Pointer to a decoding function, e.g. PyUnicode_DecodeUTF8 */
|
||||||
|
PyObject *(*cdecoder)(const char *, Py_ssize_t, const char *);
|
||||||
};
|
};
|
||||||
|
|
||||||
/* map isolation level values into a numeric const */
|
/* map isolation level values into a numeric const */
|
||||||
|
|
|
@ -361,6 +361,31 @@ exit:
|
||||||
return rv;
|
return rv;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
|
/* set fast access functions according to the currently selected codec
|
||||||
|
*/
|
||||||
|
void
|
||||||
|
conn_set_fast_codec(connectionObject *self)
|
||||||
|
{
|
||||||
|
Dprintf("conn_set_fast_codec: codec=%s", self->codec);
|
||||||
|
|
||||||
|
if (0 == strcmp(self->codec, "utf_8")) {
|
||||||
|
Dprintf("conn_set_fast_codec: PyUnicode_DecodeUTF8");
|
||||||
|
self->cdecoder = PyUnicode_DecodeUTF8;
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
|
||||||
|
if (0 == strcmp(self->codec, "iso8859_1")) {
|
||||||
|
Dprintf("conn_set_fast_codec: PyUnicode_DecodeLatin1");
|
||||||
|
self->cdecoder = PyUnicode_DecodeLatin1;
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
|
||||||
|
Dprintf("conn_set_fast_codec: no fast codec");
|
||||||
|
self->cdecoder = NULL;
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
/* Read the client encoding from the connection.
|
/* Read the client encoding from the connection.
|
||||||
*
|
*
|
||||||
* Store the encoding in the pgconn->encoding field and the name of the
|
* Store the encoding in the pgconn->encoding field and the name of the
|
||||||
|
@ -402,6 +427,8 @@ conn_read_encoding(connectionObject *self, PGconn *pgconn)
|
||||||
self->codec = codec;
|
self->codec = codec;
|
||||||
codec = NULL;
|
codec = NULL;
|
||||||
|
|
||||||
|
conn_set_fast_codec(self);
|
||||||
|
|
||||||
rv = 0;
|
rv = 0;
|
||||||
|
|
||||||
exit:
|
exit:
|
||||||
|
@ -1243,6 +1270,8 @@ conn_set_client_encoding(connectionObject *self, const char *enc)
|
||||||
codec = NULL;
|
codec = NULL;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
conn_set_fast_codec(self);
|
||||||
|
|
||||||
Dprintf("conn_set_client_encoding: set encoding to %s (codec: %s)",
|
Dprintf("conn_set_client_encoding: set encoding to %s (codec: %s)",
|
||||||
self->encoding, self->codec);
|
self->encoding, self->codec);
|
||||||
|
|
||||||
|
|
|
@ -93,12 +93,17 @@ typecast_STRING_cast(const char *s, Py_ssize_t len, PyObject *curs)
|
||||||
static PyObject *
|
static PyObject *
|
||||||
typecast_UNICODE_cast(const char *s, Py_ssize_t len, PyObject *curs)
|
typecast_UNICODE_cast(const char *s, Py_ssize_t len, PyObject *curs)
|
||||||
{
|
{
|
||||||
char *enc;
|
connectionObject *conn;
|
||||||
|
|
||||||
if (s == NULL) { Py_RETURN_NONE; }
|
if (s == NULL) { Py_RETURN_NONE; }
|
||||||
|
|
||||||
enc = ((cursorObject*)curs)->conn->codec;
|
conn = ((cursorObject*)curs)->conn;
|
||||||
return PyUnicode_Decode(s, len, enc, NULL);
|
if (conn->cdecoder) {
|
||||||
|
return conn->cdecoder(s, len, NULL);
|
||||||
|
}
|
||||||
|
else {
|
||||||
|
return PyUnicode_Decode(s, len, conn->codec, NULL);
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
/** BOOLEAN - cast boolean value into right python object **/
|
/** BOOLEAN - cast boolean value into right python object **/
|
||||||
|
|
Loading…
Reference in New Issue
Block a user