Skip to content

gh-137103: A faster check_circular #137286

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

Open
wants to merge 6 commits into
base: main
Choose a base branch
from
Open

Conversation

aivarsk
Copy link
Contributor

@aivarsk aivarsk commented Jul 31, 2025

Make check_circular faster without breaking API:

  1. It used to do PyDict_Contains check followed by PyDict_SetItem which means the hash is calculated two times and the hash lookup is done twice (which includes taking locks etc.) Instead do a speculative PyDict_SetItem and check if the dictionary size has increased. Checking the size is a simple memory read.

  2. The hash was still calculated two times for PyDict_SetItem and PyDict_DelItem. Instead calculate it once and use the _KnownHash variants for the functions to reuse the hash.

Copy link
Member

@picnixz picnixz left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I think the hashing can be done in a separate function. Unless I'm mistaken, it appears that the code is the same right?

@picnixz
Copy link
Member

picnixz commented Aug 4, 2025

Ok, looks like everything's good for me. Could you add a NEWS entry? also you could add a What's New entry under "Optimizations".

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

Successfully merging this pull request may close these issues.

2 participants