#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 nbruin):
Replying to [comment:47 SimonKing]:
> Both patches are updated, I guess we can now focus on the remaining
issue with properties.
>
> I asked whether it'd make sense to cythonize lazy_attribute. I think it
does not, because we want to override `__name__` of an instance which
won't work in a cdef class.
Do you mean for documentation and code introspection purposes? I guess
that might be an issue.
> In addition, it would be bad for speed if the property needed to first
look up a name and then try to find this name as a key of
`__cached_methods`.
I don't think it's really measurable. I added a descriptor that avoids the
lookup:
{{{
cdef class SpecializedCachedAttributeType(object):
def __init__(self, name):
if name !="_codomain":
raise ValueError("I don't do that sort of thing")
def __get__(self,instance,cls):
try:
return instance.__cached_methods["_codomain"]
except KeyError:
raise AttributeError("%s instance has no attribute
'%s'"%(cls,"_codomain"))
}}}
and changed the initialization to
{{{
_domain=CachedAttributeType("_domain")
_codomain=SpecializedCachedAttributeType("_codomain")
}}}
I'm getting:
{{{
sage: %timeit B._codomain
10000000 loops, best of 3: 82.9 ns per loop
sage: %timeit B._domain
10000000 loops, best of 3: 84 ns per loop
}}}
which is close enough together that it's hard to call the difference
significant (with the old cython I'm getting `114 ns` for each). If you
store that name on a cdef attribute, the lookup is pretty fast.
> Would it be acceptable to have two specialised non-data descriptors with
fixed names `_domain` and `_codomain`, rather than implementing a general
non-data descriptor that can take any name?
I don't think it's worth it. You'll gain much more if you allow `HomType`
instances to have a `__dict__` (although a drawback there is that all
cpdef methods will slow down; they'd need to be split in a cdef and a def
again, or cython should grow a way of calling cpdef methods "without
instance dict attribute override check", for which the infrastructure is
already available and is used internally in cython for the python wrapper
of the cpdef method)
--
Ticket URL: <http://trac.sagemath.org/sage_trac/ticket/14214#comment:48>
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.