Skip to content

gh-159092: Optimize comparisons in list operations - #159093

Open
hetaozdh wants to merge 1 commit into
python:mainfrom
hetaozdh:list-fast-eq
Open

hetaozdh wants to merge 1 commit into
python:mainfrom
hetaozdh:list-fast-eq

Conversation

@hetaozdh

@hetaozdh hetaozdh commented Oct 9, 2026 •

Copy link
Copy Markdown
Contributor

List scans (in, index, count, remove, and element comparisons in list.__eq__()) call PyObject_RichCompareBool(). For exact builtin type such as int, float or str pairs, we can avoid the generic rich-comparison dispatch and use a specialized comparison instead, following the approach already used by list.sort().

Benchmarks

Microbenchmarks (ns/element, 20000-element list, match near the end, --disable-gil build):

Operation Main PR Speedup
x in list[int] 9.14 3.24 2.82×
list.index(x) 9.45 3.66 2.58×
list.count(x) 9.71 3.38 2.87×
x in list[str] 8.83 4.79 1.84×
list.remove(x) 8.30 2.44 3.40×
[int] == [int] 9.24 3.63 2.55×
[str] == [str] 10.64 8.81 1.21×

Selected pyperformance benchmarks:

  • bm_hexiom: ~1.10× faster.
  • bm_meteor_contest, bm_go, bm_deltablue, bm_comprehensions: no significant difference.

I will try the full benchmarks when I am free.

Correctness

The fast path preserves the identity shortcut of PyObject_RichCompareBool(), including NaN identity semantics. Other type combinations retain the existing comparison path, preserving rich-comparison dispatch and recursive-comparison protection.

Tested with test_list, test_sort, test_operator and test_compare, plus differential checks covering subclasses, NaNs, custom __eq__ implementations, large integers, string representations and mutation during comparison. The existing test_deopt_from_append_list environment failure also occurs on unmodified main.

@bedevere-app bedevere-app Bot added the type-feature A feature request or enhancement label Oct 9, 2026
@hetaozdh
hetaozdh force-pushed the list-fast-eq branch 2 times, most recently from d4fc000 to 11eac6e Compare October 9, 2026 23:04
Use specialized comparisons for exact int, float and str objects to avoid unnecessary rich-comparison dispatch in list operations.
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

awaiting review type-feature A feature request or enhancement

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant