Repository navigation
[mypyc] Add primitives for list and variable-length tuple equality - #22410
Merged
Merged
Conversation
Use specialized C functions for == and != when both operands are lists or both are variable-length tuples, instead of PyObject_RichCompare followed by a truth test of the result. Subclasses use the generic path.
Tuple equality now compares items even if the lengths differ, like CPython, since item comparisons can have side effects or raise exceptions. Pass the comparison operator to the C functions, so that != on list and tuple subclasses calls __ne__ instead of negating the result of __eq__.
Jahongir-Qurbonov
force-pushed
the
mypyc-list-tuple-eq
branch
from
November 10, 2026 18:02
87ada72 to
25d80a4
Compare
13 of 14 tasks
# Conflicts: # mypyc/test-data/run-tuples.test
# Conflicts: # mypyc/lib-rt/tuple_ops.c
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.
Add primitives for
==/!=where both operands arelist, or both are variable-lengthtuple(tuple[T, ...]).Previously these used
PyObject_RichCompare, followed by a truth test of the returned object. The newCPyList_Compare/CPyTuple_Comparetake the operator (Py_EQorPy_NE) and return a C int directly (likeCPyBytes_Compare):list/tupleobjects: identical objects are equal, otherwise items are compared withPyObject_RichCompareBool, like CPython does. Lists of different lengths are not equal without comparing items. Tuples compare items even if the lengths differ, since CPython's tuple comparison doesn't have a length shortcut (item comparisons can have side effects or raise exceptions).CPyList_Compareholds a critical section on both lists (like CPython), and handles lists being mutated by an item__eq__.__eq__or__ne__) usePyObject_RichComparewith the original operator, so!=calls__ne__instead of negating__eq__.Fixed-length tuples (
RTuple) are already compared item by item, so they are not affected.Tests compare the results against the generic implementation (through
Any), and cover items with custom__eq__(including non-bool results and exceptions), identity of items (NaN), mutation during comparison, tuples of different lengths, and subclasses (including named tuples, and subclasses overriding only__eq__or only__ne__).Micro-benchmark (compiled code, 3.2M comparisons, best of 15, before -> after, Python 3.13):
listtuplePart of mypyc/mypyc#644.