gh-99761: add invalid_index macro by eendebakpt · Pull Request #99762 · python/cpython
In listobject.c there is an optimization to check whether an index is valid (e.g. 0 <= index < N) using a single comparison. The cast-to-unsigned optimization is used in current main in 3 locations: tupleobject.c, _collectionsmodule.c and bytesobject.c.
It is a micro optimization, but the code generated is different than the plain i < 0 || i >= Py_SIZE(a). This PR tries to:
i) apply the optimization to more locations
ii) make the code more consistent
By replacing index checks with a single method _Py_is_valid_index that includes the optimization we have consistency in the code and have the optimized check for all index checks.
Notes:
- The new method is private and is static inline (https://peps.python.org/pep-0670/)
- In
bytecodes.cthe optimized expression is used in three places. E.g.
DEOPT_IF(((size_t)signed_magnitude) > 1, BINARY_SUBSCR);
With the _Py_is_valid_index method it looks like
DEOPT_IF(!_Py_is_valid_index(signed_magnitude, 2), BINARY_SUBSCR);
which is in my opinion not very readable. For these cases a separate PR was created: #100064