#11900: Serious regression caused by #9138
------------------------------+---------------------------------------------
   Reporter:  SimonKing       |          Owner:  tbd                  
       Type:  defect          |         Status:  needs_work           
   Priority:  critical        |      Milestone:  sage-4.8             
  Component:  performance     |       Keywords:  categories regression
Work_issues:                  |       Upstream:  N/A                  
   Reviewer:  Jeroen Demeyer  |         Author:  Simon King           
     Merged:                  |   Dependencies:  #9138 #11911         
------------------------------+---------------------------------------------

Comment(by SimonKing):

 I changed `Rings(Category)` into `Rings(Category_singleton)` (but then had
 to remove the `rings_contains` function in the pyx file, by cyclic
 import).

 First, because of your optimisation of the classcall, one has
 {{{
 sage: %timeit Rings()
 625 loops, best of 3: 3.42 µs per loop
 }}}
 Second, one has
 {{{
 sage: R = Rings()
 sage: P.<x,y,z> = QQ[]
 sage: P in R
 True
 sage: %timeit P in R
 625 loops, best of 3: 1.38 µs per loop
 }}}

 Using `PyType_IsSubtype` from sage/ext/python_type.pxi, one comes down to
 1.08 µs.

 You use a lazy class attribute not only for the classcall, but also for
 the containment test. If one writes the containment test directly as a
 method (recall: We are here in a cython file!), one has 1.53 µs. So, to my
 surprise, the lazy class attribute trick works well also for performance!

 Using a similar approach for computing the hash works as well. Namely, for
 a singleton class, it should be fine if the hash of the (single) instance
 is not id of the instance but id of the class. With the following code
 {{{
 #!python
 cdef class hash_c(object):
     cdef long n
     def __init__(self, cls):
         self.n = id(cls)
     def __call__(self):
         return self.n

 class Category_singleton(Category):
     ...
     @lazy_class_attribute
     def __hash__(cls):
         return hash_c(cls)
 }}}
 I obtain
 {{{
 sage: %timeit {R:1}
 625 loops, best of 3: 695 ns per loop
 }}}
 Without that trick, it is 1.46 µs.

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