Skip to content

[mypyc] Add primitives for int and str containment in list and tuple - #22435

Open
Jahongir-Qurbonov wants to merge 2 commits into
python:masterfrom
Jahongir-Qurbonov:mypyc-list-contains
Open

Jahongir-Qurbonov wants to merge 2 commits into
python:masterfrom
Jahongir-Qurbonov:mypyc-list-contains

Conversation

@Jahongir-Qurbonov

Copy link
Copy Markdown
Contributor

x in lst and x in tup used the generic PySequence_Contains, which also requires boxing an int operand on every call. Add primitives for int and str operands when the container is a list or a variable-length tuple:

  • int: a short int operand is compared directly with exact int items, without boxing it. The operand is boxed (once) only if there are items of other types, and these are compared using PyObject_RichCompareBool, like in CPython.
  • str: an exact str operand is compared directly with exact str items (identity, then length, kind and contents). Other items use PyObject_RichCompareBool.

List and tuple subclasses (which may override __contains__), str subclass operands (which may override __eq__) and long ints still use PySequence_Contains. On free-threaded builds the list is accessed in a critical section, like in list.index() and list.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:

1 item 4 items 32 items
int in list 2.1x 2.8x 3.7x
str in list 1.4x 1.8x 2.6x
str in tuple 1.3x 1.7x 2.3x

On free-threaded 3.14t, int in list is 1.7x / 1.6x / 3.9x faster and str in list 1.0x / 1.6x / 3.7x faster (the critical section dominates for very short lists).

Part of mypyc/mypyc#644

@Jahongir-Qurbonov Jahongir-Qurbonov mentioned this pull request Nov 10, 2026
13 of 14 tasks
# Conflicts:
#	mypyc/primitives/tuple_ops.py
#	mypyc/test-data/run-lists.test
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.

1 participant