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

Performance of attribute lookup for module objects #104066

Closed
itamaro opened this issue May 1, 2023 · 0 comments · Fixed by #104063
Closed

Performance of attribute lookup for module objects #104066

itamaro opened this issue May 1, 2023 · 0 comments · Fixed by #104063
Labels
interpreter-core (Objects, Python, Grammar, and Parser dirs) performance Performance or resource usage type-feature A feature request or enhancement

Comments

@itamaro
Copy link
Contributor

itamaro commented May 1, 2023

Feature or enhancement

Similar to gh-92216 (for type objects) - attribute lookup on module objects for non-existing attributes is significantly slower compared to existing attributes, as well as non-existing attributes on types and instances.

microbenchmark on main:

python -m pyperf timeit -s 'import os' 'hasattr(os, "getenv")'
.....................
Mean +- std dev: 37.8 ns +- 1.5 ns
python -m pyperf timeit -s 'import os' 'hasattr(os, "nothing")'
.....................
Mean +- std dev: 541 ns +- 16 ns

As in gh-92216, the reason for the slowness is time spent in creating an AttributeError, which is not needed.
By adding a special case for modules (similar to the type special case in gh-99979), this overhead can be eliminated.

Linked PRs

@itamaro itamaro added the type-feature A feature request or enhancement label May 1, 2023
itamaro added a commit to itamaro/cpython that referenced this issue May 1, 2023
@arhadthedev arhadthedev added performance Performance or resource usage interpreter-core (Objects, Python, Grammar, and Parser dirs) labels May 2, 2023
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
interpreter-core (Objects, Python, Grammar, and Parser dirs) performance Performance or resource usage type-feature A feature request or enhancement
Projects
None yet
Development

Successfully merging a pull request may close this issue.

2 participants