-
-
Notifications
You must be signed in to change notification settings - Fork 480
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
Fix yet another memory leak caused by caching of coercion data #12313
Comments
comment:1
Memory leaks are quite tricky!! The post here is mainly a memo for myself, documenting what went wrong in the last couple of days. I tried to directly modify my code from #715. The approach was: And what happened when I replaced
I think the best place to break the reference chain is from the Eraser to the |
comment:2
Replying to @simon-king-jena:
Nope! There, the situation was different, since there is strong reference to |
This comment has been minimized.
This comment has been minimized.
comment:4
With the attached patch (to be applied after #715), a new kind of weak dictionary is introduced, and used to cache coercions and conversions. All tests pass for me. Concerning raw speed: Without the patch, we have
With the patch, it is 466 ns, hence, one gains 10% in an operation that is very frequent. The total speed seems to be fine as well, Concerning memory leaks:
That is a new doctest and tests against a memory leak that has not been fixed by #715. |
Author: Simon King |
comment:5
I have extended my patch by another coercion-related application of I found that The new patch version is overriding the Still needs review. |
comment:6
Could you add the second problem fixed into the ticket description ? Maybe this should be splitted into two tickets to be more readable, or at least two patches. The title is a little vague as well :) Sorry for the delay for this and #715 and #11521 but this is now on my top priority (for Sage). |
comment:7
And what if a user directly use a Groupoid object ? Will the underlying set be garbage collected as in #715 for Actions? Another reason to split this ticket in two (once again :) ) |
comment:8
To finish my thoughts for now, we could as well leave the tickets as they are (except for some more doc and easy fixing) and potentially implement the other behavior (where a choice is possible between strong and weak refs) in a later ticket. The current behavior with the error raising (if it gets a description of how not to get garbage collected in addition to telling that it got garbage collected) seems viable enough to me (unless that many people do actually use Groupoids and Actions directly). |
comment:9
Perhaps you are right, and we could re-organise stuff. Namely, for now (that's to say: for #715 and for #12313) we could keep strong references from an action to the underlying set, and from a groupoid to the underlying set. The unfortunate consequence would be that not all memory leaks would be fixed. But one could create a new ticket where the idea of having weak references to the underlying set is tested. And here is yet another idea, at least for the groupoid case. Let S be any object and G its groupoid. Ideally, S should keep G alive and G should keep S alive. Hence, it would be fine to have a strong reference from S to G and from G to S -- Python's garbage collection can deal with circular references. However, there should be no strong references to S or G, unless the user explicitly provides it. In particular, when creating Potential solution: The cache for the
Note that the problem can hardly be solved in the same way for actions. They need to be stored externally, namely in the coercion model. |
Work Issues: Split ticket |
comment:10
I think it is best to follow your suggestion and split stuff. Soon (perhaps tomorrow) I'll return to the old patch, i.e. without touching the groupoid stuff. In a to-be-created ticket, I'll try the idea for caching groupoids that avoids memory leaks but uses strong references, as explained in my previous post. However, I think the weak references to the underlying set of actions is an integral part of #715 - without it, the memory leak would not be fixed at all; so, I'd not split #715. |
comment:11
Python's name mangling sucks. Name mangling occurs when an attribute is requested whose name starts with double underscores and does not end with an underscore. Thus, it happens when However, for a reason that I don't understand, Python does not use the name of the class of S for mangling, but the name of cls -- for example, if I'll see if doing things in Cython helps. |
Weak coerce map dictionary with weakly referenced unique keys and optimised access speed |
Changed work issues from Split ticket to none |
comment:12
Attachment: trac12313_mono_dict.patch.gz I have updated my patch, so that groupoids are not touched any more. Needs review, again. Groupoids are now dealt with at #12357 (needs review), and I think the solution provided there is much better than the improper use of weak references. |
Work Issues: Find out why some attribute of a parent is deleted before the parent's elements are deleted |
comment:13
I am not totally sure whether the following problem is caused by this patch or by #715 or #11521. Anyway, with all three patches applied, I obtain:
As you can observe, some attribute error is ignored, but worse: Cython is unable to tell where it is ignored. The cython-users helped me to track the problem down, using gdb. The attribute error occurs in the following cdef method defined in sage/rings/polynomial/polynomial_zz_pex.pyx:
By the way it is defined, the error is propagated. However, get_cparent is in fact called in a deallocation method, namely in sage/rings/polynomial/polynomial_template.pxi:
That is interesting for two reasons: (1) Cython can not tell the location of the error, which I think is a bug in Cython. (2) The attribute _modulus of I find the latter rather strange. As long as self is alive, |
comment:328
Could it be that #12313 is not in sage-5.8.beta0? The version that I have downloaded before the official announcement does contain it. Anyway, let's try: Apply trac_12313-mono_dict-combined-random-sk.patch trac_12313-revert_callback_from_11521.patch to sage-5.8.beta0 |
comment:329
Why add that patch here when it belongs on #14159? This ticket already has positive review. It's bad form to start changing the code unless absolutely necessary and that new patch has nothing to do with this ticket. I've uploaded your patch there (and changed the commit message to reflect the appropriate ticket number). I hope that's OK with you. If so, you can just remove the ticket here from the list and move the ticket back to "Positive". We really want to get this merged, especially #13387 (which depends on this ticket) because that fixes some issues with |
This comment has been minimized.
This comment has been minimized.
comment:330
Replying to @nbruin:
I thought it was absolutely necessary, and since it wasn't merged yet, I thought it was OK to do it here.
I can't remove the attachment, but I can remove it from the list of to-be-applied-patches.
OK. Apply trac_12313-mono_dict-combined-random-sk.patch |
comment:331
This ticket is not merged (at some point it was in some preliminary version of sage-5.8.beta0). You check this by |
This comment has been minimized.
This comment has been minimized.
Merged: sage-5.8.beta2 |
The following could not be fixed in #715:
The problem is that any parent has a dictionary that stores any coerce map (and a different dictionary for conversion maps) ending at this parent. The keys are given by the domains of the maps. So, in the example above, the field
K
has an attribute that is a dictionary whose keys are the different elliptic curves.In coercion, it is usually best to compare parents not by equality but by identity. Therefore, I suggest to implement a container called
MonoDict
that works similar to the new weakTripleDict
(see #715), but takes a single item as a key.First tests show that one can actually gain a lot of speed:
MonoDict
can access its items much faster than a usual dictionary.Apply
Depends on #715
Depends on #11521
Depends on #12215
Depends on #13746
Depends on #13378
Depends on #13387
Dependencies: #715, #11521, #12215, #13746, #13378; merge with #13387
CC: @jpflori @vbraun @robertwb @alexanderdreyer @sagetrac-PolyBoRi @nthiery
Component: memleak
Keywords: coercion weak dictionary
Author: Simon King, Jean-Pierre Flori
Reviewer: Simon King, Jean-Pierre Flori, John Perry, Nils Bruin
Merged: sage-5.8.beta2
Issue created by migration from https://trac.sagemath.org/ticket/12313
The text was updated successfully, but these errors were encountered: