@@ -1429,35 +1429,34 @@ fold_tuple_of_constants(basicblock *bb, int n, PyObject *consts, PyObject *const
14291429
14301430#define MIN_CONST_SEQUENCE_SIZE 3
14311431/*
1432- Optimize literal list/set for:
1432+ Optimize lists and sets for:
14331433 1. "for" loop, comprehension or "in"/"not in" tests:
14341434 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
14361436 non-constants into tuple.
14371437 2. Constant literal lists/set with length >= MIN_CONST_SEQUENCE_SIZE:
14381438 Replace LOAD_CONST c1, LOAD_CONST c2 ... LOAD_CONST cN, BUILD_LIST N
14391439 with BUILD_LIST 0, LOAD_CONST (c1, c2, ... cN), LIST_EXTEND 1,
14401440 or BUILD_SET & SET_UPDATE respectively.
14411441*/
14421442static 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 )
14451445{
14461446 assert (PyDict_CheckExact (const_cache ));
14471447 assert (PyList_CheckExact (consts ));
14481448 cfg_instr * instr = & bb -> b_instr [i ];
14491449 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 ;
14511451 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 ) {
14531453 return SUCCESS ;
14541454 }
14551455 PyObject * newconst ;
14561456 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 */
14611460 INSTR_SET_OP1 (instr , BUILD_TUPLE , instr -> i_oparg );
14621461 }
14631462 return SUCCESS ;
@@ -1474,11 +1473,12 @@ optimize_list_or_set_literal(basicblock *bb, int i, int nextop,
14741473 int index = add_const (newconst , consts , const_cache );
14751474 RETURN_IF_ERROR (index );
14761475 nop_out (bb , i - 1 , seq_size );
1477- if (contains_or_iter_literal ) {
1476+ if (contains_or_iter ) {
14781477 INSTR_SET_OP1 (instr , LOAD_CONST , index );
14791478 }
14801479 else {
14811480 assert (i >= 2 );
1481+ assert (instr -> i_opcode == BUILD_LIST || instr -> i_opcode == BUILD_SET );
14821482 INSTR_SET_OP1 (& bb -> b_instr [i - 2 ], instr -> i_opcode , 0 );
14831483 INSTR_SET_OP1 (& bb -> b_instr [i - 1 ], LOAD_CONST , index );
14841484 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)
19391939 break ;
19401940 case BUILD_LIST :
19411941 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 ));
19431943 break ;
19441944 case POP_JUMP_IF_NOT_NONE :
19451945 case POP_JUMP_IF_NONE :
0 commit comments