diff options
author | Antoine Pitrou <solipsis@pitrou.net> | 2008-12-17 00:38:28 (GMT) |
---|---|---|
committer | Antoine Pitrou <solipsis@pitrou.net> | 2008-12-17 00:38:28 (GMT) |
commit | d0c3515bc5b31a19d00bfc685d7657ad7d79fa94 (patch) | |
tree | 299253e76cf9b66349bdaf0cca8c95da19671c74 /Python | |
parent | 43caaa09ea364aab6cbd7ede2aa9c3d004a129a5 (diff) | |
download | cpython-d0c3515bc5b31a19d00bfc685d7657ad7d79fa94.zip cpython-d0c3515bc5b31a19d00bfc685d7657ad7d79fa94.tar.gz cpython-d0c3515bc5b31a19d00bfc685d7657ad7d79fa94.tar.bz2 |
Issue #2183: Simplify and optimize bytecode for list comprehensions.
Diffstat (limited to 'Python')
-rw-r--r-- | Python/ceval.c | 3 | ||||
-rw-r--r-- | Python/compile.c | 33 | ||||
-rw-r--r-- | Python/import.c | 3 |
3 files changed, 10 insertions, 29 deletions
diff --git a/Python/ceval.c b/Python/ceval.c index bd35185..09501af 100644 --- a/Python/ceval.c +++ b/Python/ceval.c @@ -1294,9 +1294,8 @@ PyEval_EvalFrameEx(PyFrameObject *f, int throwflag) case LIST_APPEND: w = POP(); - v = POP(); + v = stack_pointer[-oparg]; err = PyList_Append(v, w); - Py_DECREF(v); Py_DECREF(w); if (err == 0) { PREDICT(JUMP_ABSOLUTE); diff --git a/Python/compile.c b/Python/compile.c index 88d54ab..756a903 100644 --- a/Python/compile.c +++ b/Python/compile.c @@ -693,7 +693,7 @@ opcode_stack_effect(int opcode, int oparg) return 0; case LIST_APPEND: - return -2; + return -1; case BINARY_POWER: case BINARY_MULTIPLY: @@ -2599,9 +2599,8 @@ compiler_call(struct compiler *c, expr_ty e) } static int -compiler_listcomp_generator(struct compiler *c, PyObject *tmpname, - asdl_seq *generators, int gen_index, - expr_ty elt) +compiler_listcomp_generator(struct compiler *c, asdl_seq *generators, + int gen_index, expr_ty elt) { /* generate code for the iterator, then each of the ifs, and then write to the element */ @@ -2638,16 +2637,13 @@ compiler_listcomp_generator(struct compiler *c, PyObject *tmpname, } if (++gen_index < asdl_seq_LEN(generators)) - if (!compiler_listcomp_generator(c, tmpname, - generators, gen_index, elt)) + if (!compiler_listcomp_generator(c, generators, gen_index, elt)) return 0; /* only append after the last for generator */ if (gen_index >= asdl_seq_LEN(generators)) { - if (!compiler_nameop(c, tmpname, Load)) - return 0; VISIT(c, expr, elt); - ADDOP(c, LIST_APPEND); + ADDOP_I(c, LIST_APPEND, gen_index+1); compiler_use_next_block(c, skip); } @@ -2659,10 +2655,6 @@ compiler_listcomp_generator(struct compiler *c, PyObject *tmpname, } ADDOP_JABS(c, JUMP_ABSOLUTE, start); compiler_use_next_block(c, anchor); - /* delete the temporary list name added to locals */ - if (gen_index == 1) - if (!compiler_nameop(c, tmpname, Del)) - return 0; return 1; } @@ -2670,21 +2662,10 @@ compiler_listcomp_generator(struct compiler *c, PyObject *tmpname, static int compiler_listcomp(struct compiler *c, expr_ty e) { - identifier tmp; - int rc = 0; - asdl_seq *generators = e->v.ListComp.generators; - assert(e->kind == ListComp_kind); - tmp = compiler_new_tmpname(c); - if (!tmp) - return 0; ADDOP_I(c, BUILD_LIST, 0); - ADDOP(c, DUP_TOP); - if (compiler_nameop(c, tmp, Store)) - rc = compiler_listcomp_generator(c, tmp, generators, 0, - e->v.ListComp.elt); - Py_DECREF(tmp); - return rc; + return compiler_listcomp_generator(c, e->v.ListComp.generators, 0, + e->v.ListComp.elt); } static int diff --git a/Python/import.c b/Python/import.c index 781bb48..40fc018 100644 --- a/Python/import.c +++ b/Python/import.c @@ -73,9 +73,10 @@ extern time_t PyOS_GetLastModificationTime(char *, FILE *); Python 2.5c2: 62131 (fix wrong code: for x, in ... in listcomp/genexp) Python 2.6a0: 62151 (peephole optimizations and STORE_MAP opcode) Python 2.6a1: 62161 (WITH_CLEANUP optimization) + Python 2.7a0: 62171 (optimize list comprehensions/change LIST_APPEND) . */ -#define MAGIC (62161 | ((long)'\r'<<16) | ((long)'\n'<<24)) +#define MAGIC (62171 | ((long)'\r'<<16) | ((long)'\n'<<24)) /* Magic word as global; note that _PyImport_Init() can change the value of this global to accommodate for alterations of how the |