[Cython] Storing many, many instances

Stefan Behnel stefan_ml at behnel.de
Wed May 6 15:23:59 CEST 2009


Dag Sverre Seljebotn wrote:
> Stefan Behnel wrote:
>> Dag Sverre Seljebotn wrote:
>>> Stefan Behnel wrote:
>>>> It might work
>>>> to always use PY_NEW() for instantiation and then generate a separate
>>>> (and direct) call to __init__() only if the type defines it.
>>>>
>>> Should one consider allocating arrays of cdef class objects as one big
>>> memory block?
>>
>> We do not control the allocation, CPython handles that.
>
> OK. I didn't know exactly how fine-grained the CPython API was and
> whether it was possible to run a constructor on already allocated memory
> (like you can do in e.g. C++). It just seemed like that could be one of
> the major bottlenecks.

What we tend to call PY_NEW here is a direct call to a type's tp_new(),
which handles both the allocation and the basic initialisation of an
instance. Cython could do a tiny bit better here, since we know exactly
what function a cdef class has in its tp_new slot. However, that function
will always recursively run into the base type's tp_new and at the end of
the chain into the one of Python's 'type' type, and there is not much we
can do to improve this situation in general.

Stefan



More information about the Cython-dev mailing list