-
-
Notifications
You must be signed in to change notification settings - Fork 30.3k
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
gh-102302 Micro-optimize inspect.Parameter.__hash__
#102303
Conversation
Aligns it with __eq__, enables default property overrides accessing hash(self), quickens execution.
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.
It is now consistent with __eq__
and __reduce__
. LTGM.
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'm a little more sceptical about this change; I've posted some questions on the issue.
No news because imho it's more akin to a fix than to a new feature.
Every change that has a user-visible impact (whether it's a bug fix, performance optimisation or feature) needs to have a news entry. If this change doesn't have a user-visible impact, we need to ask why it's worth making this change at all.
Closing; see discussion on the issue for the rationale |
I still think that this is a good idea: using properties here does not make any practical sense. And direct attribute access is slightly faster. |
If somebody can give a benchmark showing a meaningful performance improvement here, I'm happy to reconsider, but the motivation put forward in the issue was not primarily to do with performance. I don't find the rationale in the issue convincing, and I also don't consider an argument to do with "consistency" a strong enough reason to make a change to the stdlib. This doesn't really fix anything I'd consider a user-visible bug (and if it does, it needs a test/NEWS), so I can't see how it's worth the code churn. |
@AlexWaygood here are my micro-benchmarks. Old code (
New code (
I think that the speed-up is quite big for just 4 chars of code :) |
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 add a short news entry describing the impact this patch will have on users of inspect
. Something like this should suffice:
Micro-optimise ``inspect.Parameter.__hash__``, reducing the time it takes to hash an instance by around 40%.
A Python core developer has requested some changes be made to your pull request before we can consider merging it. If you could please address their requests along with any other requests in other reviews from core developers that would be appreciated. Once you have made the requested changes, please leave a comment on this pull request containing the phrase |
So, my overriding the default property will happen to work in the next version, but not be considered a documented feature and could break in future versions without warning ? Just asking to be sure we're on the same page. |
Correct -- I'm happy to accept this patch on the basis of the microbenchmark @sobolevn provided, but I still wouldn't recommend doing what you're currently doing in your code base :) Something along the lines of what I suggested in #102302 (comment) would be safer and less prone to inadvertent breakage. |
Judging from this, the :class: should work. |
inspect.Parameter.__hash__
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.
Thanks for the patch, and thanks @sobolevn for persuading me to change my mind :)
🎉 |
* main: (21 commits) pythongh-102192: Replace PyErr_Fetch/Restore etc by more efficient alternatives in sub interpreters module (python#102472) pythongh-95672: Fix versionadded indentation of get_pagesize in test.rst (pythongh-102455) pythongh-102416: Do not memoize incorrectly loop rules in the parser (python#102467) pythonGH-101362: Optimise PurePath(PurePath(...)) (pythonGH-101667) pythonGH-101362: Check pathlib.Path flavour compatibility at import time (pythonGH-101664) pythonGH-101362: Call join() only when >1 argument supplied to pathlib.PurePath() (python#101665) pythongh-102444: Fix minor bugs in `test_typing` highlighted by pyflakes (python#102445) pythonGH-102341: Improve the test function for pow (python#102342) Fix unused classes in a typing test (pythonGH-102437) pythongh-101979: argparse: fix a bug where parentheses in metavar argument of add_argument() were dropped (python#102318) pythongh-102356: Add thrashcan macros to filter object dealloc (python#102426) Move around example in to_bytes() to avoid confusion (python#101595) pythonGH-97546: fix flaky asyncio `test_wait_for_race_condition` test (python#102421) pythongh-96821: Add config option `--with-strict-overflow` (python#96823) pythongh-101992: update pstlib module documentation (python#102133) pythongh-63301: Set exit code when tabnanny CLI exits on error (python#7699) pythongh-101863: Fix wrong comments in EUC-KR codec (pythongh-102417) pythongh-102302 Micro-optimize `inspect.Parameter.__hash__` (python#102303) pythongh-102179: Fix `os.dup2` error reporting for negative fds (python#102180) pythongh-101892: Fix `SystemError` when a callable iterator call exhausts the iterator (python#101896) ...
* main: (37 commits) pythongh-102192: Replace PyErr_Fetch/Restore etc by more efficient alternatives in sub interpreters module (python#102472) pythongh-95672: Fix versionadded indentation of get_pagesize in test.rst (pythongh-102455) pythongh-102416: Do not memoize incorrectly loop rules in the parser (python#102467) pythonGH-101362: Optimise PurePath(PurePath(...)) (pythonGH-101667) pythonGH-101362: Check pathlib.Path flavour compatibility at import time (pythonGH-101664) pythonGH-101362: Call join() only when >1 argument supplied to pathlib.PurePath() (python#101665) pythongh-102444: Fix minor bugs in `test_typing` highlighted by pyflakes (python#102445) pythonGH-102341: Improve the test function for pow (python#102342) Fix unused classes in a typing test (pythonGH-102437) pythongh-101979: argparse: fix a bug where parentheses in metavar argument of add_argument() were dropped (python#102318) pythongh-102356: Add thrashcan macros to filter object dealloc (python#102426) Move around example in to_bytes() to avoid confusion (python#101595) pythonGH-97546: fix flaky asyncio `test_wait_for_race_condition` test (python#102421) pythongh-96821: Add config option `--with-strict-overflow` (python#96823) pythongh-101992: update pstlib module documentation (python#102133) pythongh-63301: Set exit code when tabnanny CLI exits on error (python#7699) pythongh-101863: Fix wrong comments in EUC-KR codec (pythongh-102417) pythongh-102302 Micro-optimize `inspect.Parameter.__hash__` (python#102303) pythongh-102179: Fix `os.dup2` error reporting for negative fds (python#102180) pythongh-101892: Fix `SystemError` when a callable iterator call exhausts the iterator (python#101896) ...
Aligns it with eq, enables default properties in subclasses to access hash(self), quickens execution.
No news because imho it's more akin to a fix than to a new feature.