Repository navigation
[mypyc] Add primitives for int and str containment in list and tuple - #22435
Open
Jahongir-Qurbonov wants to merge 2 commits into
Open
Jahongir-Qurbonov wants to merge 2 commits into
Jahongir-Qurbonov wants to merge 2 commits into
Conversation
13 of 14 tasks
# Conflicts: # mypyc/primitives/tuple_ops.py # mypyc/test-data/run-lists.test
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
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.
x in lstandx in tupused the genericPySequence_Contains, which also requires boxing anintoperand on every call. Add primitives forintandstroperands when the container is alistor a variable-lengthtuple:int: a short int operand is compared directly with exactintitems, without boxing it. The operand is boxed (once) only if there are items of other types, and these are compared usingPyObject_RichCompareBool, like in CPython.str: an exactstroperand is compared directly with exactstritems (identity, then length, kind and contents). Other items usePyObject_RichCompareBool.List and tuple subclasses (which may override
__contains__),strsubclass operands (which may override__eq__) and long ints still usePySequence_Contains. On free-threaded builds the list is accessed in a critical section, like inlist.index()andlist.remove(). The size of the list is read again after each comparison, since__eq__may modify the list.Benchmark: searching for an equal (but not identical) item at the end of the container, compiled, master vs this PR, Python 3.13:
int in liststr in liststr in tupleOn free-threaded 3.14t,
int in listis 1.7x / 1.6x / 3.9x faster andstr in list1.0x / 1.6x / 3.7x faster (the critical section dominates for very short lists).Part of mypyc/mypyc#644