Skip to content

gh-153198 Optimize set and frozenset membership lookup#153199

Open
rajat315315 wants to merge 3 commits into
python:mainfrom
rajat315315:set_membership_opt
Open

gh-153198 Optimize set and frozenset membership lookup#153199
rajat315315 wants to merge 3 commits into
python:mainfrom
rajat315315:set_membership_opt

Conversation

@rajat315315

Copy link
Copy Markdown

Linked Issue

Fixes gh-153198

Proposed Changes

  • Modified Objects/setobject.c to:
    • Add PyUnicode_CheckExact and unicode_eq() fast-path check in set_compare_frozenset.
    • Mark set_do_lookup as static inline Py_ALWAYS_INLINE int so the compiler can specialize and inline the lookup loop for both comparison target functions.
  • Verified correctness by running test_set (all 644 tests passed successfully).

Benchmarks (1,000,000 lookup operations)

  • set_lookup (int keys): 20.1% faster (reduced from 42.2 ms to 33.7 ms)
  • set_lookup_str (string keys): 26.2% faster (reduced from 71.1 ms to 52.5 ms)
  • frozenset_lookup_str (string keys): 17.0% faster (reduced from 71.2 ms to 59.1 ms)

@rajat315315 rajat315315 requested a review from rhettinger as a code owner July 6, 2026 14:17
@bedevere-app

bedevere-app Bot commented Jul 6, 2026

Copy link
Copy Markdown

Most changes to Python require a NEWS entry. Add one using the blurb_it web app or the blurb command-line tool.

If this change has little impact on Python users, wait for a maintainer to apply the skip news label instead.

@python-cla-bot

python-cla-bot Bot commented Jul 6, 2026

Copy link
Copy Markdown

All commit authors signed the Contributor License Agreement.

CLA signed

@rajat315315 rajat315315 changed the title Optimize set and frozenset membership lookup gh-153198 Optimize set and frozenset membership lookup Jul 6, 2026

@StanFromIreland StanFromIreland left a comment

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

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

Please provide benchmarks, run on a PGO + LTO build.

Comment thread Objects/setobject.c
#define PERTURB_SHIFT 5

static int
static inline Py_ALWAYS_INLINE int

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

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

Is the compiler not already specialising this?

Comment thread Objects/setobject.c

// This is similar to set_compare_entry_lock_held() but we don't need to
// incref startkey before comparing and we don't need to check if the set has
// changed. This also omits the PyUnicode_CheckExact() special case since it

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

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

??

@bedevere-app

bedevere-app Bot commented Jul 6, 2026

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.

@StanFromIreland StanFromIreland added the pending The issue will be closed if no feedback is provided label Jul 6, 2026
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

awaiting changes pending The issue will be closed if no feedback is provided

Projects

None yet

Development

Successfully merging this pull request may close these issues.

Optimize set and frozenset membership lookup (x in s) performance

2 participants