[pypy-issue] [issue1121] [cpyext] speed up some macros

Stefan Behnel tracker at bugs.pypy.org
Tue Apr 10 22:41:03 CEST 2012

New submission from Stefan Behnel <stefan_ml at behnel.de>:

Inlining a fast path into a macro can avoid the call into cpyext and greatly
speed up the macro. Here are some proposed improvements:

// include/object.h

#define Py_INCREF(ob)  ((((PyObject *)ob)->ob_refcnt > 0) ? \
     ((PyObject *)ob)->ob_refcnt++ : (Py_IncRef((PyObject *)ob)))

#define Py_DECREF(ob)  ((((PyObject *)ob)->ob_refcnt > 1) ? \
     ((PyObject *)ob)->ob_refcnt-- : (Py_DecRef((PyObject *)ob)))

#define Py_XINCREF(op) do { if ((op) == NULL) ; else Py_INCREF(op); \
                          } while (0)

#define Py_XDECREF(op) do { if ((op) == NULL) ; else Py_DECREF(op); \
                          } while (0)

// unicodeobject.h (with a the slight pessimisation by calling
PyUnicode_AsUnicode() instead of the other way round - but if we have to call
it, we already know that we have to allocate the buffer, so that won't hurt much)

#define PyUnicode_AS_UNICODE(obj) (((PyUnicodeObject *)(obj))->c_buffer ? \
     (((PyUnicodeObject *)(obj))->c_buffer : PyUnicode_AsUnicode(obj))

#define PyUnicode_AS_DATA(obj) ((char*)PyUnicode_AS_UNICODE(obj))

// stringobject.h

#define PyString_AS_STRING(obj) (((PyStringObject *)(obj))->c_buffer ? \
     (((PyStringObject *)(obj))->c_buffer : PyString_AsString(obj))

messages: 4220
nosy: pypy-issue, sbehnel
priority: performance bug
status: unread
title: [cpyext] speed up some macros

PyPy bug tracker <tracker at bugs.pypy.org>

More information about the pypy-issue mailing list