Skip to content

Conversation

@henryiii
Copy link
Contributor

@henryiii henryiii commented Dec 9, 2025

Based on KRRT7#3; codeflash-ai thinks it can speed up this function by 564% (single function benchmark), but I think most of the speed up comes from avoiding constructing a set in every function call, and I think this is more standard of a structure too.

Signed-off-by: Henry Schreiner <henryfs@princeton.edu>
@henryiii henryiii changed the title chore: pull set construction out of function perf: pull set construction out of function Dec 9, 2025
@notatallshaw
Copy link
Member

I guess this makes sense but I do wonder if it switching from a set to an in-line tuple might be faster for such a small set.

Looking up a global value is slower than looking up a local value, constructing a tuple is very fast, doing x in (a, b, c) does a fast path of checking the Python object ids first, strings are often interned in CPythin, and the O(1) look up of a set might have a real time slower lookup than a tuple of a handful of items.

@henryiii
Copy link
Contributor Author

henryiii commented Dec 9, 2025

To get the full fast path, I believe it does have to be fully inlined. If it is, then it doesn’t matter if it’s a set or a tuple, it produces the same byte code.

I was assuming this wasn’t all that hot, and this is a common way to do this sort of thing.

@brettcannon
Copy link
Member

I do wonder if it switching from a set to an in-line tuple might be faster for such a small set.

Is might, but I personally would rather we didn't to retain the semantics that there is no preferred order.

@henryiii henryiii merged commit a7a1d1c into pypa:main Dec 9, 2025
40 checks passed
@henryiii henryiii deleted the henryiii/chore/_have_compatible_api branch December 9, 2025 19:21
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

3 participants