From b6d6fbbe8ccfd38fb9d0fab876802def9255930b Mon Sep 17 00:00:00 2001 From: Daniele Varrazzo Date: Thu, 17 Feb 2011 20:08:04 +0000 Subject: [PATCH] Use a global object for NULL Small optimization as NULL is a frequent value to build. --- psycopg/adapter_asis.c | 3 ++- psycopg/adapter_list.c | 11 +++++++---- psycopg/cursor_type.c | 8 +++++--- psycopg/psycopg.h | 3 +++ psycopg/psycopgmodule.c | 4 ++++ 5 files changed, 21 insertions(+), 8 deletions(-) diff --git a/psycopg/adapter_asis.c b/psycopg/adapter_asis.c index bc64aa74..f194e1d1 100644 --- a/psycopg/adapter_asis.c +++ b/psycopg/adapter_asis.c @@ -39,7 +39,8 @@ asis_getquoted(asisObject *self, PyObject *args) { PyObject *rv; if (self->wrapped == Py_None) { - rv = Bytes_FromString("NULL"); + Py_INCREF(psyco_null); + rv = psyco_null; } else { rv = PyObject_Str(self->wrapped); diff --git a/psycopg/adapter_list.c b/psycopg/adapter_list.c index d5c5b4a3..cbb75422 100644 --- a/psycopg/adapter_list.c +++ b/psycopg/adapter_list.c @@ -52,12 +52,15 @@ list_quote(listObject *self) for (i=0; iwrapped, i); - if (wrapped == Py_None) - quoted = Bytes_FromString("NULL"); - else + if (wrapped == Py_None) { + Py_INCREF(psyco_null); + quoted = psyco_null; + } + else { quoted = microprotocol_getquoted(wrapped, (connectionObject*)self->connection); - if (quoted == NULL) goto error; + if (quoted == NULL) goto error; + } /* here we don't loose a refcnt: SET_ITEM does not change the reference count and we are just transferring ownership of the tmp diff --git a/psycopg/cursor_type.c b/psycopg/cursor_type.c index 4330d77f..159b9de8 100644 --- a/psycopg/cursor_type.c +++ b/psycopg/cursor_type.c @@ -142,9 +142,10 @@ _mogrify(PyObject *var, PyObject *fmt, connectionObject *conn, PyObject **new) /* None is always converted to NULL; this is an optimization over the adapting code and can go away in - the future if somebody finds a None adapter usefull. */ + the future if somebody finds a None adapter useful. */ if (value == Py_None) { - t = Bytes_FromString("NULL"); + Py_INCREF(psyco_null); + t = psyco_null; PyDict_SetItem(n, key, t); /* t is a new object, refcnt = 1, key is at 2 */ @@ -220,7 +221,8 @@ _mogrify(PyObject *var, PyObject *fmt, connectionObject *conn, PyObject **new) d = c+1; if (value == Py_None) { - PyTuple_SET_ITEM(n, index, Bytes_FromString("NULL")); + Py_INCREF(psyco_null); + PyTuple_SET_ITEM(n, index, psyco_null); while (*d && !isalpha(*d)) d++; if (*d) *d = 's'; Py_DECREF(value); diff --git a/psycopg/psycopg.h b/psycopg/psycopg.h index 8edc42ed..710a51bc 100644 --- a/psycopg/psycopg.h +++ b/psycopg/psycopg.h @@ -105,6 +105,9 @@ import_psycopg(void) /* postgresql<->python encoding map */ extern HIDDEN PyObject *psycoEncodings; +/* SQL NULL */ +extern HIDDEN PyObject *psyco_null; + typedef struct { char *pgenc; char *pyenc; diff --git a/psycopg/psycopgmodule.c b/psycopg/psycopgmodule.c index 7b6a3346..59562e25 100644 --- a/psycopg/psycopgmodule.c +++ b/psycopg/psycopgmodule.c @@ -66,6 +66,9 @@ HIDDEN PyObject *psycoEncodings = NULL; HIDDEN int psycopg_debug_enabled = 0; #endif +/* Python representation of SQL NULL */ +HIDDEN PyObject *psyco_null = NULL; + /** connect module-level function **/ #define psyco_connect_doc \ "connect(dsn, ...) -- Create a new database connection.\n\n" \ @@ -882,6 +885,7 @@ INIT_MODULE(_psycopg)(void) /* other mixed initializations of module-level variables */ psycoEncodings = PyDict_New(); psyco_encodings_fill(psycoEncodings); + psyco_null = Bytes_FromString("NULL"); /* set some module's parameters */ PyModule_AddStringConstant(module, "__version__", PSYCOPG_VERSION);