#715: Parents probably not reclaimed due to too much caching
-------------------------------------------------------------------+--------
       Reporter:  robertwb                                         |         
Owner:  somebody                     
           Type:  defect                                           |        
Status:  needs_review                 
       Priority:  major                                            |     
Milestone:  sage-pending                 
      Component:  coercion                                         |    
Resolution:                               
       Keywords:  weak cache coercion Cernay2012                   |   Work 
issues:                               
Report Upstream:  N/A                                              |     
Reviewers:  Jean-Pierre Flori, Simon King
        Authors:  Simon King, Jean-Pierre Flori                    |     Merged 
in:                               
   Dependencies:  #9138, #11900, #11599, to be merged with #11521  |      
Stopgaps:                               
-------------------------------------------------------------------+--------

Comment (by nbruin):

 Excellent! Thank you for the great work. This is incredibly important for
 so
 many parts of sage.

 > Previously, there was a global dictionary, that was shared by all
 > `TripleDicts`. That probably was a bad idea, for the reasons you pointed
 out.
 > Now, the references are stored in a dictionary that is an attribute of
 each
 > `TripleDict`.

 Excellent! I agree with your assessment. I think this addresses all my
 concerns.
 I think this is a useful data structure in general, so can we formalize
 its
 behaviour in the documentation? (rewrite as you see fit)

 {{{
 TripleDict is a structure like WeakKeyDictionary, optimized for lookup
 speed.
 Keys consist of a triple (k1,k2,k3) and are looked up by identity rather
 than
 equality. The keys are stored by weakrefs if possible. If any one of the
 components k1,k2,k3 gets garbage collected, then the entry is removed from
 the
 TripleDict. Key components that do not allow for weakrefs are stored via a
 normal refcounted reference. That means that any entry stored using a
 triple
 (k1,k2,k3) with none of the k1,k2,k3 weakreffable behaves as an entry in a
 normal dictionary, so its existence in TripleDict prevents it from being
 garbage
 collected.
 }}}

 > Another advantage: If the `TripleDict` is deallocated, then the strong
 > references associated with the `TripleDict` will vanish as well, which
 wouldn't
 > have been the case with the old code.

 AND if an entry gets deleted/garbage collected due to a weakreffed key
 component
 disappearing, we also deref any strongly reffed key components! So I think
 we
 never behave worse than a normal dict in terms of keeping objects alive.

 > Currently, there is only one bad situation I can think of: Let P be an
 object
 > that can not be weak-refed, has a `TripleDict` T as an attribute, is
 used as a
 > key in T, and has a `__del__` method. Then the reference cycle
 > P->T->T._refcache->P will keep P alive.

 I think we're safe for that. There are very few `__del__` definitions in
 the
 sage library and they're all associated with interface-type objects. And
 those
 are plain python classes anyway, so they are weakreffable.

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