@@ -1429,35 +1429,34 @@ fold_tuple_of_constants(basicblock *bb, int n, PyObject *consts, PyObject *const
1429
1429
1430
1430
#define MIN_CONST_SEQUENCE_SIZE 3
1431
1431
/*
1432
- Optimize literal list/set for:
1432
+ Optimize lists and sets for:
1433
1433
1. "for" loop, comprehension or "in"/"not in" tests:
1434
1434
Change literal list or set of constants into constant
1435
- tuple or frozenset respectively. Change literal list of
1435
+ tuple or frozenset respectively. Change list of
1436
1436
non-constants into tuple.
1437
1437
2. Constant literal lists/set with length >= MIN_CONST_SEQUENCE_SIZE:
1438
1438
Replace LOAD_CONST c1, LOAD_CONST c2 ... LOAD_CONST cN, BUILD_LIST N
1439
1439
with BUILD_LIST 0, LOAD_CONST (c1, c2, ... cN), LIST_EXTEND 1,
1440
1440
or BUILD_SET & SET_UPDATE respectively.
1441
1441
*/
1442
1442
static int
1443
- optimize_list_or_set_literal (basicblock * bb , int i , int nextop ,
1444
- PyObject * consts , PyObject * const_cache )
1443
+ optimize_lists_and_sets (basicblock * bb , int i , int nextop ,
1444
+ PyObject * consts , PyObject * const_cache )
1445
1445
{
1446
1446
assert (PyDict_CheckExact (const_cache ));
1447
1447
assert (PyList_CheckExact (consts ));
1448
1448
cfg_instr * instr = & bb -> b_instr [i ];
1449
1449
assert (instr -> i_opcode == BUILD_LIST || instr -> i_opcode == BUILD_SET );
1450
- bool contains_or_iter_literal = nextop == GET_ITER || nextop == CONTAINS_OP ;
1450
+ bool contains_or_iter = nextop == GET_ITER || nextop == CONTAINS_OP ;
1451
1451
int seq_size = instr -> i_oparg ;
1452
- if (seq_size < MIN_CONST_SEQUENCE_SIZE && !contains_or_iter_literal ) {
1452
+ if (seq_size < MIN_CONST_SEQUENCE_SIZE && !contains_or_iter ) {
1453
1453
return SUCCESS ;
1454
1454
}
1455
1455
PyObject * newconst ;
1456
1456
RETURN_IF_ERROR (get_constant_sequence (bb , i - 1 , seq_size , consts , & newconst ));
1457
- if (newconst == NULL ) {
1458
- /* not a const sequence */
1459
- if (contains_or_iter_literal && instr -> i_opcode == BUILD_LIST ) {
1460
- /* convert list iterable to tuple */
1457
+ if (newconst == NULL ) { /* not a const sequence */
1458
+ if (contains_or_iter && instr -> i_opcode == BUILD_LIST ) {
1459
+ /* iterate over a tuple instead of list */
1461
1460
INSTR_SET_OP1 (instr , BUILD_TUPLE , instr -> i_oparg );
1462
1461
}
1463
1462
return SUCCESS ;
@@ -1474,11 +1473,12 @@ optimize_list_or_set_literal(basicblock *bb, int i, int nextop,
1474
1473
int index = add_const (newconst , consts , const_cache );
1475
1474
RETURN_IF_ERROR (index );
1476
1475
nop_out (bb , i - 1 , seq_size );
1477
- if (contains_or_iter_literal ) {
1476
+ if (contains_or_iter ) {
1478
1477
INSTR_SET_OP1 (instr , LOAD_CONST , index );
1479
1478
}
1480
1479
else {
1481
1480
assert (i >= 2 );
1481
+ assert (instr -> i_opcode == BUILD_LIST || instr -> i_opcode == BUILD_SET );
1482
1482
INSTR_SET_OP1 (& bb -> b_instr [i - 2 ], instr -> i_opcode , 0 );
1483
1483
INSTR_SET_OP1 (& bb -> b_instr [i - 1 ], LOAD_CONST , index );
1484
1484
INSTR_SET_OP1 (& bb -> b_instr [i ], instr -> i_opcode == BUILD_LIST ? LIST_EXTEND : SET_UPDATE , 1 );
@@ -1939,7 +1939,7 @@ optimize_basic_block(PyObject *const_cache, basicblock *bb, PyObject *consts)
1939
1939
break ;
1940
1940
case BUILD_LIST :
1941
1941
case BUILD_SET :
1942
- RETURN_IF_ERROR (optimize_list_or_set_literal (bb , i , nextop , consts , const_cache ));
1942
+ RETURN_IF_ERROR (optimize_lists_and_sets (bb , i , nextop , consts , const_cache ));
1943
1943
break ;
1944
1944
case POP_JUMP_IF_NOT_NONE :
1945
1945
case POP_JUMP_IF_NONE :
0 commit comments