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

Minor Performance Regression Initializing threading.Event #96349

Closed
dgiger42 opened this issue Aug 28, 2022 · 0 comments
Closed

Minor Performance Regression Initializing threading.Event #96349

dgiger42 opened this issue Aug 28, 2022 · 0 comments
Labels
performance Performance or resource usage stdlib Python modules in the Lib dir

Comments

@dgiger42
Copy link
Contributor

Bug report

In the main Python branch, creating a threading.Event is slightly slower than it was in 3.10:

I ran a simple benchmark on the main branch (built with --enable-optimizations):

../cpython/python -m pyperf timeit -s 'from threading import Event' 'e = Event()'
.....................
Mean +- std dev: 1.59 us +- 0.02 us

And on 3.10:

../cpython3.10/python -m pyperf timeit -s 'from threading import Event' 'e = Event()'
.....................
Mean +- std dev: 1.52 us +- 0.02 us

When initializing an Event, it creates a Condition via Condition(Lock()). The Condition's __init__() then has to catch 3 exceptions in these blocks:

try:
    self._release_save = lock._release_save
except AttributeError:
    pass
try:
    self._acquire_restore = lock._acquire_restore
except AttributeError:
    pass
try:
    self._is_owned = lock._is_owned
except AttributeError:
    pass

My understanding is that the new zero-cost exceptions make try/except blocks significantly faster when an exception isn't thrown, but don't help when the exception is actually thrown.

For Condition objects, I think it would likely be easy to avoid this minor slowdown by using hasattr() instead of the try/except blocks, and it would also be more concise.

Your environment

  • CPython versions tested on: 3.10, 3.12
  • Operating system and architecture: Ubuntu 20.04, x86_64
@dgiger42 dgiger42 added the type-bug An unexpected behavior, bug, or error label Aug 28, 2022
@AlexWaygood AlexWaygood added performance Performance or resource usage stdlib Python modules in the Lib dir labels Aug 28, 2022
@corona10 corona10 removed the type-bug An unexpected behavior, bug, or error label Aug 30, 2022
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
performance Performance or resource usage stdlib Python modules in the Lib dir
Projects
None yet
Development

No branches or pull requests

3 participants