Opened 6 years ago

Last modified 3 years ago

#3 new enhancement

Cython calls ExtType.__init__() as Python function

Reported by: robertwb Owned by: somebody
Priority: major Milestone: wishlist
Component: Optimization Keywords:

Description (last modified by scoder)

When subclasses of extension types call the __init__() method of their supertype, Cython generates code that looks up the "__init__" attribute of the instance and then calls it through Python using arg tuple/kwargs. This is because the special __init__() method ("tp_init" slot) uses this call signature.

Cython should recognise calls to this method and at least call it directly.

In the (presumably very common) case that the arguments do not use starargs, a more advanced approach would be to split the __init__() method into an internal plain C-function replacement and a tp_init wrapper, and then call the internal function directly, without doing any tuple packing etc. Not sure if it's worth it, though, as the call is already preceded by an (expensive) object allocation.

Change History (6)

comment:1 Changed 6 years ago by robertwb

  • Owner changed from robertwb to somebody

comment:2 Changed 6 years ago by gfurnish

One of the first things I do is make my own c wrappers for creating objects. Even with the expensive new call the savings it noticeable.

comment:3 Changed 6 years ago by robertwb

  • Milestone set to wishlist

comment:4 Changed 6 years ago by scoder

  • Description modified (diff)

The same idea is reported in #141. This should use cpdef semantics.

comment:5 Changed 6 years ago by scoder

The same applies to other special methods like __call__. (noted here to close #141 as a duplicate)

comment:6 Changed 3 years ago by scoder

The test in tests/run/special_methods_T561.pyx has some test code for this:

Note: See TracTickets for help on using tickets.