-
-
Notifications
You must be signed in to change notification settings - Fork 32.3k
gh-107710: Speed up logging.getHandlerNames
function
#107711
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
Conversation
Lib/logging/__init__.py
Outdated
@@ -916,8 +916,7 @@ def getHandlerNames(): | |||
""" | |||
Return all known handler names as an immutable set. | |||
""" | |||
result = set(_handlers.keys()) | |||
return frozenset(result) | |||
return frozenset(_handlers.keys()) |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Why keep .keys()
?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I've tested with frozenset(_handlers)
it is a bit slower:
(.venv) ~/Desktop/cpython issue-107710 ✗ 1 ⚠️
» pyperf timeit --setup 'import ex; l = ex.logging' 'l.getHandlerNames()'
.....................
Mean +- std dev: 2.07 us +- 0.02 us
(.venv) ~/Desktop/cpython issue-107710 ✗
» pyperf timeit --setup 'import ex; l = ex.logging' 'l.getHandlerNames()'
.....................
Mean +- std dev: 2.07 us +- 0.01 us
(.venv) ~/Desktop/cpython issue-107710 ✗
» pyperf timeit --setup 'import ex; l = ex.logging' 'l.getHandlerNames()'
.....................
Mean +- std dev: 2.07 us +- 0.01 us
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I don't think a 3us difference is a good enough reason to avoid using the simpler code here. And it is still faster than main
:)
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
The difference is so small that I would classify this change as a code cleanup rather than an optimization.
This comment was marked as spam.
This comment was marked as spam.
Sorry, something went wrong.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Please guys I need a help
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Login
logging.getHandlerNames()
#107710