On Wed, Feb 11, 2015 at 3:15 AM, Marc Mezzarobba <m...@mezzarobba.net> wrote:
> Volker Braun wrote:
>> My advice would be to implement the parent in Python and benchnmark
>> it. If you are doing something in _element_constructor_ that would
>> benefit greatly by Cython then you can just move that part to Cython.
>
> Apparently my question was unclear. Of course, it is good advice as a
> general rule decide based on profiling results. But: _assuming_ not only
> that parts of what _element_constructor_ does benefit from Cython, but
> also that the cost of issuing a Python method call when constructing
> elements is significant, what is the best approach to deal with it?

Let MyParent be defined in my_parent.py, and MyElement be defined in
my_element.pyx.

Say your parent doesn't override __call__ (which it shouldn't), in
which case you'll inherit the Cythonized Parent.__call__. When you
write MyParent(x, *args, **kwds) this will finds/creates the
appropriate morphism parent(x) -> MyParent. By default, this invokes
MyParent._element_constructor_(x, *args, **kwds) as fast as possible
(it's actually cached, so there's not even a dict lookup there). Now
MyParent._element_constructor_ need not be defined in my_parent.py, in
fact it's often MyElement's constructor.

> Or, to put it differently: why are so many parents Cython classes if
> there are no benefits?

Some Parent have methods and/or data that is best implemented in
Cython. Others are like that for historical reasons and/or incomplete
understanding. As Volker said

On Wed, Feb 11, 2015 at 7:07 AM, Volker Braun <vbraun.n...@gmail.com> wrote:

> Some of the basic parent/elements in Sage are extremely optimized and not a
> good model for getting started with your own structure.

I would go further and say they're not even all good models for how to
make things fast; a lot of early parents pre-date Cython itself, let
alone the coercion model, etc.

-- 
You received this message because you are subscribed to the Google Groups 
"sage-devel" group.
To unsubscribe from this group and stop receiving emails from it, send an email 
to sage-devel+unsubscr...@googlegroups.com.
To post to this group, send email to sage-devel@googlegroups.com.
Visit this group at http://groups.google.com/group/sage-devel.
For more options, visit https://groups.google.com/d/optout.

Reply via email to