#715: Parents probably not reclaimed due to too much caching
--------------------------------+-------------------------------------------
   Reporter:  robertwb          |          Owner:  somebody           
       Type:  defect            |         Status:  needs_work         
   Priority:  major             |      Milestone:  sage-4.8           
  Component:  coercion          |       Keywords:  weak cache coercion
Work_issues:  avoid regression  |       Upstream:  N/A                
   Reviewer:                    |         Author:  Simon King         
     Merged:                    |   Dependencies:  #9138, #11900      
--------------------------------+-------------------------------------------

Old description:

> Here is a small example illustrating the issue.
>
> The memory footprint of the following piece of code grows indefinitely.
>
> {{{
> sage: K = GF(1<<55,'t')
> sage: a = K.random_element()
> sage: while 1:
> ....:     E = EllipticCurve(j=a); P = E.random_point(); 2*P; del E, P;
>
> }}}
> E and P get deleted, but when 2*P is computed, the action of integers on
> A, the abelian group of rational points of the ellitpic curve, gets
> cached in the corecion model.
>
> A key-value pair is left in coercion_model._action_maps dict:
>
> (ZZ,A,*) : IntegerMulAction
>
> Moreover there is at least also references to A in the IntegerMulAction
> and one in ZZ._action_hash.
>
> So weak refs should be used in all these places if it does not slow
> things too much.
>
> Apply:
>
> [attachment:trac715_two_tripledicts.patch]

New description:

 Here is a small example illustrating the issue.

 The memory footprint of the following piece of code grows indefinitely.

 {{{
 sage: K = GF(1<<55,'t')
 sage: a = K.random_element()
 sage: while 1:
 ....:     E = EllipticCurve(j=a); P = E.random_point(); 2*P; del E, P;

 }}}
 E and P get deleted, but when 2*P is computed, the action of integers on
 A, the abelian group of rational points of the ellitpic curve, gets cached
 in the corecion model.

 A key-value pair is left in coercion_model._action_maps dict:

 (ZZ,A,*) : IntegerMulAction

 Moreover there is at least also references to A in the IntegerMulAction
 and one in ZZ._action_hash.

 So weak refs should be used in all these places if it does not slow things
 too much.

 Apply:

  * [attachment:trac715_two_tripledicts.patch]
  * [attachment:trac715_weak_action.patch]

--

Comment(by SimonKing):

 I have attached another patch, which implements the ideas sketched above.
 I think it corresponds to what you suggested ("use a weak reference from
 the action to the domain").

 One detail: We have to distinguish between the underlying set, the domain
 and the codomain of an action. In fact, the new patch only uses a weak
 reference to the underlying set, and introduces a cdef function (hence,
 hopefully with little overhead) returning it.

 I consider sage-5.0.prealpha0 plus
 trac11780_unique_auxiliar_polyring.patch (probably not needed) plus
 [attachment:trac715_two_tripledicts.patch] plus
 [attachment:trac715_weak_action.patch].

 At least `sage -t sage/modular/modsym/space.py` passes, but I need to run
 the whole test suite.

 The example from the ticket description does not leak. However, if the
 j-invariant varies, it seems that for each elliptic curve one copy is
 preserved:
 {{{
 sage: K = GF(1<<55,'t')
 sage: for i in range(500):
 ....:     a = K.random_element()
 ....:     E = EllipticCurve(j=a)
 ....:     P = E.random_point()
 ....:     Q = 2*P
 ....:
 sage: import gc
 sage: gc.collect()
 2124
 sage: from sage.schemes.generic.homset import
 SchemeHomsetModule_abelian_variety_coordinates_field
 sage: LE = [x for x in gc.get_objects() if
 isinstance(x,SchemeHomsetModule_abelian_variety_coordinates_field)]
 sage: len(LE)
 500
 }}}

 In any case, the original leak is fixed with the two patches. The question
 is whether the second patch suffices to keep actions alive, whether it
 avoids a regression, and whether all tests pass.

 If everything is alright, we may still try to find out where the remaining
 strong reference to an elliptic curve comes from.

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