#14214: Cythoned homsets
----------------------------------------------+-----------------------------
       Reporter:  SimonKing                   |         Owner:  tbd     
           Type:  enhancement                 |        Status:  new     
       Priority:  major                       |     Milestone:  sage-5.8
      Component:  performance                 |    Resolution:          
       Keywords:  Hom, cython, cached method  |   Work issues:          
Report Upstream:  N/A                         |     Reviewers:          
        Authors:  Simon King                  |     Merged in:          
   Dependencies:  #14159, #12951              |      Stopgaps:          
----------------------------------------------+-----------------------------

Comment (by SimonKing):

 Ideally, sage.categories.homset.Homset should be a cdef class, with cdef
 attributes _domain, _codomain, `__category`.

 __Problem and potential long term solutions__

 `sage.modular.abvar.homspace.EndomorphimSubring` inherits from
 sage.categories.homset.Homset and from sage.rings.ring.Ring. Making Homset
 cdef with cdef attributes results in incompatible base classes.

 - One could think of creating a new cdef class
 `sage.categories.homset.EndomorphismRing` inheriting from
 sage.rings.ring.Ring and copying the methods implemented in
 sage.categories.homset.Homset, and use it in sage.modular.abvar.homspace.
   - However, that's code duplication, and I doubt that we want that.
 - One could think of dropping `sage.rings.ring.Ring` and let
 `EndomorphismSubring` entirely rely on the ring functionality that is
 provided by the category framework.
   - I tried, but this means opening a can of worms. One would then also
 need to implement the new coercion model in sage.modular. But replacing
 `EndomorphismSubring.__call__` by an _element_constructor_ does not work,
 because Homset has a `__call__` method, too. Hence, we would then also
 need to use the new coercion model for sage.categories.homset. Yes, that's
 desirable. But it is not available in short time.
 - One could try to make some important basic methods of Homset fast, by
 inheritance from `WithEqualityById` that I introduced in #14054. It would
 be desirable that two homsets are equal if and only if they are identical
 if and only if both domain, codomain and category are identical.
   - Unfortunately, the old parts of sage (about elliptic curves and stuff)
 rely on the assumption that we do ''not'' have unique parents and do
 ''not'' have unique homsets. This would be another can of worms.

 __Short term improvements suggested in my patch__

 - Changing sage.categories.homset into a Cython module means: We can cdef
 the homset cache, and can thus use fast get/set methods for `TripleDict`.
 That's a speed-up.
 - One can make Homset cdef, but keep the _domain and _codomain attributes
 Python attributes. Then, there are no layout conflicts.
 - I suggest to change `__category` into a single underscore attribute, to
 avoid name mangling.
 - Accessing a Python attribute _domain defined for a Homset can be
 surprisingly slow. It is fast, if one has an instance of Homset itself.
 But it is slow if one considers an instance of a sub-class, such as
 `EndomorphismSubring`. It turns out that calling a cached method is
 ''faster'' than accessing the attribute! Hence, I suggest using cached
 method decorators on the domain() and codomain() methods (these methods
 are internally used in `EndomorphismSubring` anyway).
 - As a step towards uniqueness, I suggest that `Homset.__cmp__` tests by
 identity rather than equality, and uses `cmp` only on non-identical
 domain/codomain/category.

 Making Homset cdef requires to put `Set_generic` into
 sage.structure.parent.pxd.

 Timings will be in the next post.

-- 
Ticket URL: <http://trac.sagemath.org/sage_trac/ticket/14214#comment:1>
Sage <http://www.sagemath.org>
Sage: Creating a Viable Open Source Alternative to Magma, Maple, Mathematica, 
and MATLAB

-- 
You received this message because you are subscribed to the Google Groups 
"sage-trac" group.
To unsubscribe from this group and stop receiving emails from it, send an email 
to [email protected].
To post to this group, send email to [email protected].
Visit this group at http://groups.google.com/group/sage-trac?hl=en.
For more options, visit https://groups.google.com/groups/opt_out.


Reply via email to