Sorry to bother you again, but I think that changes on a very basic level such as sage/structure/element.pxd require sage-devel's blessing.
On 25 Apr., 19:57, Simon King <simon.k...@uni-jena.de> wrote: > The background of my question is trac ticket #11115. It provides a > Cython version of @cached_method, and it is really fast now. To back my claim up: With the patch, I obtain sage: class MyClass: ....: def __init__(self): ....: self._cache = {} ....: @cached_method ....: def noarg_cache(self): ....: return True ....: def noarg(self): ....: return True ....: @cached_method ....: def arg_cache(self,x,n=10): ....: return x^n ....: def arg(self,x,n=10): ....: try: ....: return self._cache[x,n] ....: except KeyError: ....: a = self._cache[x,n] = x^n ....: return a ....: sage: O = MyClass() sage: timeit('O.noarg_cache()', number=10^6) 1000000 loops, best of 3: 104 ns per loop sage: timeit('O.noarg()', number=10^6) 1000000 loops, best of 3: 237 ns per loop sage: timeit('O.arg_cache(3)', number=10^6) 1000000 loops, best of 3: 942 ns per loop sage: timeit('O.arg(3)', number=10^6) 1000000 loops, best of 3: 844 ns per loop sage: timeit('O.arg_cache(3,10)', number=10^6) 1000000 loops, best of 3: 933 ns per loop sage: timeit('O.arg(3,10)', number=10^6) 1000000 loops, best of 3: 1.01 µs per loop So, if there are no arguments then the @cached_method is actually faster than simply returning "True". With arguments, it can compete with a self-made cache written in Python. But of course, using @cached_method is more convenient than a hand-knitted cache. Now, the proposed additional attribute is not related with the speedup in the example above. But it would play a role if you have * a cdef class derived from Element * that does not allow attribute assignment and * that inherits a cached method from the category framework. Namely, the additional attribute makes the cache work in the situation above. One could argue: 1) The situation above is very rare, and if a developer wants that it works for a cdef class Foo(Element), then s/he can still provide Foo with a "cdef public dict __cached_methods": It is not needed to do that on the level of sage.structure.element. or 2) I worked with that patch, and I did not see memory problems. So, apparently the additional attribute is lightweight enough. Thus we can afford to make the creation of cached methods via categories as convenient as possible. I am in favour of 2). 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