#15247: Introduce a baseclass for singletons
------------------------+-------------------------------
  Reporter:  SimonKing  |             Type:  enhancement
    Status:  new        |         Priority:  major
 Milestone:  sage-5.13  |        Component:  performance
Resolution:             |  Report Upstream:  N/A
------------------------+-------------------------------
 Since some while, we have
 `sage.categories.category_singleton.Category_singleton`. A singleton is a
 class that allows the creation of exactly one instance (of any subclass).
 The technical implementa
 tion of `Category_singleton` is roughly like this: In some static
 `__classcall__`, test that the given class is a direct subclass of
 `Category_singleton`; then create a (the) instance of the class;
 then set the override the classcall of this class and of the class of the
 instance (which might be a subclass) by a constant function returning the
 instance.

 I suggest to provide this construction scheme generally, in
 `sage.misc.fast_methods.SingletonClass`. The idea is: If one write a class
 inheriting from `SingletonClass` then one does not need to think about
 caching the unique instance or writing a fast hash or writing fast
 comparison, since this will all be inherited from `SingletonClass`.

 I suggest to use it at least on the rational field, which currently has a
 custom cache, and its hash method is written in Python.

 With the branch that I am going to add, I get
 {{{
 sage: %timeit RationalField()
 1000000 loops, best of 3: 269 ns per loop
 sage: %timeit hash(QQ)
 10000000 loops, best of 3: 134 ns per loop
 sage: %timeit QQ==ZZ
 10000000 loops, best of 3: 144 ns per loop
 }}}

 With vanilla sage-5.12.b5, it is a lot slower:
 {{{
 sage: %timeit RationalField()
 10000 loops, best of 3: 82.4 us per loop
 sage: %timeit hash(QQ)
 1000000 loops, best of 3: 496 ns per loop
 sage: %timeit QQ==ZZ
 1000000 loops, best of 3: 878 ns per loop
 }}}

--
Ticket URL: <http://trac.sagemath.org/ticket/15247>
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 unsubscribe from this group and stop receiving emails from it, send an email 
to [email protected].
To post to this group, send email to [email protected].
Visit this group at http://groups.google.com/group/sage-trac.
For more options, visit https://groups.google.com/groups/opt_out.

Reply via email to