Skip to content

Refactor getattr_from_other_class() for lookup of methods in categories #20686

@nthiery

Description

@nthiery

For parents and elements implemented in Cython, category lookup is emulated in __getattr__ using getattr_from_other_class. In this ticket, this is refactored a bit:

  1. We should not pick up special attributes from type, there is no point in returning the type name of the category here (similarly for __dict__, __mro__, ...):
sage: ZZ(1).__name__
'JoinCategory.element_class'
sage: ZZ.__name__
'JoinCategory.parent_class'

This change causes a few failures which need to be fixed.

  1. The implementation of getattr_from_other_class is not very robust. For example, static methods are not supported. We fix this by using low-level Python functions to get the attribute and we manually call the descriptor __get__ if needed.

  2. We shouldn't do anything special with double-underscore private attributes: in plain Python, this is implemented by the parser and not by getattr(). So __getattr__ would already receive the mangled private name.

  3. Some of the changes broke _sage_src_lines_, so we also change that: currently, _sage_src_lines_() is used both to get the source lines of a class (e.g. dynamic classes) and an instance (e.g. cached functions). We change this to always mean the source lines of an instance, which makes things clearer.

  4. The lookup using getattr_from_other_class is about 9 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

We improve on this by roughly a factor 2 (but even then, it's still a lot slower than usual method lookup).

NOTE: This needs a trivial Cython patch, see #21030 for the Cython upgrade.

Depends on #21030
Depends on #21409

Upstream: Fixed upstream, in a later stable release.

CC: @jdemeyer @tscrim

Component: categories

Author: Jeroen Demeyer

Branch/Commit: 74041f3

Reviewer: Vincent Delecroix

Issue created by migration from https://trac.sagemath.org/ticket/20686

Metadata

Metadata

Assignees

No one assigned

    Type

    No type

    Projects

    No projects

    Milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions