Skip to content

Conversation

@itamaro
Copy link
Contributor

@itamaro itamaro commented May 1, 2023

Fixes gh-104066

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 

microbenchmark with PR

python -m pyperf timeit -s 'import os' 'hasattr(os, "getenv")' ..................... Mean +- std dev: 37.2 ns +- 2.6 ns 
python -m pyperf timeit -s 'import os' 'hasattr(os, "nothing")' ..................... Mean +- std dev: 55.8 ns +- 3.0 ns 

the missing attr case is almost 10x faster

@carljm carljm added the 🔨 test-with-refleak-buildbots Test PR w/ refleak buildbots; report in status section label May 1, 2023
@bedevere-bot
Copy link

🤖 New build scheduled with the buildbot fleet by @carljm for commit 8c07ffb 🤖

If you want to schedule another build, you need to add the 🔨 test-with-refleak-buildbots label again.

@bedevere-bot bedevere-bot removed the 🔨 test-with-refleak-buildbots Test PR w/ refleak buildbots; report in status section label May 1, 2023
@itamaro itamaro changed the title gh-NNNNN: Improve performance of hasattr for module objects gh-104066: Improve performance of hasattr for module objects May 1, 2023
@itamaro itamaro marked this pull request as ready for review May 1, 2023 23:19
Copy link
Member

@carljm carljm left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This looks fine to me; it's following the existing pattern already used to optimize hasattr for both types and normal objects, by avoiding raising and then suppressing an AttributeError.

Will leave it for a couple days before merging in case any other reviewers want to take a look.

@carljm carljm self-assigned this May 2, 2023
@arhadthedev arhadthedev added performance Performance or resource usage interpreter-core (Objects, Python, Grammar, and Parser dirs) labels May 2, 2023
@carljm carljm merged commit fdcb49c into python:main May 4, 2023
@itamaro itamaro deleted the module-hasattr branch May 4, 2023 14:51
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

4 participants