Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

gh-103951: enable optimization for fast attribute access on module subclasses #126264

Open
wants to merge 7 commits into
base: main
Choose a base branch
from

Conversation

skirpichev
Copy link
Member

@skirpichev skirpichev commented Nov 1, 2024

This patch relax specialization requirements from
PyModule_CheckExact(op) to
Py_TYPE(op)->tp_getattro != PyModule_Type.tp_getattro.

Benchmarks:

# a.py
import pyperf
import b
import c
runner = pyperf.Runner()
runner.timeit('b.x', 'b.x', globals=globals())
runner.timeit('c.x', 'c.x', globals=globals())
# b.py
x = 1
# c.py
import sys, types
x = 1
class _Foo(types.ModuleType): pass
sys.modules[__name__].__class__ = _Foo

On the main:

$ python a.py -q
b.x: Mean +- std dev: 50.2 ns +- 2.7 ns
c.x: Mean +- std dev: 132 ns +- 7 ns

With the patch:

$ python a.py -q
b.x: Mean +- std dev: 52.9 ns +- 3.6 ns
c.x: Mean +- std dev: 52.6 ns +- 2.7 ns

…ule subclasses

This patch relax specialization requirements from
``PyModule_CheckExact(op)`` to
``Py_TYPE(op)->tp_getattro != PyModule_Type.tp_getattro``.

Benchmarks:

```py
import pyperf
import b
import c
runner = pyperf.Runner()
runner.timeit('b.x', 'b.x', globals=globals())
runner.timeit('c.x', 'c.x', globals=globals())
```
```py
x = 1
```
```py
import sys, types
x = 1
class _Foo(types.ModuleType): pass
sys.modules[__name__].__class__ = _Foo
```

On the main:
```
$ python a.py -q
b.x: Mean +- std dev: 50.2 ns +- 2.7 ns
c.x: Mean +- std dev: 132 ns +- 7 ns
```

With the patch:
```
$ python a.py -q
b.x: Mean +- std dev: 52.9 ns +- 3.6 ns
c.x: Mean +- std dev: 52.6 ns +- 2.7 ns
```

Co-authored-by: Nicolas Tessore <n.tessore@ucl.ac.uk>
@skirpichev skirpichev changed the title gh-103951: enable optimization for fast attribute access on module su… gh-103951: enable optimization for fast attribute access on module subclasses Nov 1, 2024
@skirpichev

This comment was marked as outdated.

@skirpichev skirpichev marked this pull request as draft November 5, 2024 08:50
@skirpichev

This comment was marked as outdated.

@skirpichev skirpichev marked this pull request as ready for review November 5, 2024 10:11
@skirpichev skirpichev marked this pull request as draft November 6, 2024 15:24
@skirpichev skirpichev marked this pull request as ready for review November 7, 2024 03:48
@ntessore
Copy link
Contributor

ntessore commented Nov 7, 2024

Thanks for the PR @skirpichev

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

Successfully merging this pull request may close these issues.

3 participants