#14214: Cythoned homsets
----------------------------------------------+-----------------------------
       Reporter:  SimonKing                   |         Owner:  tbd             
                 
           Type:  enhancement                 |        Status:  needs_work      
                 
       Priority:  major                       |     Milestone:  sage-5.10       
                 
      Component:  performance                 |    Resolution:                  
                 
       Keywords:  Hom, cython, cached method  |   Work issues:  How to store 
domain and codomain?
Report Upstream:  N/A                         |     Reviewers:                  
                 
        Authors:  Simon King                  |     Merged in:                  
                 
   Dependencies:  #14159, #12951, #13184      |      Stopgaps:                  
                 
----------------------------------------------+-----------------------------

Comment (by nbruin):

 I think you want the following code:
 {{{
 from sage.all import Sets
 from sage.structure.parent cimport Parent

 cdef class CachedAttributeType(object):
     cdef public str name
     def __init__(self, name):
         self.name = name
     def __get__(self,instance,cls):
         try:
             return instance.__cached_methods[self.name]
         except KeyError:
             raise AttributeError("%s instance has no attribute
 '%s'"%(cls,self.name))

 cdef class Bla(Parent):
     def __init__(self, D,C):
         try:
             self.__dict__['_domain'] = D
             self.__dict__['_codomain'] = C
         except AttributeError:
             if self.__cached_methods is None:
                 self.__cached_methods = {'_domain':D, '_codomain':C}
             else:
                 self.__cached_methods['_domain'] = D
                 self.__cached_methods['_codomain'] = C
         Parent.__init__(self, category = Sets())
     _domain=CachedAttributeType("_domain")
     _codomain=CachedAttributeType("_codomain")
 }}}
 Timings we'll be getting with the cython upgrade (with Python's attribute
 lookup cache enabled)
 {{{
 sage: b=Bla("dommi","codommi")
 sage: b._domain
 'dommi'
 sage: %timeit b._domain
 10000000 loops, best of 3: 97.6 ns per loop
 sage: c=Cla("dommi","codommi")
 sage: %timeit c._domain
 10000000 loops, best of 3: 50 ns per loop
 }}}
 With the old cython (where MRO depth is a big factor in attribute lookup):
 {{{
 sage: %timeit b._domain
 10000000 loops, best of 3: 130 ns per loop
 sage: %timeit c._domain
 10000000 loops, best of 3: 78.5 ns per loop
 }}}

 What we should really do is get rid of `__cached_methods` make sure these
 objects have a `__dict__` instead, where such information can be stored
 much more effectively. There is an "empty dict" value one can use as a
 place holder for empty dictionaries. There's really no good reason why
 cython types cannot have a dictionary.

-- 
Ticket URL: <http://trac.sagemath.org/sage_trac/ticket/14214#comment:40>
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