From 7fcb7869ba82586f68e0cf28c3a25e78457fa0e0 Mon Sep 17 00:00:00 2001 From: Raymond Hettinger Date: Mon, 7 Feb 2005 19:32:38 +0000 Subject: [PATCH] Adopt Skip's idea to optimize lists of constants in the context of a "in" or "not in" test. --- Lib/test/test_peepholer.py | 3 ++- Python/compile.c | 20 +++++++++++++------- 2 files changed, 15 insertions(+), 8 deletions(-) diff --git a/Lib/test/test_peepholer.py b/Lib/test/test_peepholer.py index bedf7632492..0e723ebec6c 100644 --- a/Lib/test/test_peepholer.py +++ b/Lib/test/test_peepholer.py @@ -135,7 +135,8 @@ class TestTranforms(unittest.TestCase): def test_set_conversion(self): for line in ( - 'x in (1,2,3)', + 'x in [1,2,3]', + 'x in (1,2,3)', 'x not in (1,2,3)', 'not x in (1,2,3)', 'not x not in (1,2,3)', diff --git a/Python/compile.c b/Python/compile.c index de07c974000..dd58aa64280 100644 --- a/Python/compile.c +++ b/Python/compile.c @@ -397,7 +397,9 @@ intern_strings(PyObject *tuple) The consts table must still be in list form so that the new constant (c1, c2, ... cn) can be appended. Called with codestr pointing to the first LOAD_CONST. - Bails out with no change if one or more of the LOAD_CONSTs is missing. */ + Bails out with no change if one or more of the LOAD_CONSTs is missing. + Also works for BUILD_LIST when followed by an "in" or "not in" test. +*/ static int tuple_of_constants(unsigned char *codestr, int n, PyObject *consts) { @@ -406,7 +408,7 @@ tuple_of_constants(unsigned char *codestr, int n, PyObject *consts) /* Pre-conditions */ assert(PyList_CheckExact(consts)); - assert(codestr[n*3] == BUILD_TUPLE); + assert(codestr[n*3] == BUILD_TUPLE || codestr[n*3] == BUILD_LIST); assert(GETARG(codestr, (n*3)) == n); for (i=0 ; i= 0 && j <= lastlc && - ISBASICBLOCK(blocks, h, 3*(j+1)) && + (opcode == BUILD_TUPLE && + ISBASICBLOCK(blocks, h, 3*(j+1)) || + opcode == BUILD_LIST && + codestr[i+3]==COMPARE_OP && + ISBASICBLOCK(blocks, h, 3*(j+2)) && + (GETARG(codestr,i+3)==6 || GETARG(codestr,i+3)==7)) && tuple_of_constants(&codestr[h], j, consts)) { assert(codestr[i] == LOAD_CONST); cumlc = 1; break; } - /* Intentional fallthrough */ - case BUILD_LIST: - j = GETARG(codestr, i); if (codestr[i+3] != UNPACK_SEQUENCE || !ISBASICBLOCK(blocks,i,6) || j != GETARG(codestr, i+3))