GV

GeezerVison

The World Wide Web, restored to 800 pixels of glory.

Home|About|Gaming cabinet|Archives|How it works|Starter sites

On this site

  • Sign in
  • ActionsCI/CD and automation
  • CodespacesCloud development environments
  • All Features
  • Documentation
  • Training
  • Certifications
  • Build with Copilot
  • Customize Copilot
  • Integrations
  • Trending repos
  • GitHub Sponsors
  • Developer articles
  • Enterprises

This view

  • Original page
  • New address

GeezerVison is showing github.com — gh-102302 Micro-optimize `inspect.Parameter.__hash__` by Gouvernathor · Pull Request #102303 · python/cpython

Conversation

@Gouvernathor

@Gouvernathor Gouvernathor commented Feb 27, 2023 •
edited
Loading

Uh oh!

There was an error while loading. Please reload this page.

Copy link
Copy Markdown
Contributor

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.

  • Issue: Make inspect.Parameter.__hash__ use _default instead of default #102302

Sorry, something went wrong.

Uh oh!

There was an error while loading. Please reload this page.

All reactions
@Gouvernathor
Make Parameter.__hash__ use the actual values r.t properties
604425d
Aligns it with __eq__, enables default property overrides accessing hash(self), quickens execution.
@bedevere-bot bedevere-bot added the awaiting review label Feb 27, 2023
@bedevere-bot bedevere-bot mentioned this pull request Feb 27, 2023
Make inspect.Parameter.__hash__ use _default instead of default #102302
Closed
@sobolevn sobolevn added the skip news label Feb 28, 2023
sobolevn
sobolevn approved these changes Feb 28, 2023
View reviewed changes

@sobolevn sobolevn left a comment

Copy link
Copy Markdown
Member

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.

Sorry, something went wrong.

Uh oh!

There was an error while loading. Please reload this page.

Gouvernathor reacted with heart emoji
All reactions
  • ❤️ 1 reaction
@bedevere-bot bedevere-bot added awaiting core review and removed awaiting review labels Feb 28, 2023
@arhadthedev arhadthedev added the stdlib Standard Library Python modules in the Lib/ directory label Feb 28, 2023
@AlexWaygood AlexWaygood removed the skip news label Feb 28, 2023
AlexWaygood
AlexWaygood reviewed Feb 28, 2023
View reviewed changes

@AlexWaygood AlexWaygood left a comment

Copy link
Copy Markdown
Member

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.

Sorry, something went wrong.

Uh oh!

There was an error while loading. Please reload this page.

All reactions
@AlexWaygood

AlexWaygood commented Mar 3, 2023

Copy link
Copy Markdown
Member

Closing; see discussion on the issue for the rationale

All reactions

Sorry, something went wrong.

Uh oh!

There was an error while loading. Please reload this page.

@AlexWaygood AlexWaygood closed this Mar 3, 2023
@sobolevn

sobolevn commented Mar 3, 2023

Copy link
Copy Markdown
Member

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.

Gouvernathor reacted with thumbs up emoji
All reactions
  • 👍 1 reaction

Sorry, something went wrong.

Uh oh!

There was an error while loading. Please reload this page.

@AlexWaygood

AlexWaygood commented Mar 3, 2023

Copy link
Copy Markdown
Member

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.

All reactions

Sorry, something went wrong.

Uh oh!

There was an error while loading. Please reload this page.

@sobolevn

sobolevn commented Mar 3, 2023 •
edited
Loading

Uh oh!

There was an error while loading. Please reload this page.

Copy link
Copy Markdown
Member

@AlexWaygood here are my micro-benchmarks.

Old code (hash((selfname, self.kind, self.annotation, self.default))):

(.venv) ~/Desktop/cpython  main ✗                                                    
» pyperf timeit --setup 'import inspect; p = inspect.Parameter("p", inspect.Parameter.POSITIONAL_ONLY, default=4567890, annotation=int)' 'hash(p)'
.....................
Mean +- std dev: 1.87 us +- 0.16 us

New code (hash((self_name, self._kind, self._annotation, self._default))):

(.venv) ~/Desktop/cpython  main ✗                                                    
» pyperf timeit --setup 'import inspect; p = inspect.Parameter("p", inspect.Parameter.POSITIONAL_ONLY, default=4567890, annotation=int)' 'hash(p)'
.....................
Mean +- std dev: 1.05 us +- 0.05 us

I think that the speed-up is quite big for just 4 chars of code :)

AlexWaygood reacted with thumbs up emoji
All reactions
  • 👍 1 reaction

Sorry, something went wrong.

