Uh oh!
There was an error while loading.Please reload this page.
- Notifications
You must be signed in to change notification settings - Fork33.7k
Closed
Description
Bug report
In the main Python branch, creating athreading.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 usAnd on 3.10:
../cpython3.10/python -m pyperf timeit -s 'from threading import Event' 'e = Event()'.....................Mean +- std dev: 1.52 us +- 0.02 usWhen initializing anEvent, it creates aCondition viaCondition(Lock()). TheCondition's__init__() then has to catch 3 exceptions in these blocks:
try:self._release_save=lock._release_saveexceptAttributeError:passtry:self._acquire_restore=lock._acquire_restoreexceptAttributeError:passtry:self._is_owned=lock._is_ownedexceptAttributeError:pass
My understanding is that the new zero-cost exceptions maketry/except blocks significantly faster when an exceptionisn't thrown, but don't help when the exception is actually thrown.
ForCondition objects, I think it would likely be easy to avoid this minor slowdown by usinghasattr() instead of thetry/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