[Pyrex] cdef __init__

Robert Bradshaw robertwb at math.washington.edu
Wed Mar 28 01:33:46 UTC 2007


We've taken a slightly different track when trying to instantiate  
objects quickly, namely we've defined a macro PY_NEW as

#define PY_NEW(zzz_type) \
     (((PyTypeObject*)(zzz_type))->tp_new((PyTypeObject*)(zzz_type),  
global_empty_tuple, NULL))

We also have a module element global_empty_tuple that we instantiate  
once and keep around. The tuple argument unpacking still happens  
(although quickly) inside the new method, but it is pretty fast.

We call this rather than the init method to get a new object quickly.  
Typically our new methods are fairly minimal, and do just the  
essentials (e.g. memory allocation). This only works when the new  
method can take an empty argument list. What has been useful as well  
is creating a new_c method of a class, which we use as the "cdef'd"  
init method.

- Robert


On Mar 25, 2007, at 4:23 PM, Greg Ewing wrote:

> Sam Rushing wrote:
>
>> The attached hack supports a very specific form of fast constructor.
>> It's not ideal, and hopefully Greg can find a cleaner and better  
>> way to
>> wedge it in than I did.
>
> I'll have to give this some careful thought. I'd like
> to find some way of integrating this with the normal
> construction mechanism so you don't have to write two
> initialisation methods if you want your class to be
> instantiable from Python as well. Also I need to
> figure out what happens when there is inheritance.
>
> So I probably won't be incorporating this patch as
> it stands, but it's given me some ideas to work on.
>
> Thanks,
> Greg
>
> _______________________________________________
> Pyrex mailing list
> Pyrex at lists.copyleft.no
> http://lists.copyleft.no/mailman/listinfo/pyrex




More information about the Pyrex mailing list