Hi Nils,

On 2012-08-15, Nils Bruin <nbr...@sfu.ca> wrote:
> I think I have identified this mysterious object. I think it's
>
> sage/rings/ring.pyx:1926
>
> cdef dict _is_Field_cache = {}
>
> as introduced in
>
> http://trac.sagemath.org/sage_trac/ticket/11900
>
> with author listed Simon King :-)

Oops.

> DONT CACHE SO MUCH!!! It often leads to nasty complications.

I think in this case, the real problem is that "R in Fields()" has to do
special cases, mainly for historic reasons. Some fields are not declared
as fields, so, their category is not a sub-category of Fields(). Hence,
one needs to call is_Field. But that used to be slow in many cases.
Therefore the answer is cached.

> IntegerRingMods cache for themselves whether they are fields already

Yes, but some rings don't. And that's why it became slow.

> and really, it's their responsibility to answer swiftly if someone
> wants to know whether they're a field.

"Swiftly" isn't always possible. IIRC, that was the reason for the
complicated code in Fields().__contains__.

I think a better way of caching would be _in the rings_, not externally.
In that way, the cache would not prevent them from being garbage
collected.

Perhaps the following approach would work:
 - Modify sage.rings.ring.Ring.is_field, so that it does "internal"
   caching (so that there is no memory leak). In particular, the caching
   should be intelligent enough that a cached result for proof=True should
   be used when calling it later with proof=False.
 - is_field should not be overridden. Instead, the actual implementation
   should be provided in a method _is_field. So, all is_field
   implementations in the Sage tree should be renamed. _is_field does
   not need to be cached.
 - is_Field should not be cached, since caching is the job of
   Ring.is_field.

Alternatively, one could try to use the category framework in a nasty
way: If "R in Fields()" returns True, then R.category() is refined to a
sub-category of Fields(). In that way, is_Field(R) would only be called
in the first instance of "R in Fields()", so that caching of is_Field
isn't needed anymore, even if R.is_field() is not cached.

Did you open a ticket already? If not, I'll do so.

Thanks for tracking that down!
Cheers,
Simon

-- 
-- 
To post to this group, send an email to sage-devel@googlegroups.com
To unsubscribe from this group, send an email to 
sage-devel+unsubscr...@googlegroups.com
For more options, visit this group at http://groups.google.com/group/sage-devel
URL: http://www.sagemath.org



Reply via email to