#14058: Weakly reference binary operation codomains
-----------------------------------+----------------------------------------
       Reporter:  robertwb         |         Owner:  rlm         
           Type:  enhancement      |        Status:  needs_review
       Priority:  major            |     Milestone:  sage-5.7    
      Component:  memleak          |    Resolution:              
       Keywords:                   |   Work issues:              
Report Upstream:  N/A              |     Reviewers:              
        Authors:  Robert Bradshaw  |     Merged in:              
   Dependencies:  #12313           |      Stopgaps:              
-----------------------------------+----------------------------------------

Comment (by SimonKing):

 In vanilla Sage, the coercion model stores (coercion) morphisms in its
 cache (which was a `TripleDict`, hence, with weak keys), and the only
 change introduced by your patch is to store weak references to the
 (coercion) morphism instead. Did I understand this correctly?

 In vanilla Sage, the coercion model kept a strong reference to the
 morphism, which kept a strong reference to domain and codomain, which were
 thus not reclaimable, and so the item of the `TripleDict` has eternal
 life, in spite of the weak keys. Correct?

 With the patch, the coercion model keeps a weak reference to the coercion
 morphism, hence, the strong reference from the morphism to domain and
 codomain doesn't matter, and thus the item of the `TripleDict` may
 disappear.

 But how is the morphism kept alive? The coercion model only has a weak
 reference to it. Do I understand correctly that the morphism involved in
 the binary operation is, at the same time, stored in the coerce dict of
 its codomain? That's why it does not immediately die?

 In other words, do I understand that the layout is as follows? We have
 parents A and B, and want to apply some binary operator, with a result in
 the parent C (C may coincide with either A or B). And we have maps r_A and
 r_B from A or B to C.

 Both r_A and r_B are stored with a strong reference in a cache located in
 C, with weak keys A and B. At the same time, they are stored with a weak
 reference in the coercion model, again with weak keys A and B. r_A has
 strong references to C and to A, r_B has strong references to C and to B.

 What do we want? Do we want that keeping C alive makes A and B survive? Or
 do we want that keeping both A and B makes C survive?

 If the user has a strong reference to C, then C has a strong reference to
 r_A and r_B (in its coerce cache), and r_A/r_B strongly refers to A/B.
 Hence, the existence of C keeps A and B alive. Since C is a complicated
 object, it is more likely to be mortal, hence, probably it is not much of
 a problem.

 If the user has a strong reference to both A and B, then C keeps a strong
 reference to both r_A and r_B, and the coercion model keeps a weak
 reference to r_A and r_B. Moreover, r_A and r_B strongly refer to C.

 However, isn't it just a reference cycle between C and r_A/r_B? It would
 not prevent C from becoming collectable, right?

 I doubt that we want that. It is not desirable that, when adding elements
 of ZZ['x'] and QQ, the ring QQ['x'] is created repeatedly (OK, polynomial
 rings have a strong cache anyway. But you see what I mean).

 Or did I misunderstand something?

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