#11115: Rewrite cached_method in Cython
---------------------------+------------------------------------------------
   Reporter:  SimonKing    |          Owner:  jason                
       Type:  enhancement  |         Status:  needs_info           
   Priority:  major        |      Milestone:  sage-4.7             
  Component:  misc         |       Keywords:  category cython cache
Work_issues:               |       Upstream:  N/A                  
   Reviewer:               |         Author:  Simon King           
     Merged:               |   Dependencies:                       
---------------------------+------------------------------------------------

Comment(by SimonKing):

 It occured to me that I did not yet provide an example to show cached
 methods for elements that do not allow attribute assignment:
 {{{
 sage: cython("""from sage.structure.element cimport Element
 cdef class MyElement(Element):
     cdef object x
     def __init__(self,P,x):
         self.x=x
         Element.__init__(self,P)
     def __neg__(self):
         return MyElement(self.parent(),-self.x)
     def _repr_(self):
         return '<%s>'%self.x
     def raw_test(self):
         return -self
 """)
 sage: class MyCategory(Category):
     @cached_method
     def super_categories(self):
         return [Objects()]
     class ElementMethods:
         @cached_method
         def test_cache(self):
             return -self
 sage: class MyParent(Parent): pass
 sage: C = MyCategory()
 sage: P = MyParent(category=C)
 sage: e = MyElement(P,5)
 sage: e
 <5>
 sage: -e
 <-5>
 # This is inherited from the category, ...
 sage: e.test_cache()
 <-5>
 # ... and the cache works, ...
 sage: e.test_cache() is e.test_cache()
 True
 # ... even though you can't set attributes:
 sage: e.bla = 1
 Traceback (most recent call last):
 ...
 AttributeError:
 '_mnt_local_king__sage_temp_mpc622_25602_tmp_1_spyx_0.MyElement' object
 has no attribute 'bla'
 # By lack of "setattr", the cache is not as fast as it could be, ...
 sage: timeit('e.test_cache()')
 625 loops, best of 3: 1.62 µs per loop
 # ... but it is faster than to create a new element
 sage: timeit('e.raw_test()')
 625 loops, best of 3: 1.78 µs per loop
 }}}

 At sage-devel, Martin Raum suggested to time element creation (if I
 understood his suggestion correctly).

 I found without patch:
 {{{
 sage: K = GF(101)
 sage: get_memory_usage()
 839.96484375
 sage: %time for i in xrange(10^7): a = K(i)
 CPU times: user 25.19 s, sys: 0.01 s, total: 25.21 s
 Wall time: 25.20 s
 sage: get_memory_usage()
 839.96484375
 }}}
 and with patch
 {{{
 sage: K = GF(101)
 sage: get_memory_usage()
 841.96875
 # show that the new attribute is there, but it is non-initialized
 sage: a=K(4)
 sage: print a.__cached_methods
 None
 sage: %time for i in xrange(10^7): a = K(i)
 CPU times: user 24.46 s, sys: 0.00 s, total: 24.46 s
 Wall time: 24.46 s
 sage: get_memory_usage()
 841.96875
 }}}

 So the time actually improved with the patch, and the memory usage at
 starting sage increased by as much as 0.24% - I hope that can be afforded.

-- 
Ticket URL: <http://trac.sagemath.org/sage_trac/ticket/11115#comment:24>
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 post to this group, send email to [email protected].
To unsubscribe from this group, send email to 
[email protected].
For more options, visit this group at 
http://groups.google.com/group/sage-trac?hl=en.

Reply via email to