Uh oh!

There was an error while loading. Please reload this page.

@AlexWaygood AlexWaygood reopened this Mar 4, 2023
AlexWaygood
AlexWaygood requested changes Mar 4, 2023
View reviewed changes

@AlexWaygood AlexWaygood left a comment •
edited
Loading

Uh oh!

There was an error while loading. Please reload this page.

Copy link
Copy Markdown
Member

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%.

Sorry, something went wrong.

Uh oh!

There was an error while loading. Please reload this page.

Gouvernathor reacted with thumbs up emoji
All reactions
  • 👍 1 reaction
@bedevere-bot bedevere-bot added awaiting changes and removed awaiting core review labels Mar 4, 2023
@bedevere-bot

bedevere-bot commented Mar 4, 2023

Copy link
Copy Markdown

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 I have made the requested changes; please review again. I will then notify any core developers who have left a review that you're ready for them to take another look at this pull request.

All reactions

Sorry, something went wrong.

Uh oh!

There was an error while loading. Please reload this page.

@Gouvernathor

Gouvernathor commented Mar 4, 2023

Copy link
Copy Markdown
Contributor Author

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.

All reactions

Sorry, something went wrong.

Uh oh!

There was an error while loading. Please reload this page.

@AlexWaygood

AlexWaygood commented Mar 4, 2023 •
edited
Loading

Uh oh!

There was an error while loading. Please reload this page.

Copy link
Copy Markdown
Member

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.

Gouvernathor reacted with thumbs up emoji
All reactions
  • 👍 1 reaction

Sorry, something went wrong.

Uh oh!

There was an error while loading. Please reload this page.

@blurb-it
📜🤖 Added by blurb_it.
837bb3f
@Gouvernathor

Gouvernathor commented Mar 4, 2023

Copy link
Copy Markdown
Contributor Author

Judging from this, the :class: should work.

AlexWaygood reacted with thumbs up emoji
All reactions
  • 👍 1 reaction

Sorry, something went wrong.

Uh oh!

There was an error while loading. Please reload this page.

@AlexWaygood AlexWaygood changed the title gh-102302 Make Parameter.__hash__ use the actual values r.t properties gh-102302 Micro-optimize inspect.Parameter.__hash__ Mar 4, 2023
AlexWaygood
AlexWaygood approved these changes Mar 4, 2023
View reviewed changes

@AlexWaygood AlexWaygood left a comment

Copy link
Copy Markdown
Member

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 :)

Sorry, something went wrong.

Uh oh!

There was an error while loading. Please reload this page.

All reactions
@bedevere-bot bedevere-bot added awaiting merge and removed awaiting changes labels Mar 4, 2023
@AlexWaygood
AlexWaygood merged commit 90801e4 into python:main Mar 4, 2023
@bedevere-bot bedevere-bot removed the awaiting merge label Mar 4, 2023
@Gouvernathor
Gouvernathor deleted the patch-3 branch March 4, 2023 15:10
@sobolevn

sobolevn commented Mar 4, 2023

Copy link
Copy Markdown
Member

🎉

Gouvernathor reacted with hooray emoji
All reactions
  • 🎉 1 reaction

Sorry, something went wrong.

Uh oh!

There was an error while loading. Please reload this page.

hugovk pushed a commit to hugovk/cpython that referenced this pull request Mar 6, 2023
@Gouvernathor @hugovk
pythongh-102302 Micro-optimize inspect.Parameter.__hash__ (python#1…
2fffbe6
…02303)
carljm added a commit to carljm/cpython that referenced this pull request Mar 6, 2023
@carljm
Merge branch 'main' into nodeaddict
33dda2a
* 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)
  ...
carljm added a commit to carljm/cpython that referenced this pull request Mar 7, 2023
@carljm
Merge branch 'main' into inlinecomp2
ecb313c
* 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)
  ...
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Reviewers

@sobolevn sobolevn sobolevn approved these changes
@AlexWaygood AlexWaygood AlexWaygood approved these changes

Uh oh!

There was an error while loading. Please reload this page.

Assignees

No one assigned

Labels

stdlib Standard Library Python modules in the Lib/ directory

Projects

None yet

Milestone

No milestone

Development

Successfully merging this pull request may close these issues.

Uh oh!

There was an error while loading. Please reload this page.

5 participants

@Gouvernathor @AlexWaygood @sobolevn @bedevere-bot @arhadthedev
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
You are visitor 01402503 — GeezerVison does not store the pages you look up.
Best viewed with Netscape Navigator 4.7 or Internet Explorer 6 at 800×600. No cookies required.