On Jan 31, 6:49 am, "Nicolas M. Thiery" <nicolas.thi...@u-psud.fr>
wrote:

> Shall we ask the Cython devs to implement the __classcall__ protocol?
> Then we could merge the factory function and the class in Cython
> classes as is done by UniqueRepresentation.

I guess that could be quite doable for "python" style classes that
have their attributes in a dynamic dictionary (it probably doesn't
need any extra work at all!), but for cdef extension classes that
needs some serious redesigning. As I understand it at the moment,
"instance slot variables" (i.e., the cdeffed instance variables) are
simply allocated contiguously. That means the actual extent of the
memory allocation needed for an instance is only known by the lowest
down class (and by avoiding multiple inheritance, the memory layout is
also unambiguous).

Suppose you have

cdef class A(...):
   def __classcall__(...):
      ...

cdef class B(A):
      ...

cdef class C(A):
      ...

If you let A.__classcall__ interfere with what gets returned by
b=B(...) and c=C(...) then A.__classcall__ would somehow need to know
whether it needs to ensure it returns an object with memory layout
(and initialization!) compatible with B or with C.

Particularly if A=UniqueRepresentation you'd need somehow make
absolutely sure that b=B(...) either triggers normal creation of a B
instance or that it triggers (via A.__classcall__ ?) the return of a
fully initialized B instance. Since c=C(...) would have to do the
same, you see that A.__classcall__ would have to somehow know what to
instantiate.

Note that "cdef B b=B(...)" provides cython with static type
information that allows it to compile the assumptions about the B
memory layout into the code. So somehow the A.__classcall__ mechanism
would have to resolve type ambiguities at compile time. It might well
be possible to do that, but it seems to me the mechanism would have to
be quite different from what python does (since that leads to full
runtime dynamism). It sounds like a nice challenge for language
designers but perhaps less attractive for something we need to be
absolutely rock solid. Probably once you start designing something
that meets the requirements here, you end up with something like the C+
+ templating system (and above all, the distinction between templates
and classes).

What should be quite doable (and I think this is what Simon is trying)
is to make UniqueRepresentation a normal python class, but where all
methods are cython implemented. I think that's what a "class ..." in a
".pyx" file would do. From his initial reports it seems that
eliminating interpreter overhead, while (necessarily) keeping all the
overhead in looking up attributes via the usual python protocols at
runtime in __dict__ etc. still leads to a (moderate) speed increase.

-- 
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?hl=en.
For more options, visit https://groups.google.com/groups/opt_out.


Reply via email to