Repository navigation
Conversation
hetaozdh
force-pushed
the
list-fast-eq
branch
2 times, most recently
from
October 9, 2026 23:04
d4fc000 to
11eac6e
Compare
Use specialized comparisons for exact int, float and str objects to avoid unnecessary rich-comparison dispatch in list operations.
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.
List scans (
in,index,count,remove, and element comparisons inlist.__eq__()) callPyObject_RichCompareBool(). For exact builtin type such asint,floatorstrpairs, we can avoid the generic rich-comparison dispatch and use a specialized comparison instead, following the approach already used bylist.sort().Benchmarks
Microbenchmarks (ns/element, 20000-element list, match near the end,
--disable-gilbuild):x in list[int]list.index(x)list.count(x)x in list[str]list.remove(x)[int] == [int][str] == [str]Selected
pyperformancebenchmarks: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_operatorandtest_compare, plus differential checks covering subclasses, NaNs, custom__eq__implementations, large integers, string representations and mutation during comparison. The existingtest_deopt_from_append_listenvironment failure also occurs on unmodified main.list.__contains__/index/count/remove#159092