gh-131798: Remove bounds check when indexing into tuples with a constant index (#137607)

* Remove bounds check when indexing into tuples with a constant index

* Add news entry

* fixup after rebase
This commit is contained in:
Tomas R. 2025-12-28 22:06:06 +01:00 committed by GitHub
parent c3febba73b
commit 713684de53
No known key found for this signature in database
GPG key ID: B5690EEEBB952194
10 changed files with 1232 additions and 940 deletions

View file

@ -332,6 +332,19 @@ dummy_func(void) {
i = sub_st;
}
op(_GUARD_BINARY_OP_SUBSCR_TUPLE_INT_BOUNDS, (tuple_st, sub_st -- tuple_st, sub_st)) {
assert(sym_matches_type(tuple_st, &PyTuple_Type));
if (sym_is_const(ctx, sub_st)) {
assert(PyLong_CheckExact(sym_get_const(ctx, sub_st)));
long index = PyLong_AsLong(sym_get_const(ctx, sub_st));
assert(index >= 0);
int tuple_length = sym_tuple_length(tuple_st);
if (tuple_length != -1 && index < tuple_length) {
REPLACE_OP(this_instr, _NOP, 0, 0);
}
}
}
op(_BINARY_OP_SUBSCR_TUPLE_INT, (tuple_st, sub_st -- res, ts, ss)) {
assert(sym_matches_type(tuple_st, &PyTuple_Type));
if (sym_is_const(ctx, sub_st)) {