#14214: Cythoned homsets
----------------------------------------------+-----------------------------
Reporter: SimonKing | Owner: tbd
Type: enhancement | Status: needs_work
Priority: major | Milestone: sage-5.10
Component: performance | Resolution:
Keywords: Hom, cython, cached method | Work issues: How to store
domain and codomain?
Report Upstream: N/A | Reviewers:
Authors: Simon King | Merged in:
Dependencies: #14159, #12951, #13184 | Stopgaps:
----------------------------------------------+-----------------------------
Comment (by SimonKing):
Replying to [comment:54 nbruin]:
> Ping!. The present patches here pass doctests on the bot!
>
> I think conceptually what you want in this case is really just a nondata
descriptor that acts as a backstopper to provide (essentially) a read-only
attribute stored in `__cached_methods` if no instance dictionary is
available (or more accurately: if it isn't used) The code at [comment:40]
provides that.
Agreed.
The first question is: Where shall we put the code?
> {{{
> cdef class CachedAttributeType(object):
> cdef public str name
> def __init__(self, name):
> self.name = name
> def __get__(self,instance,cls):
> cdef dict CM
> try:
> CM = getattr(instance,"__cached_methods")
> return CM[self.name]
> except TypeError, KeyError:
> raise AttributeError("%s instance has no attribute
'%s'"%(cls,self.name))
> }}}
I guess you either mean `getattr(instance, "__cached_methods", None)` or
`except (AttributeError,KeyError):` (in any case, it should be with
brackets, otherwise it will not catch the `KeyError`).
And I guess you could get it still a bit faster, if you do `def
__get__(self, Parent instance, cls)`, because our homsets are parents, and
because the cdef public attribute `__cached_methods` could be accessed
more quickly if the instance is known to be a Parent.
> Since the `__get__` here is very close the one of the faster code paths
in a properly cythonized `lazy_attribute` (see #14615), defining the
backed attribute either way:
>
> {{{
> _domain=CachedAttributeType("_domain")
>
> @lazy_attribute
> def _codomain(self);
> raise AttributeError("You should have set me already!")
> }}}
>
> is going to give the same performance.
And that's the second question: Are you really sure that lazy_attribute
will be as fast as `CachedAttributeType` in the case of no `__dict__`?
Note that I tested to cythonize lazy_attribute ''and'' to cimport Parent.
It failed (circular import or so). So, we come back to the first question:
Where shall we put the code? I think it makes sense to cdefine `instance`
being Parent, but this would be impossible in
`sage/misc/lazy_attribute.pyx`.
And the third question is: Shall we really make this ticket depend on a
proper cythonized version of lazy_attribute? Note that currently the lazy
attribute or cached attribute would never come into play, because all our
homsets are Python subclasses of the (now cythonized) Homset.
--
Ticket URL: <http://trac.sagemath.org/sage_trac/ticket/14214#comment:55>
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 unsubscribe from this group and stop receiving emails from it, send an email
to [email protected].
To post to this group, send email to [email protected].
Visit this group at http://groups.google.com/group/sage-trac?hl=en.
For more options, visit https://groups.google.com/groups/opt_out.