-
-
Notifications
You must be signed in to change notification settings - Fork 481
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
The category graph should comply with Python's method resolution order #11943
Comments
comment:1
With the new test, I found a bug in algebra_ideals:
Thus, apparently, that category has never been used. The problem is that the super categories of the category of algebra ideals should be the category of algebra modules. However, while the former accepts non-commutative algebras, the latter wants to see commutative algebras. The clean way to proceed would be: Make algebra modules work over non-commutative rings. If that is too much of work, C.super_categories() should be |
Dependencies: #11900 |
comment:2
Patch's up! To my surprise, the change in the order of super categories was relatively harmless. In few cases, a test involving all_super_categories had to change. I added a test Apart from these small changes, all doc tests passed! In particular, I did not need to change That was the good news. The bad news is that we have a regression in the computation of
With the patches from here added, we only have
I tried hard to make my C3 implementation as fast as possible in the range we need: few lists (say, 4) of moderate length (not more than 60). I think the performance should be improved. But a review can already be started. |
Author: Simon King |
comment:3
Replying to @simon-king-jena:
+1. Actually my patch on #10963 is adding a TODO about it :-) |
comment:4
Replying to @simon-king-jena:
I am not so surprised, since most categories are actually used by some Cheers, |
comment:5
I tried to track down the regression of With #11900, the main work is done in
I guess I have to improve the implementation of the recursion. |
comment:6
PS: It also seems that much time is spent in Namely, even though #11115 will almost totally reduce the overhead of calling a cached function, a lazy attribute will always be faster. |
comment:7
Replying to @simon-king-jena:
Sorry, I meant: One lazy attribute C.super_categories, and two lazy attributes C.all_super_categories and C.all_super_categories_proper (super_categories() has no arguments). |
This comment has been minimized.
This comment has been minimized.
comment:8
I have created a more invasive patch: It turns The two patches are alternative. Personally, I prefer the second patch. If the reviewer disagrees and suggests to use the first patch, I'd like to add one improvement to the first patch. If the reviewer agrees with me, then eventually either this patch or the patch from #11935 needs to be rebased. I just tested that all doctests pass, with sage-4.7.2.alpha3+#11900+the second patch from here. And here are some timings:
and
With #11900+the second patch from here:
and
Note an additional doc test, demonstrating that the new version of all_super_categories can detect inconsistencies:
Python is not able to create a consistent mro for the parent class - and all_super_categories detects that inconsistency as well:
Of course, there still is the new consistency test for the category graph in the test suite. So, it can really be reviewed now, and my suggestion is: Apply trac11943_mro_for_all_super_categories_lazy.patch |
comment:9
There was no reply on sage-combinat-devel and only little reply on sage-devel: The only reply was Jason Grout stating that the use of attributes is often more pythonic than the use of a method. So, the idea to turn super_categories and all_super_categories into lazy attributes is supported. |
comment:10
Maarten suggested the following:
I think that this would be a very good solution indeed. |
Work Issues: Preserve super_categories etc. as methods, but introduce a lazy attribute _super_categories |
comment:11
I think the following model is best for preserving backwards compatibility:
|
comment:12
I have changed my patch according to what I learnt from the discussion on sage-devel. Namely:
Anyway, why do I see a need to make things faster? Recall that the purpose of this ticket is to order the list of all super categories according to Python's method resolution order, i.e., the C3 algorithm. I did my very best to implement the C3 algorithm as fast as possible. However:
Here are the data. I've run
That's not much (3.3% speedup), but certainly better than a regression. |
Changed work issues from Preserve super_categories etc. as methods, but introduce a lazy attribute _super_categories to none |
comment:13
I forgot the patch bot: Apply trac11943_mro_for_all_super_categories_lazy.patch |
comment:14
There is a new suggestion, also based on the sage-devel thread: If we want to test whether a category C is a super category of a category D, then we currently try to find C in the list of all super categories. But it is much faster to search it in a frozen set of super categories. So, I suggest to use a lazy attribute Since #11115 is merged into sage-4.7.3.alpha1, I wonder whether it is still necessary to have the list of all super categories as a lazy attribute, or whether a cached method is fast enough. I'll need some tests. However, I think it is a good idea to have a lazy attribute "Needs work", until I have sorted it out. |
comment:82
Pushed. I am running the tests now. Note: I also had to make some modifications to the original patch; so please start from http://combinat.sagemath.org/patches/file/tip/trac11943_mro_for_all_super_categories_lazy_hook.patch. As far as I remember, all I did to this patch was, as discussed, to remove the hunks that were removing "cached_method"'s on those super_categories methods that are going to disapear with #10963 anyway. That to make the rebase of #10963 easier. I'll upload both patches here once the tests are positive. |
Attachment: trac11943_mro_for_all_super_categories_lazy_hook.patch.gz Order the super categories of a category according to Python's mro. Internally replace (all_)super_categories by lazy attributes, to prevent a regression. Use |
This comment has been minimized.
This comment has been minimized.
Changed work issues from rebase to none |
comment:84
All tests pass; latest versions of the patches uploaded. Please fold the two patches once you have reviewed my changes. |
comment:85
Sorry, I can not do (reviewing or other) work, being in the middle of holidays. I'll return next week. |
comment:86
Replying to @simon-king-jena:
Sure: enjoy your vacations! Just in case: are you ok with, e.g., Florent finishing the review, or do you prefer double checking things yourself? (just to give a chance to the patch to go into 5.0). Cheers, |
comment:87
On 5.0.beta13, I was getting a random doctest failure due to the order
The updated patch uses "..." instead of X and Y. |
comment:88
Replying to @nthiery:
Thank you! By the way, it is in France.
Of course! Just go ahead. |
comment:89
The reviewer patch mostly looks fine to me (running tests now). But I think one should use the occasion and apply the trac directive in the Sphynx markup. Hence,
rather than
When I wrote the original patch, I have not been aware of the directive. |
comment:90
Replying to @simon-king-jena:
Perfect; I was about to upload an updated patch doing just that after a suggestion by Florent on Friday :-) Give me two minutes. |
comment:91
Attachment: trac11943_mro_for_all_super_categories_lazy_hook-review-nt.patch.gz There it is. Here is the diff between the two patches (oops, sorry, I did the diff the wrong way):
If you are happy with the changes, please fold the two patches, and double check the patch header. Cheers, |
Replaces the two other patches: Order the super categories of a category according to Python's mro. Internally replace (all_)super_categories by lazy attributes, to prevent a regression. Use |
This comment has been minimized.
This comment has been minimized.
comment:92
Attachment: trac11943_mro_for_all_super_categories_combined.patch.gz Replying to @nthiery:
Done! I am happy with your reviewer patch (which is folded into the combined patch), and if you think it is appropriate, please give it a positive review. Apply trac11943_mro_for_all_super_categories_combined.patch |
Reviewer: Nicolas M. Thiéry |
Merged: sage-5.1.beta0 |
Let C be a category.
C.all_super_categories()
starts withC.super_categories()
and finds all super categories inductively.Unfortunately, the algorithm of
C.all_super_categories()
does not comply with the C3 algorithm, that is used by Python to determine the method resolution order forC.parent_class
andC.element_class
.The aim of this ticket is to be more consistent. Eventually, for any category C (perhaps with modifications for hom categories), one should have
and that test should become part of the test suite.
At #11900, the implementation of
all_super_categories()
has been changed, so, work should rely on #11900.Unfortunately, it seems that the C3 algorithm can not simply be imported from Python, even though Python uses it internally, namely in
Objects/typeobject.c
. Looking at the implementation, it requires that one gets a list of types, while we want to use it on a list of objects.Therefore, we need to implement C3 from scratch. Implementing C3 is easy, but if it shall be fast, one needs to be careful.
In particular, one needs to be able to
L.pop(0)
quickly, where L is a list. Unfortunately,L.pop(0)
is slow, even in Cython. I found that, if the lists are not too long, it is best to revert L and doL.pop()
instead, which is optimized in Cython. See the discussion at sage-devel.What my patch does:
sage.misc.c3
all_super_categories()
_test_category_graph
, asserting thatself.parent_class.mro()
andself.all_super_categories()
are compatible.Apply:
attachment: trac11943_mro_for_all_super_categories_combined.patch
Depends on #11900
Depends on #7980
CC: @hivert @koffie
Component: categories
Keywords: category graph, method resolution order
Author: Simon King
Reviewer: Nicolas M. Thiéry
Merged: sage-5.1.beta0
Issue created by migration from https://trac.sagemath.org/ticket/11943
The text was updated successfully, but these errors were encountered: