Re: [sage-devel] WeakValueDictionary: items are deleted too soon

2018-03-12 Thread Erik Bray
On Mon, Mar 12, 2018 at 10:58 AM, Jeroen Demeyer  wrote:
> There is some very bad behaviour related to CachedRepresentation caching
> that I'm observing on #24742 (but this is otherwise unrelated to that
> ticket):
>
> sage: timeit('MatrixSpace(ZZ,3,3)')
> 625 loops, best of 3: 117 µs per loop
>
> Now, we try again but we first create a strong reference:
>
> sage: S = MatrixSpace(ZZ,3,3)
> sage: timeit('MatrixSpace(ZZ,3,3)')
> 625 loops, best of 3: 4.13 µs per loop
>
> This is much faster the second time! In the first example, the caching of
> CachedRepresentation.__classcall__ is pointless since there is no strong
> reference to the entry in the cache, so it gets deleted immediately whenever
> the "MatrixSpace(ZZ,3,3)" is deleted.
>
> This is just the usual Py_DECREF of Python objects, it has nothing to do
> with the cyclic garbage collector: the behaviour remains the same even with
> gc.disable().
>
> This makes me think that we might need a version of CachedRepresentation
> which keeps semi-strong references: these would only be deleted by the
> cyclic garbage collector but not by a simple Py_DECREF().

I'm not exactly sure what you have in mind here, but you might be
interested in PEP-442 regardless:
https://www.python.org/dev/peps/pep-0442/

-- 
You received this message because you are subscribed to the Google Groups 
"sage-devel" group.
To unsubscribe from this group and stop receiving emails from it, send an email 
to sage-devel+unsubscr...@googlegroups.com.
To post to this group, send email to sage-devel@googlegroups.com.
Visit this group at https://groups.google.com/group/sage-devel.
For more options, visit https://groups.google.com/d/optout.


Re: [sage-devel] WeakValueDictionary: items are deleted too soon

2018-03-12 Thread Jeroen Demeyer

I created https://trac.sagemath.org/ticket/24954 for this

--
You received this message because you are subscribed to the Google Groups 
"sage-devel" group.
To unsubscribe from this group and stop receiving emails from it, send an email 
to sage-devel+unsubscr...@googlegroups.com.
To post to this group, send email to sage-devel@googlegroups.com.
Visit this group at https://groups.google.com/group/sage-devel.
For more options, visit https://groups.google.com/d/optout.


[sage-devel] WeakValueDictionary: items are deleted too soon

2018-03-12 Thread Jeroen Demeyer
There is some very bad behaviour related to CachedRepresentation caching 
that I'm observing on #24742 (but this is otherwise unrelated to that 
ticket):


sage: timeit('MatrixSpace(ZZ,3,3)')
625 loops, best of 3: 117 µs per loop

Now, we try again but we first create a strong reference:

sage: S = MatrixSpace(ZZ,3,3)
sage: timeit('MatrixSpace(ZZ,3,3)')
625 loops, best of 3: 4.13 µs per loop

This is much faster the second time! In the first example, the caching 
of CachedRepresentation.__classcall__ is pointless since there is no 
strong reference to the entry in the cache, so it gets deleted 
immediately whenever the "MatrixSpace(ZZ,3,3)" is deleted.


This is just the usual Py_DECREF of Python objects, it has nothing to do 
with the cyclic garbage collector: the behaviour remains the same even 
with gc.disable().


This makes me think that we might need a version of CachedRepresentation 
which keeps semi-strong references: these would only be deleted by the 
cyclic garbage collector but not by a simple Py_DECREF().



Jeroen.

--
You received this message because you are subscribed to the Google Groups 
"sage-devel" group.
To unsubscribe from this group and stop receiving emails from it, send an email 
to sage-devel+unsubscr...@googlegroups.com.
To post to this group, send email to sage-devel@googlegroups.com.
Visit this group at https://groups.google.com/group/sage-devel.
For more options, visit https://groups.google.com/d/optout.