-
-
Notifications
You must be signed in to change notification settings - Fork 482
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
Optimize ClassCallMetaClass using Cython #12808
Comments
comment:1
Before
After the preliminary patch
|
comment:2
I'm finishing the patch, I've added a large optimization for the other branch Here are the results in the basis case (no
So I'm 5% only slower than normal class. Here are the results in the basis case (no
Here I'm twice as fast as before. I'm cleanup the patch (doctests) an will post it shortly. |
comment:3
Sorry, I can not do any serious work (reviewing), as I am in the middle of holidays. But it looks promising! |
comment:4
Hi Florent! I know the patch is preliminary, but here are two comments: In the new file classcall_metaclass_cy.pyx, it is not explained what In the old python version of classcall_metaclass, you move |
comment:5
Also, why not going all the way and remove the python version completely? Or at least: Why not cythonizing (and caching?) the |
comment:6
Or ideally, one could try to provide a generalised metaclass framework in Sage. Namely, one could think of having different small metaclasses, each providing a particular feature. For example:
And if we have different "small" metaclasses then it would be useful to combine them. But that's a problem for Python: If A, B and C have different metaclasses, you can not simply have a class definition like
This is (I guess) why So, what I mean by a generalised metaclass framework in Sage is:
But I guess that's a topic for a different ticket... |
comment:7
PS: If that works, one could also get rid of the |
comment:8
Hi Simon, Thanks for all those remarks and sorry for not having finished this one as fast as I wanted. Replying to @simon-king-jena:
Obviously. Please see my patch on sage combinat's queue which already adresses all those remarks. I'm currently doing intensive timing to be sure that we have the fastest way. Florent |
comment:9
Replying to @simon-king-jena:
Yes it is. We discussed last week with Nicolas of switching the inheritance
+1 for this and for keeping it for a different ticket. |
comment:10
Hi Simon, I just uploaded an updated patch. I still have to lauch the tests (but I've to run for a train now) and to check the documentation. The code together with the timings should hopefully be the final version here. Thanks for your remarks. Florent |
comment:12
Could it be that the patch was created by something like "hg remove sage/misc/classcall_metaclass.py" followed by "hg add sage/misc/classcall_metaclass.pyx"? I am no mercurial expert, but I was told that one should better use "hg rename sage/misc/classcall_metaclass.py sage/misc/classcall_metaclass.pyx". |
comment:13
While we are at it: Shouldn't we also try whether changing unique_representation.py into unique_representation.pyx yields a speedup (but not changing "class" into "cdef class", I guess that won't work)? |
comment:14
I have created an alternative patch (using hg rename). It is smaller than the original patch, but should result in identical code. So, either of the patches could be used, but I guess the smaller patch is easier to read (because one sees more easily the additional differences between classcall_metaclass.py and classcall_metaclass.pyx) Apply trac_12808-classcall_speedup2.patch |
This comment has been minimized.
This comment has been minimized.
comment:15
Good news: All doctests pass. I am astonished that the C-level way of calling |
comment:16
Probably the dynamic class cythonisation should be on a different ticket. However, it seems that the change to cython will not be so difficult: I simply renamed dynamic_class.py into dynamic_class.pyx (and added two lines to module_list.py), and the test suite seems to pass (most of it is already finished). And then, one can apply your trick with |
comment:17
I see that you added |
comment:18
Replying to @simon-king-jena:
Thanks for trying this out! It's cool that it works. Now, do you mind postponing to a later ticket after #11935? I am doing a couple small changes to dynamic_class in my upcoming reviewer's patch (hopefuly tomorrow). |
comment:19
Note that there is a conflict with #12215, so, either of the patches needs to be rebased. But I guess we first see what patch will be reviewed first... |
comment:20
Some further questions (in addition to my question about the rôle of Why is Why is there a two-step cythonisation? I mean, why is there a cdef class I guess the typecall function will be useful in other modules (e.g., when cythonising dynamic_class). But it only is def. Shouldn't it rather be Concerning the timing tools provided in the module: Is there still no "central" location for all aspects of timing? I thought there were occasional discussions on sage-devel about a benchmark/timing framework. |
comment:21
Hi Simon, Thanks for your in depth review. Replying to @simon-king-jena:
This is a tentative inclusion of the doc of the special methods (see
I just want to hide the few class I wrote here for timing (CRef, C2, C3,
There is this discussion about cleaning up the way metaclass are defined and
You mean
Not that I know of. Florent |
comment:22
I forgot: thanks for creating a new patch. I didn't use rename because at some point of my experiment, I was having the two files How did you manage to get a patch using rename at the end ? Florent |
comment:23
Replying to @hivert:
So, it is not for making something importable, but for excluding something from automatic import? Cool!
You mean the sage-combinat-devel thread I started?
Well, I actually think cythoning Compare: You have to add a new cdef class I think I will test it...
Yes, I forgot the letter "p".
I expected it to be inlined, if it is defined cpdef inline in the pxd file, and then cimported into another cython file. But I don't know if that is really done, actually. |
comment:24
Replying to @hivert:
Manually. Namely: Apply your patch, copy the touched/new files to a temporary directory, remove your patch, |
comment:25
Replying to @simon-king-jena:
Yep ! see
Yes ! An some face to face discussion we had with Nicolas.
Except that at some point Nicolas suggested to keep NestedClassMetaclass for
I'll look at the compiled result. |
Reviewer: Simon King |
comment:48
I forgot: Since your patch looks fine to me (independent on whether we eventually decide to cdef |
comment:49
Replying to @simon-king-jena:
Ok ! For your part I've a question: is there any reason why do you call Otherwise things looks good ! Florent |
Attachment: trac_12808_nested_class_cython.patch.gz Cythonise nested classes |
comment:50
Replying to @hivert:
Thanks for spotting it! I have updated the nested_class patch. Apply trac_12808-classcall_speedup-fh.patch trac_12808_nested_class_cython.patch trac_12808-classcall_cdef.patch |
Changed reviewer from Simon King to Simon King, Florent Hivert |
comment:51
Then it's a positive review for me ! I also created #12886 as a followup. Thanks Simon. |
comment:52
Please decide which patches have to be applied. |
This comment has been minimized.
This comment has been minimized.
comment:53
Replying to @jdemeyer:
Sorry ! I forgot to remove Simon's question. It's decided. Florent |
comment:55
There is a trivial conflict between this ticket and #12215 (in unique_representation.py). Which one should go first? |
comment:56
I guess this one should go in first,: After all, #12215 has not been reviewed, yet. |
comment:57
Replying to @simon-king-jena:
Ok. May I let you handle the rebase? |
comment:58
Replying to @nthiery:
OK. |
Merged: sage-5.1.beta0 |
When a class
C
is an instance ofClasscallMetaclass
, then for any constructor call, the system currently looks for__classcall__
and__classcall_private__
inC
. This adds quite an overhead and this could be cached, assuming no one modifiesC
(which seems reasonable). Improving this speeds up, in particular, all call to the constructor of a subclass ofUniqueRepresentation
, eg. many parents, all categories...Apply
CC: @nthiery @simon-king-jena
Component: misc
Keywords: classcall UniqueRepresentation
Author: Florent Hivert, Simon King
Reviewer: Simon King, Florent Hivert
Merged: sage-5.1.beta0
Issue created by migration from https://trac.sagemath.org/ticket/12808
The text was updated successfully, but these errors were encountered: