@@ -3122,11 +3122,20 @@ dummy_func(
3122
3122
index_or_null = PyStackRef_TagInt (0 );
3123
3123
}
3124
3124
else {
3125
- PyObject * iter_o = PyObject_GetIter (PyStackRef_AsPyObjectBorrow (iterable ));
3126
- PyStackRef_CLOSE (iterable );
3127
- ERROR_IF (iter_o == NULL );
3128
- iter = PyStackRef_FromPyObjectSteal (iter_o );
3129
- index_or_null = PyStackRef_NULL ;
3125
+ PyObject * iter_o = PyStackRef_AsPyObjectBorrow (iterable );
3126
+ if (tp == & PyRange_Type && _PyRange_IsSimpleCompact (iter_o )) {
3127
+ Py_ssize_t stop = _PyRange_GetStopIfCompact (iter_o );
3128
+ PyStackRef_CLOSE (iterable );
3129
+ iter = PyStackRef_TagInt (stop );
3130
+ index_or_null = PyStackRef_TagInt (0 );
3131
+ }
3132
+ else {
3133
+ iter_o = PyObject_GetIter (iter_o );
3134
+ PyStackRef_CLOSE (iterable );
3135
+ ERROR_IF (iter_o == NULL );
3136
+ iter = PyStackRef_FromPyObjectSteal (iter_o );
3137
+ index_or_null = PyStackRef_NULL ;
3138
+ }
3130
3139
}
3131
3140
}
3132
3141
@@ -3204,17 +3213,33 @@ dummy_func(
3204
3213
3205
3214
replaced op (_FOR_ITER , (iter , null_or_index -- iter , null_or_index , next )) {
3206
3215
/* before: [iter]; after: [iter, iter()] *or* [] (and jump over END_FOR.) */
3207
- PyObject * iter_o = PyStackRef_AsPyObjectBorrow (iter );
3208
3216
if (PyStackRef_IsTaggedInt (null_or_index )) {
3209
- next = _PyForIter_NextWithIndex (iter_o , null_or_index );
3210
- if (PyStackRef_IsNull (next )) {
3211
- null_or_index = PyStackRef_TagInt (-1 );
3212
- JUMPBY (oparg + 1 );
3213
- DISPATCH ();
3217
+ if (PyStackRef_IsTaggedInt (iter )) {
3218
+ if (PyStackRef_Is (iter , null_or_index )) {
3219
+ null_or_index = PyStackRef_TagInt (-1 );
3220
+ JUMPBY (oparg + 1 );
3221
+ DISPATCH ();
3222
+
3223
+ }
3224
+ next = PyStackRef_BoxInt (null_or_index );
3225
+ if (PyStackRef_IsNull (next )) {
3226
+ ERROR_NO_POP ();
3227
+ }
3228
+ null_or_index = PyStackRef_IncrementTaggedIntNoOverflow (null_or_index );
3229
+ }
3230
+ else {
3231
+ PyObject * iter_o = PyStackRef_AsPyObjectBorrow (iter );
3232
+ next = _PyForIter_NextWithIndex (iter_o , null_or_index );
3233
+ if (PyStackRef_IsNull (next )) {
3234
+ null_or_index = PyStackRef_TagInt (-1 );
3235
+ JUMPBY (oparg + 1 );
3236
+ DISPATCH ();
3237
+ }
3238
+ null_or_index = PyStackRef_IncrementTaggedIntNoOverflow (null_or_index );
3214
3239
}
3215
- null_or_index = PyStackRef_IncrementTaggedIntNoOverflow (null_or_index );
3216
3240
}
3217
3241
else {
3242
+ PyObject * iter_o = PyStackRef_AsPyObjectBorrow (iter );
3218
3243
PyObject * next_o = (* Py_TYPE (iter_o )-> tp_iternext )(iter_o );
3219
3244
if (next_o == NULL ) {
3220
3245
if (_PyErr_Occurred (tstate )) {
@@ -3264,10 +3289,25 @@ dummy_func(
3264
3289
inst (INSTRUMENTED_FOR_ITER , (unused /1 , iter , null_or_index -- iter , null_or_index , next )) {
3265
3290
PyObject * iter_o = PyStackRef_AsPyObjectBorrow (iter );
3266
3291
if (PyStackRef_IsTaggedInt (null_or_index )) {
3267
- next = _PyForIter_NextWithIndex (iter_o , null_or_index );
3268
- if (PyStackRef_IsNull (next )) {
3269
- JUMPBY (oparg + 1 );
3270
- DISPATCH ();
3292
+ if (PyStackRef_IsTaggedInt (iter )) {
3293
+ if (PyStackRef_Is (iter , null_or_index )) {
3294
+ null_or_index = PyStackRef_TagInt (-1 );
3295
+ JUMPBY (oparg + 1 );
3296
+ DISPATCH ();
3297
+
3298
+ }
3299
+ next = PyStackRef_BoxInt (null_or_index );
3300
+ if (PyStackRef_IsNull (next )) {
3301
+ ERROR_NO_POP ();
3302
+ }
3303
+ null_or_index = PyStackRef_IncrementTaggedIntNoOverflow (null_or_index );
3304
+ }
3305
+ else {
3306
+ next = _PyForIter_NextWithIndex (iter_o , null_or_index );
3307
+ if (PyStackRef_IsNull (next )) {
3308
+ JUMPBY (oparg + 1 );
3309
+ DISPATCH ();
3310
+ }
3271
3311
}
3272
3312
INSTRUMENTED_JUMP (this_instr , next_instr , PY_MONITORING_EVENT_BRANCH_LEFT );
3273
3313
}
@@ -3298,10 +3338,10 @@ dummy_func(
3298
3338
3299
3339
3300
3340
op (_ITER_CHECK_LIST , (iter , null_or_index -- iter , null_or_index )) {
3301
- PyObject * iter_o = PyStackRef_AsPyObjectBorrow (iter );
3302
- EXIT_IF (Py_TYPE (iter_o ) != & PyList_Type );
3341
+ EXIT_IF (PyStackRef_TYPE (iter ) != & PyList_Type );
3303
3342
assert (PyStackRef_IsTaggedInt (null_or_index ));
3304
3343
#ifdef Py_GIL_DISABLED
3344
+ PyObject * iter_o = PyStackRef_AsPyObjectBorrow (iter );
3305
3345
EXIT_IF (!_Py_IsOwnedByCurrentThread (iter_o ) && !_PyObject_GC_IS_SHARED (iter_o ));
3306
3346
#endif
3307
3347
}
@@ -3383,8 +3423,7 @@ dummy_func(
3383
3423
_ITER_NEXT_LIST ;
3384
3424
3385
3425
op (_ITER_CHECK_TUPLE , (iter , null_or_index -- iter , null_or_index )) {
3386
- PyObject * iter_o = PyStackRef_AsPyObjectBorrow (iter );
3387
- EXIT_IF (Py_TYPE (iter_o ) != & PyTuple_Type );
3426
+ EXIT_IF (PyStackRef_TYPE (iter ) != & PyTuple_Type );
3388
3427
assert (PyStackRef_IsTaggedInt (null_or_index ));
3389
3428
}
3390
3429
@@ -3424,21 +3463,11 @@ dummy_func(
3424
3463
_ITER_NEXT_TUPLE ;
3425
3464
3426
3465
op (_ITER_CHECK_RANGE , (iter , null_or_index -- iter , null_or_index )) {
3427
- _PyRangeIterObject * r = (_PyRangeIterObject * )PyStackRef_AsPyObjectBorrow (iter );
3428
- EXIT_IF (Py_TYPE (r ) != & PyRangeIter_Type );
3429
- #ifdef Py_GIL_DISABLED
3430
- EXIT_IF (!_PyObject_IsUniquelyReferenced ((PyObject * )r ));
3431
- #endif
3466
+ EXIT_IF (!PyStackRef_IsTaggedInt (iter ));
3432
3467
}
3433
3468
3434
3469
replaced op (_ITER_JUMP_RANGE , (iter , null_or_index -- iter , null_or_index )) {
3435
- _PyRangeIterObject * r = (_PyRangeIterObject * )PyStackRef_AsPyObjectBorrow (iter );
3436
- assert (Py_TYPE (r ) == & PyRangeIter_Type );
3437
- #ifdef Py_GIL_DISABLED
3438
- assert (_PyObject_IsUniquelyReferenced ((PyObject * )r ));
3439
- #endif
3440
- STAT_INC (FOR_ITER , hit );
3441
- if (r -> len <= 0 ) {
3470
+ if (PyStackRef_Is (iter , null_or_index )) {
3442
3471
// Jump over END_FOR instruction.
3443
3472
JUMPBY (oparg + 1 );
3444
3473
DISPATCH ();
@@ -3447,24 +3476,15 @@ dummy_func(
3447
3476
3448
3477
// Only used by Tier 2
3449
3478
op (_GUARD_NOT_EXHAUSTED_RANGE , (iter , null_or_index -- iter , null_or_index )) {
3450
- _PyRangeIterObject * r = (_PyRangeIterObject * )PyStackRef_AsPyObjectBorrow (iter );
3451
- assert (Py_TYPE (r ) == & PyRangeIter_Type );
3452
- EXIT_IF (r -> len <= 0 );
3479
+ EXIT_IF (PyStackRef_Is (iter , null_or_index ));
3453
3480
}
3454
3481
3455
3482
op (_ITER_NEXT_RANGE , (iter , null_or_index -- iter , null_or_index , next )) {
3456
- _PyRangeIterObject * r = (_PyRangeIterObject * )PyStackRef_AsPyObjectBorrow (iter );
3457
- assert (Py_TYPE (r ) == & PyRangeIter_Type );
3458
- #ifdef Py_GIL_DISABLED
3459
- assert (_PyObject_IsUniquelyReferenced ((PyObject * )r ));
3460
- #endif
3461
- assert (r -> len > 0 );
3462
- long value = r -> start ;
3463
- r -> start = value + r -> step ;
3464
- r -> len -- ;
3465
- PyObject * res = PyLong_FromLong (value );
3466
- ERROR_IF (res == NULL );
3467
- next = PyStackRef_FromPyObjectSteal (res );
3483
+ next = PyStackRef_BoxInt (null_or_index );
3484
+ if (PyStackRef_IsNull (next )) {
3485
+ ERROR_NO_POP ();
3486
+ }
3487
+ null_or_index = PyStackRef_IncrementTaggedIntNoOverflow (null_or_index );
3468
3488
}
3469
3489
3470
3490
macro (FOR_ITER_RANGE ) =
@@ -3474,8 +3494,8 @@ dummy_func(
3474
3494
_ITER_NEXT_RANGE ;
3475
3495
3476
3496
op (_FOR_ITER_GEN_FRAME , (iter , null -- iter , null , gen_frame : _PyInterpreterFrame * )) {
3497
+ DEOPT_IF (PyStackRef_TYPE (iter ) != & PyGen_Type );
3477
3498
PyGenObject * gen = (PyGenObject * )PyStackRef_AsPyObjectBorrow (iter );
3478
- DEOPT_IF (Py_TYPE (gen ) != & PyGen_Type );
3479
3499
#ifdef Py_GIL_DISABLED
3480
3500
// Since generators can't be used by multiple threads anyway we
3481
3501
// don't need to deopt here, but this lets us work on making
0 commit comments