Skip to content

[mypyc] Add primitives for list and variable-length tuple equality - #22410

Merged
p-sawicki merged 4 commits into
python:masterfrom
Jahongir-Qurbonov:mypyc-list-tuple-eq
Nov 16, 2026
Merged

p-sawicki merged 4 commits into
python:masterfrom
Jahongir-Qurbonov:mypyc-list-tuple-eq

Conversation

@Jahongir-Qurbonov

@Jahongir-Qurbonov Jahongir-Qurbonov commented Nov 9, 2026 •

Copy link
Copy Markdown
Contributor

Add primitives for == / != where both operands are list, or both are variable-length tuple (tuple[T, ...]).

Previously these used PyObject_RichCompare, followed by a truth test of the returned object. The new CPyList_Compare / CPyTuple_Compare take the operator (Py_EQ or Py_NE) and return a C int directly (like CPyBytes_Compare):

  • If both objects are exact list / tuple objects: identical objects are equal, otherwise items are compared with PyObject_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_Compare holds a critical section on both lists (like CPython), and handles lists being mutated by an item __eq__.
  • Subclasses (which may override __eq__ or __ne__) use PyObject_RichCompare with 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):

Case list tuple
Empty 0.048s -> 0.038s 0.049s -> 0.038s
Length 3, equal 0.056s -> 0.047s 0.053s -> 0.043s
Length 3, equal, distinct item objects 0.102s -> 0.108s 0.088s -> 0.090s
Different lengths 0.049s -> 0.041s 0.053s -> 0.042s
First item differs 0.065s -> 0.062s 0.062s -> 0.058s
Length 20, equal 0.101s -> 0.078s 0.083s -> 0.067s

Part of mypyc/mypyc#644.

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__.

@p-sawicki p-sawicki left a comment

Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

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

thanks!

@p-sawicki
p-sawicki merged commit 7100f3e into python:master Nov 16, 2026
19 checks passed
@Jahongir-Qurbonov
Jahongir-Qurbonov deleted the mypyc-list-tuple-eq branch November 16, 2026 18:01
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.

2 participants