[Cython] Py_New still good way to create classes?
Robert Bradshaw
robertwb at math.washington.edu
Wed Apr 15 11:31:42 CEST 2009
On Apr 5, 2009, at 12:26 PM, Hoyt Koepke wrote:
> Hello,
>
> I'm using an extension class where I need to create a number of
> instances quickly, and I'm looking at the section "Can Cython create
> objects or apply operators to locally created objects as pure C code?"
> on http://wiki.cython.org/FAQ. Is this information still correct?
>
> In particular, I'm wondering about it in reference to supporting
> cyclical garbage collection and creating object via PyObject_GC_New
> etc. IIRC, cython automatically supports cyclical garbage collection
> on classes that have python members, and my extension class falls into
> this category. Should I
>
> a) Use Py_NEW, as specified in the FAQ?
Yes, if one has an absolute need for speed. Cyclic garbage collection
is supported via a flag on the type.
> b) Replace Py_NEW with Py_Object_GC_New, and do a?
Py_Object_GC_New will allocate the space, but will not call the
__cinit__ functions, and in particular will not initialize the base
fields, so don't use this unless you really know what you're doing.
> c) Ignore this and just go with standard creation and deletion,
> ignoring the fact that I'll be calling the constructors through
> python.
This is what I would do, at least until I determined object creation
was a large bottleneck. PY_NEW as stated on the wiki calls the
__cinit__ functions, but avoids calling the __init__ functions, so
depending on your usecase it can be a savings. Also, it would be
desirable and feasible to potentially call __init__ via a C call
rather than a Python call (maybe, if the signature is simple enough,
using the cpdef trick to avoid the pythonic argument packing/
unpacking overhead.)
- Robert
More information about the Cython-dev
mailing list