#715: Parents probably not reclaimed due to too much caching
------------------------+---------------------------------------------------
   Reporter:  robertwb  |          Owner:  somebody
       Type:  defect    |         Status:  new     
   Priority:  major     |      Milestone:  sage-4.8
  Component:  coercion  |       Keywords:          
Work_issues:            |       Upstream:  N/A     
   Reviewer:            |         Author:          
     Merged:            |   Dependencies:          
------------------------+---------------------------------------------------

Comment(by SimonKing):

 I try to wrap my mind around weak references. I found that when creating a
 weak reference, one can also provide a method that is called when the weak
 reference becomes invalid. I propose to use such method to erase the
 deleted object from the cache, regardless whether it appears as domain or
 codomain.

 Here is a proof of concept:
 {{{
 sage: ref = weakref.ref
 sage: D = {}
 sage: def remove(x):
 ....:     for a,b,c in D.keys():
 ....:         if a is x or b is x or c is x:
 ....:             D.__delitem__((a,b,c))
 ....:
 sage: class A:
 ....:     def __init__(self,x):
 ....:         self.x = x
 ....:     def __repr__(self):
 ....:         return str(self.x)
 ....:     def __del__(self):
 ....:         print "deleting",self.x
 ....:
 sage: a = A(5)
 sage: b = A(6)
 sage: r = ref(a,remove)
 sage: s = ref(b,remove)
 sage: D[r,r,s] = 1
 sage: D[s,r,s] = 2
 sage: D[s,s,s] = 3
 sage: D[s,s,1] = 4
 sage: D[r,s,1] = 5
 sage: D.values()
 [5, 3, 1, 4, 2]
 sage: del a
 deleting 5
 sage: D.values()
 [4, 3]
 sage: del b
 deleting 6
 sage: D.values()
 []
 }}}

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