#12941: Cache should be cleared on cloning
----------------------------------------+-----------------------------------
       Reporter:  hivert                |         Owner:  hivert  
           Type:  defect                |        Status:  new     
       Priority:  major                 |     Milestone:  sage-5.1
      Component:  combinatorics         |    Resolution:          
       Keywords:  Clone, cache, days38  |   Work issues:          
Report Upstream:  N/A                   |     Reviewers:          
        Authors:  Florent Hivert        |     Merged in:          
   Dependencies:                        |      Stopgaps:          
----------------------------------------+-----------------------------------

Comment (by SimonKing):

 Replying to [comment:14 hivert]:
 > - I'm Ok to implement a ``copyAndClearTheCache`` at the level of
 ``Element``
 >   or ``ClonableElement``.  Any support from the cache mechanism which
 makes it
 >   as fast as possible is welcome.

 I hope that the `clear_cache` method of cached methods is quick enough:
 {{{
 sage: class A:
 ....:     @cached_method
 ....:     def f(self):
 ....:         return 5
 ....:     @cached_method
 ....:     def g(self,i,n=4):
 ....:         return i**n
 ....:
 sage: a = A()
 sage: %timeit a.f.clear_cache()
 625 loops, best of 3: 182 ns per loop
 sage: %timeit a.g.clear_cache()
 625 loops, best of 3: 328 ns per loop
 }}}

 > - On the second stage I don't want to clear the cache upon any
 modification as
 >   they are supposed to be elementary (eg: modifying an array entry). As
 a
 >   consequence, I'd like to have a hook which **deactivate any caching on
 >   mutable objects**. Caching will be be reactivated once the object is
 set
 >   immutable. Do you think that's feasible on the ``CachedMethod`` side
 while
 >   keeping the overhead as low as possible ?

 One could certainly create a new decorator, say, @cache_if_immutable, that
 only writes to or reads from the cache if the object is immutable. I have
 no idea of the overhead (at least, one would have one call to
 _is_immutable() per access).

 > Currently all sage mutable objects including not only clones but
 matrices and
 > vectors have a method ``_is_immutable``. Vectors and matrices implements
 > their own caching mechanism, but I think having cached method work with
 them
 > would be good.

 Technical difficulty: cached_method requires that either the object allows
 to set new attributes, or it has an attribute `__cached_methods` which is
 a dictionary. The latter exists for sage.structure.parent.Parent, by
 #11115.

 Now the problem is that vectors (such as created by `vector(CC, [1,2,3])`)
 do ''not'' support adding attributes, and they do not have
 `__cached_methods`. By consequence, @cached_method will not work for them.

 A possible solution would be to add `cpdef dict __cached_methods` to
 vectors. But then, the next complication arises:
 {{{
 sage: cython("""
 ....: from sage.structure.parent cimport Parent
 ....: cdef class MyParent(Parent):
 ....:     def f(self):
 ....:         return 5
 ....: """)
 sage: MyParent.f.__module__
 ---------------------------------------------------------------------------
 AttributeError                            Traceback (most recent call
 last)

 /home/simon/SAGE/work/pathalgebras/<ipython console> in <module>()

 AttributeError: 'method_descriptor' object has no attribute '__module__'
 }}}

 Unfortunately, the `__module__` attribute is read when a cached method is
 defined. Hence, although cached_method does work for a python class
 defined in a cython module, it does not (yet) work on a cdef class.

 But one could try to work around:
 {{{
 sage: MyParent.f.__objclass__.__module__
 '_home_simon__sage_temp_linux_sqwp_site_4700_tmp_2_spyx_0'
 }}}

 So, I guess I'll open another cached_method ticket!

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