Hi Nils,

On 2013-02-03, Nils Bruin <nbr...@sfu.ca> wrote:
> so in all cases, aliasing does NOT work well with cached_method.
> Furthermore, I think the logic behind some of the __get__ methods
> might need some reconsideration. It seems that the
> MethodCaller.__get__ tries to replicate some of the functionality of
> CachedMethod.__get__ (which is really required to "bind" the cached
> method), but I'm not sure it should.

I think the problem with aliasing is that each cached method knows its
own name. Hence, if you do Foo.trac = Foo.truc or define the same during
class definition, then Foo.trac still believes that its name is
'truc'.

Now, in the __get__ method, it is tried to store the cached method
(actually, the CachedMethodCaller) as an attribute of the instance, in
one or another way. The name of this attribute is the name known to the
cached method.

In other words: If you do f = Foo() and then f.truc, then the
__get__ method assigns an instance of CachedMethodCaller(NoArgs) to f
via setattr(f, "truc", ...).

If you call f.truc the next time, it is very fast, because the __get__
method is not involved any more.

But if you call f.trac, then the __get__ method is called again, and it
assings a new CachedMethodCaller(NoArgs) to f --- *again* under the name
"truc", *not* "trac"!

Hence, the old f.truc gets overridden (therefore the cache is now
empty), and calling f.trac again does *not* work via attribute lookup.
The __get__ method will be called repeatedly, and will never realise
that we want to use the name "trac".

Of course, one could add some 
    try:
        return getattr(inst, self._cachedmethod._cachedfunc.__name__)
    except AttributeError:
        pass
to the __get__ methods in sage/misc/cachefunc.pyx. It would speed up the
access to aliases of a cached method, and it would thus make the cache
of f.truc accessible via f.trac.

But it would slow down the access to non-aliased cached methods, because
adding this line means to look up the attribute *twice* (namely before
calling __get__ and then *inside* of __get__). Therefore I am -1 to that
addition.

Can we think of a different mechanism to allow aliases? Is there any way
to tell a cached method named "truc" that it has just been accessed under
the name "trac" and shall thus please store itself under the name "trac"
as an attribute of the instance?

Best regards,
Simon


-- 
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 http://groups.google.com/group/sage-devel?hl=en.
For more options, visit https://groups.google.com/groups/opt_out.


Reply via email to