@cython.final optimization?

35 views
Skip to first unread message

Brock Mendel

unread,
Nov 23, 2017, 11:10:44 AM11/23/17
to cython-users
The docs say* that `@cython.final` applies a small optimization in cases where it is applicable.  I applied it to _Timestamp.__add__ and _Timestamp.__sub__ here but am not seeing a measurable difference.  So two questions:

1) Are there more specific conditions under which this should give an optimization?
2) Ballpark for the size of the optimization?  Should I aim for a (much) larger sample size to measure a difference?

* Well, I thought it did.  Not seeing it in the docs now.  This could be a complete non-issue.

Stefan Behnel

unread,
Nov 24, 2017, 2:37:42 AM11/24/17
to cython...@googlegroups.com
Brock Mendel schrieb am 23.11.2017 um 17:10:
> The docs say* that `@cython.final` applies a small optimization in cases
> where it is applicable. I applied it to _Timestamp.__add__ and
> _Timestamp.__sub__ here
> <https://github.com/pandas-dev/pandas/pull/18446/files#diff-231ac35d2116a12844a7cfed02730580R215> but
> am not seeing a measurable difference. So two questions:
>
> 1) Are there more specific conditions under which this should give an
> optimization?

It does not currently apply to special methods, which are already faster
than normal Python methods. It would be possible to further speed up
operator usage for final classes here, though, as the lookup currently goes
through a pointer indirection, which still leads to less efficient code
than when telling the C compiler explicitly which exact C function we are
calling, especially because it prevents inlining.



> 2) Ballpark for the size of the optimization?

Depends on your code, as always, but a big difference compared to Python
method calls, and small compared to c(p)def methods and special methods.

Stefan

Brock Mendel

unread,
Nov 24, 2017, 2:45:17 PM11/24/17
to cython-users
Good to know.  Thanks for walking me through this stuff.

I might be able to contribute to the docs just by writing up all the stuff you've explained to me.
Reply all
Reply to author
Forward
0 new messages