#20686: Optimize method lookup from the categories for instances of Cython
classes
-------------------------------------+-------------------------------------
Reporter: nthiery | Owner:
Type: enhancement | Status: new
Priority: major | Milestone: sage-7.3
Component: categories | Resolution:
Keywords: | Merged in:
Authors: Jeroen Demeyer | Reviewers:
Report Upstream: Fixed upstream, | Work issues:
but not in a stable release. | Commit:
Branch: | 92f4520d01640379590d6c5974622f3d6f396e9b
u/jdemeyer/optimize_method_lookup_from_the_categories_for_instances_of_cython_classes|
Stopgaps:
Dependencies: #20825 |
-------------------------------------+-------------------------------------
Description changed by jdemeyer:
Old description:
> For instances of Cython classes, the inheritance from categories is
> emulated by a manual lookup in
> `sage.structure.element.Element.__getattr__`. This lookup is about 10
> times slower than a normal method lookup::
>
> {{{
> sage: def foo(self): return
> sage: Sets().element_class.foo = foo
> sage: def g(x):
> ....: for i in range(1000):
> ....: x.foo()
>
> sage: x = Semigroups().example().an_element()
> sage: y = 1
>
> sage: %timeit g(x)
> 10000 loops, best of 3: 115 µs per loop
>
> sage: %timeit g(y)
> 1000 loops, best of 3: 1.18 ms per loop
> }}}
>
> So there should be room for optimization (or using a different
> approach for emulating this inheritance).
>
> Besides this, we should not pick up special attributes from `type`, there
> is no point in returning `type.__name__` here (similarly for `__dict__`,
> `__mro__`, ...):
> {{{
> sage: getattr_from_other_class(1, type, "__name__")
> 'type'
> }}}
>
> This needs a trivial Cython patch:
> https://github.com/cython/cython/pull/522
New description:
For instances of Cython classes, the inheritance from categories is
emulated by a manual lookup in
`sage.structure.element.Element.__getattr__`. This lookup is about 10
times slower than a normal method lookup::
{{{
sage: def foo(self): return
sage: Sets().element_class.foo = foo
sage: def g(x):
....: for i in range(1000):
....: x.foo()
sage: x = Semigroups().example().an_element()
sage: y = 1
sage: %timeit g(x)
10000 loops, best of 3: 115 µs per loop
sage: %timeit g(y)
1000 loops, best of 3: 1.18 ms per loop
}}}
So there should be room for optimization (or using a different
approach for emulating this inheritance).
Besides this, we should not pick up special attributes from `type`, there
is no point in returning `type.__name__` here (similarly for `__dict__`,
`__mro__`, ...):
{{{
sage: getattr_from_other_class(1, type, "__name__")
'type'
}}}
This needs a trivial Cython patch (accepted by upstream):
https://github.com/cython/cython/pull/522
--
--
Ticket URL: <http://trac.sagemath.org/ticket/20686#comment:14>
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 https://groups.google.com/group/sage-trac.
For more options, visit https://groups.google.com/d/optout.