On Mon, Jul 26, 2010 at 11:36 PM, Carl Witty <[email protected]> wrote:
> I just posted a patch on #561 that greatly speeds up the case when you
> define a special function in a cdef class and then inherit in a
> non-cdef class.  Consider the following code:
>
> cdef class NullAdder:
>    def __add__(self, other):
>        return self
>
> class NullAdder2(NullAdder):
>    pass
>
> Before the patch, I got the following timings:
>
> sage: timeit('na + na', number=10^5, repeat=30)
> 100000 loops, best of 30: 94.3 ns per loop
> sage: na2 = NullAdder2()
> sage: timeit('na2 + na2', number=10^5, repeat=30)
> 100000 loops, best of 30: 261 ns per loop
>
> so being in a subclass had 170ns of overhead.  (The overhead depends
> on the method; the overhead for __getattr__ is more like 700ns on the
> same computer.)
>
> After the patch, there is no penalty for the subclass:
>
> sage: timeit('na + na', number=10^5, repeat=30)
> 100000 loops, best of 30: 92 ns per loop
> sage: na2 = NullAdder2()
> sage: timeit('na2 + na2', number=10^5, repeat=30)
> 100000 loops, best of 30: 92.9 ns per loop

Very, very cool. I've actually been thinking a lot about this myself
(given the recent thread in sage-devel). I think one reason we did our
own wrappers was for introspection/docstrings--does that break? Did
you fix the most egregious __getattr__?

> More details are on the ticket.
>
> I tested the patch with the Cython test suite (against 0.12.1,
> 0.13.beta0, and current mercurial cython-devel), and by rebuilding
> Sage 4.5.1 with the modified version and running its test suite.  All
> tests passed.
>
> One not-quite-a-microbenchmark got 20% faster in Sage with this patch
> (adding QQbar(0) to itself went from about 26.5 microseconds to about
> 21 microseconds).
>
> Given the potentially large performance benefit, I think you should
> consider this patch (or something like it that's perhaps a little
> cleaner) for 0.13.

It's certainly worth considering!

- Robert
_______________________________________________
Cython-dev mailing list
[email protected]
http://codespeak.net/mailman/listinfo/cython-dev

Reply via email to