Use a global object for NULL

Small optimization as NULL is a frequent value to build.
This commit is contained in:
Daniele Varrazzo 2011-02-17 20:08:04 +00:00
parent c51165e2aa
commit b6d6fbbe8c
5 changed files with 21 additions and 8 deletions

View File

@ -39,7 +39,8 @@ asis_getquoted(asisObject *self, PyObject *args)
{ {
PyObject *rv; PyObject *rv;
if (self->wrapped == Py_None) { if (self->wrapped == Py_None) {
rv = Bytes_FromString("NULL"); Py_INCREF(psyco_null);
rv = psyco_null;
} }
else { else {
rv = PyObject_Str(self->wrapped); rv = PyObject_Str(self->wrapped);

View File

@ -52,12 +52,15 @@ list_quote(listObject *self)
for (i=0; i<len; i++) { for (i=0; i<len; i++) {
PyObject *quoted; PyObject *quoted;
PyObject *wrapped = PyList_GET_ITEM(self->wrapped, i); PyObject *wrapped = PyList_GET_ITEM(self->wrapped, i);
if (wrapped == Py_None) if (wrapped == Py_None) {
quoted = Bytes_FromString("NULL"); Py_INCREF(psyco_null);
else quoted = psyco_null;
}
else {
quoted = microprotocol_getquoted(wrapped, quoted = microprotocol_getquoted(wrapped,
(connectionObject*)self->connection); (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 /* here we don't loose a refcnt: SET_ITEM does not change the
reference count and we are just transferring ownership of the tmp reference count and we are just transferring ownership of the tmp

View File

@ -142,9 +142,10 @@ _mogrify(PyObject *var, PyObject *fmt, connectionObject *conn, PyObject **new)
/* None is always converted to NULL; this is an /* None is always converted to NULL; this is an
optimization over the adapting code and can go away in 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) { if (value == Py_None) {
t = Bytes_FromString("NULL"); Py_INCREF(psyco_null);
t = psyco_null;
PyDict_SetItem(n, key, t); PyDict_SetItem(n, key, t);
/* t is a new object, refcnt = 1, key is at 2 */ /* 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; d = c+1;
if (value == Py_None) { 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++; while (*d && !isalpha(*d)) d++;
if (*d) *d = 's'; if (*d) *d = 's';
Py_DECREF(value); Py_DECREF(value);

View File

@ -105,6 +105,9 @@ import_psycopg(void)
/* postgresql<->python encoding map */ /* postgresql<->python encoding map */
extern HIDDEN PyObject *psycoEncodings; extern HIDDEN PyObject *psycoEncodings;
/* SQL NULL */
extern HIDDEN PyObject *psyco_null;
typedef struct { typedef struct {
char *pgenc; char *pgenc;
char *pyenc; char *pyenc;

View File

@ -66,6 +66,9 @@ HIDDEN PyObject *psycoEncodings = NULL;
HIDDEN int psycopg_debug_enabled = 0; HIDDEN int psycopg_debug_enabled = 0;
#endif #endif
/* Python representation of SQL NULL */
HIDDEN PyObject *psyco_null = NULL;
/** connect module-level function **/ /** connect module-level function **/
#define psyco_connect_doc \ #define psyco_connect_doc \
"connect(dsn, ...) -- Create a new database connection.\n\n" \ "connect(dsn, ...) -- Create a new database connection.\n\n" \
@ -882,6 +885,7 @@ INIT_MODULE(_psycopg)(void)
/* other mixed initializations of module-level variables */ /* other mixed initializations of module-level variables */
psycoEncodings = PyDict_New(); psycoEncodings = PyDict_New();
psyco_encodings_fill(psycoEncodings); psyco_encodings_fill(psycoEncodings);
psyco_null = Bytes_FromString("NULL");
/* set some module's parameters */ /* set some module's parameters */
PyModule_AddStringConstant(module, "__version__", PSYCOPG_VERSION); PyModule_AddStringConstant(module, "__version__", PSYCOPG_VERSION);