[Cython] Storing many, many instances
Robert Bradshaw
robertwb at math.washington.edu
Wed May 6 19:38:53 CEST 2009
On May 6, 2009, at 8:56 AM, Carl Witty wrote:
> On Wed, May 6, 2009 at 6:23 AM, Stefan Behnel <stefan_ml at behnel.de>
> wrote:
>> 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.
>
> Well, you could optimize tp_new. You could have it call the base
> type's tp_new directly, instead of through a vtable (at least if the
> base type is a Cython type);
I already implemented this optimization, though only for cdef classes
within the same module. (If they're small enough, they may get even
inlined by the C compiler.) I'm not sure how possible this would be
to do across modules, but one difficulty is that the contents of
tp_new can change based on the .pyx file, not just the .pxd file.
That being said, one does have control over allocation via the
tp_alloc method. Allocating an array of objects as a big chuck is
messy though--how would one handle deallocating them one by one? Or
would one try to reference-count them as a group? I think it could be
done, but it would be a non-trivial interface.
- Robert
More information about the Cython-dev
mailing list