diff options
author | Daniele Varrazzo <daniele.varrazzo@gmail.com> | 2016-10-12 01:10:31 +0100 |
---|---|---|
committer | Daniele Varrazzo <daniele.varrazzo@gmail.com> | 2016-10-12 01:10:31 +0100 |
commit | 121cf3b8f8426765d983579d3a4b2e932429cd9f (patch) | |
tree | 2c43095c9dbef1c3f4c2a03963d2eb22dbd623ca /psycopg/connection_int.c | |
parent | 584c7e68902c45c72f2ab3b0b171aca58ce36f0d (diff) | |
download | psycopg2-121cf3b8f8426765d983579d3a4b2e932429cd9f.tar.gz |
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.
Diffstat (limited to 'psycopg/connection_int.c')
-rw-r--r-- | psycopg/connection_int.c | 29 |
1 files changed, 29 insertions, 0 deletions
diff --git a/psycopg/connection_int.c b/psycopg/connection_int.c index 43d0fda..62976d4 100644 --- a/psycopg/connection_int.c +++ b/psycopg/connection_int.c @@ -361,6 +361,31 @@ exit: 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. * * 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; codec = NULL; + conn_set_fast_codec(self); + rv = 0; exit: @@ -1243,6 +1270,8 @@ conn_set_client_encoding(connectionObject *self, const char *enc) codec = NULL; } + conn_set_fast_codec(self); + Dprintf("conn_set_client_encoding: set encoding to %s (codec: %s)", self->encoding, self->codec); |