Inlining methods of a cdef class

81 views
Skip to first unread message

Nathann Cohen

unread,
Nov 11, 2012, 2:28:39 PM11/11/12
to cython...@googlegroups.com, Florent Hivert
Hellooooooooo everybody !!

My question is very simple. Right now, I have a C struct on which I can apply different functions, and I need those functions to be *FAST*, so I have them inlined. This being said, dealing with a struct (allocation, deallocation, applying the functions) would be much easier to if my C struct were an object. Well. If I make it a "cdef class" and make methods out of my functions, can I make these methods inlined, or will I necessary lose time because I have a class instead ? :-)

Thank you for your help ! :-)

Nathann

Stefan Behnel

unread,
Nov 12, 2012, 1:17:45 AM11/12/12
to cython...@googlegroups.com
Nathann Cohen, 11.11.2012 20:28:
> My question is very simple. Right now, I have a C struct on which I can
> apply different functions, and I need those functions to be *FAST*, so I
> have them inlined. This being said, dealing with a struct (allocation,
> deallocation, applying the functions) would be much easier to if my C
> struct were an object. Well. If I make it a "cdef class" and make methods
> out of my functions, can I make these methods inlined, or will I necessary
> lose time because I have a class instead ? :-)

If you add a decorator "@cython.final" (after a "cimport cython") to the
class (or to some of its methods), you get direct function calls instead of
method calls. By adding the "inline" modifier to some methods, you can ask
the C compiler to additionally inline them, which may or may not present an
advantage.

Note that a cdef class instance (like any object) can only be allocated on
the heap. Stack allocation is not possible, for example.

Stefan

Nathann Cohen

unread,
Nov 12, 2012, 4:49:40 AM11/12/12
to cython...@googlegroups.com
Thaaaaaaaaaaaaaaaaaaaaaaanks !!!! :-)

Nathann

Kornél JAHN

unread,
Nov 12, 2012, 5:08:40 AM11/12/12
to cython...@googlegroups.com
Hello Stefan!

The user guide states here:
"Module level functions can now be declared inline, with the inline keyword passed on to the C compiler. These can be as fast as macros. [...]
Note that class-level cdef functions are handled via a virtual function table, so the compiler won’t be able to inline them in almost all cases."

Does your comment mean that cdef class methods can be inlined, too, if the cdef class is decorated with @cython.final? Hence the last sentence does not apply for this case?

I think this information is *very* valuable for those (like me), who need speed as well as keeping the code well-structured in cdef classes. It would be nice to include this information in the documentation (I could not find it anywhere).

Thanks,
Kornel
--
Kornél JAHN
engineering physicist, PhD student
Budapest University of Technology and Economics, Hungary

Robert Bradshaw

unread,
Nov 12, 2012, 2:34:41 PM11/12/12
to cython...@googlegroups.com
On Mon, Nov 12, 2012 at 2:08 AM, Kornél JAHN <korne...@gmail.com> wrote:
> Hello Stefan!
>
> The user guide states here:
> "Module level functions can now be declared inline, with the inline keyword
> passed on to the C compiler. These can be as fast as macros. [...]
> Note that class-level cdef functions are handled via a virtual function
> table, so the compiler won’t be able to inline them in almost all cases."
>
> Does your comment mean that cdef class methods can be inlined, too, if the
> cdef class is decorated with @cython.final? Hence the last sentence does not
> apply for this case?
>
> I think this information is *very* valuable for those (like me), who need
> speed as well as keeping the code well-structured in cdef classes. It would
> be nice to include this information in the documentation (I could not find
> it anywhere).

I agree, we'd welcome a patch to the documentation.

However, taking a step back (in response to the original post) I would
like to echo the old adage that the first step to optimization is to
profile, profile, profile and then optimize the critical parts.
Sometimes inlining methods can make a difference, but it's surprising
how many times it really isn't the part that matters.

Kornél JAHN

unread,
Nov 12, 2012, 2:44:12 PM11/12/12
to cython...@googlegroups.com
I'll second that. And also many thanks to all cython developers, it is an invaluable tool for me in my physical optics simulations. You're great.
Reply all
Reply to author
Forward
0 new messages