-
-
Notifications
You must be signed in to change notification settings - Fork 519
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Parents probably not reclaimed due to too much caching #715
Comments
comment:3
I think this is a bit too vague for a ticket. Robert, could you be more specific or close this? |
comment:4
The coercion model needs to use weakrefs so that parents aren't needlessly referenced when they're discarded. It is nontrivial to see where the weakrefs need to go, and how to do so without slowing the code down. The ticket is still valid. |
This comment has been minimized.
This comment has been minimized.
This comment has been minimized.
This comment has been minimized.
comment:7
With the piece of code in the desrciption, there is only one reference to these objects in that ZZ._hash_actions dictionary because to build it we test if A1 == A2 and not A1 is A2 as in coercion_model._action_maps, and because of the current implementation of ellitpic curves, see http://groups.google.com/group/sage-nt/browse_thread/thread/ec8d0ad14a819082 and #11474, and decause the above code use only one j-invariant, only ones gets finally stored. However with random curves, I guess there would be all of them. About the weakref, the idea should more be to build something like WeakKeyDictionnary if it does not slow down coercion too much... |
comment:8
The following example also exhibits a suspicious, steady growth in memory use. The only reason I can think of why that would happen is that references to the created finite field remain lying around somewhere, preventing deallocation:
If you change it to the creation of a polynomial ring the memory use rises much faster:
Are "unique" parents simply never deallocated? |
comment:9
Be aware that polynomial rings are also cached because of uniqueness of parents, explaining somehow your second memory consumption; see #5970 for example. For finite fields I did not check. |
comment:11
See #11521 for some concrete instances of this problem and some advice to investigate it. |
comment:12
In my code for the computation Ext algebras of basic algebras, I use letterplace algebras (see #7797), and they involve the creation of many polynomial rings. Only one of them is used at a time, so, the others could be garbage collected. But they aren't, and I suspect this is because of using strong references in the coercion cache. See the following example (using #7797)
That is certainly not a proof of my claim, but it indicates that it might be worth while to investigate. In order to facilitate work, I am providing some other tickets that may be related to this:
I guess that one should use a similar cache model to what I did in #11521: The key for the cache should not just be |
comment:13
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:
|
comment:14
It turns out that using weak references in the coercion cache will not be enough. Apparently there are other direct references that have to be dealt with. |
comment:15
I wonder whether the problem has already been solved. I just tested the example from the ticket description, and get (at least with #11900, #11521 and #11115):
I think that this is not particularly scary. I'll repeat the test with vanilla sage-4.8.alpha3, but this will take a while to rebuild. |
comment:16
No, even in vanilla sage-4.8.alpha3 I don't find a scary memory leak in this example. Do we have a better example? One could, of course, argue that one should use weak references for caching even if we do not find an apparent memory leak. I am preparing a patch for it now. |
comment:17
Here is an experimental patch. A new test shows that the weak caching actually works. Note that the patch also introduces a weak cache for polynomial rings, which might be better to put into #5970. Well, we can sort things out later... |
comment:18
It needs work, though. Some tests in sage/structure fail, partially because of pickling, partially because some tests do not follow the new specification of |
comment:19
Now I wonder: Should I try to use weak references and make it accept stuff that does not allow for weak references? In the intended applications, weak references are possible. But in some tests and in the pickle jar, the "wrong" type of keys (namely strings and ints) are used. |
comment:20
The only place where the weak references are created is in the |
comment:21
With the attached patch, all tests pass for me, and the new features are doctested. Needs review! |
Author: Simon King |
Changed keywords from none to weak cache coercion |
Dependencies: #11900 |
comment:360
I noticed that the |
comment:361
Hopefully the updated Cython 0.17.3 at #13832 might fix the last bugs we encounter. See some comments as well on testing Sage with a pydebug enable Python at #13864 and the long thread at |
comment:362
Please note #13870. |
comment:363
More bad news: #715 + #11521 cause a significant slowdown for the command
from 22 to 33 seconds. See https://groups.google.com/forum/?fromgroups#!topic/sage-devel/EzFPIG6EFMI |
comment:364
Without #715 and friends,
yields
With #715, it becomes
So, it seems to me that the slow-down is in the creation of homsets. First question: Why are so many homsets needed in this example? Second question: What can we do to make the creation of a homset more efficient? |
comment:365
Replying to @simon-king-jena:
By this, I mean "Why so many even with strong cache?" Note that #715 only involves a mild increase in the number of homsets created. But the time for creation increases dramatically. |
comment:366
To me it looks as if most of the extra time is in the symbolic gcd calls. But seriously, why on earth does potting a simple trig function involve anything as sophisticated as creating homsets? And why also are any gcds being computed? Is it that for each of the values t which are being iterated over, which are rational multiples of pi, the evaluation of sin(7t) and cos (30t) is being much too clever when all that is needed is a low-precision numerical value? |
comment:367
Replying to @JohnCremona:
And in particular: Why is
I don't know. But in any case, there is a regression in the time for creating a homset. I have opened #13922 for this problem. |
comment:368
Replying to @simon-king-jena:
PS: The category for this homset is always the same, namely the category of euclidean domains. It should definitely not happen that this homset is created more than once, even with a weak cache. |
Here is a small example illustrating the issue.
The memory footprint of the following piece of code grows indefinitely.
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.
To be merged with #11521. Apply:
and then the patches from #11521.
Depends on #13145
Depends on #13741
Depends on #13746
Depends on #11521
Dependencies: #13145, #13741, #13746, to be merged with #11521
CC: @jpflori @zimmermann6 @vbraun @robertwb @nbruin @malb @orlitzky
Component: coercion
Keywords: weak cache coercion Cernay2012
Author: Simon King, Jean-Pierre Flori
Reviewer: Jean-Pierre Flori, Simon King, Nils Bruin
Merged: sage-5.5.beta0
Issue created by migration from https://trac.sagemath.org/ticket/715
The text was updated successfully, but these errors were encountered: