#715: Parents probably not reclaimed due to too much caching
------------------------+---------------------------------------------------
Reporter: robertwb | Owner: somebody
Type: defect | Status: needs_review
Priority: major | Milestone: sage-4.8
Component: coercion | Keywords: weak cache coercion
Work_issues: | Upstream: N/A
Reviewer: | Author: Simon King
Merged: | Dependencies: #9138, #11900
------------------------+---------------------------------------------------
Comment(by SimonKing):
By the way, here is an example that shows that a `TripleDictById` finds
its items ''faster'' then a usual dict, even though it has the additional
advantage of weak keys. If one uses Cython, one can still save some more,
which is what I did in the preceding change of the second patch.
I create a list of pairs of rings:
{{{
sage: for p in prime_range(10^3):
....: K = GF(p)
....: P = K['x','y']
....: L.append((K,P))
....:
sage: len(L)
168
}}}
I create a `TripleDictById` and a usual dictionary, and fill it by the
same data:
{{{
sage: from sage.structure.coerce_dict import TripleDictById
sage: D = TripleDictById(113)
sage: for i,(K,P) in enumerate(L):
....: D[K,P,True] = i
....:
sage: E = {}
sage: for i,(K,P) in enumerate(L):
....: E[K,P,True] = i
....:
sage: len(D)
168
sage: len(E)
168
}}}
I create cython functions that know about the types. In the first, I use
the Python way of accessing data from `TripleDictById`, in the second, I
use the special cdefed `get()` method, and the third is for usual
dictionaries.
{{{
sage: cython("""
....: from sage.structure.coerce_dict cimport TripleDictById
....: def testD(TripleDictById D, list L):
....: for K,P in L:
....: n = D[K,P,True]
....: def testDget(TripleDictById D, list L):
....: for K,P in L:
....: n = D.get(K,P,True)
....: def testE(dict D, list L):
....: for K,P in L:
....: n = D[K,P,True]
....: """)
}}}
Even though Cython is supposed to be quite good at optimising dictionary
access (mind that `testE(...)` knows that it will receive a dictionary!),
I was surprised by how much faster the `TripleDictById` is:
{{{
sage: %timeit testD(D,L)
625 loops, best of 3: 67.8 µs per loop
sage: %timeit testDget(D,L)
625 loops, best of 3: 52.1 µs per loop
sage: %timeit testE(E,L)
125 loops, best of 3: 3.26 ms per loop
}}}
Fourty to sixty times faster! So, I think it was a good idea to use
`TripleDictById` not only in the coercion model, but also as an attribute
of Parent.
--
Ticket URL: <http://trac.sagemath.org/sage_trac/ticket/715#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.