From f6f575ae6fc4b58f8735b6aebaa422d48bedcef4 Mon Sep 17 00:00:00 2001 From: Raymond Hettinger Date: Wed, 26 Mar 2003 01:07:54 +0000 Subject: [PATCH] SF patch #707257: Improve code generation Adds a single function to improve generated bytecode. Has a single line attachment point, so it is completely de-coupled from both the compiler and ceval.c. Makes three simple transforms that do not require a basic block analysis or re-ordering of code. Gives improved timings on pystone, pybench, and any code using either "while 1" or "x,y=y,x". --- Python/compile.c | 96 +++++++++++++++++++++++++++++++++++++++++++++++- 1 file changed, 94 insertions(+), 2 deletions(-) diff --git a/Python/compile.c b/Python/compile.c index 6616c588824..8c259c12305 100644 --- a/Python/compile.c +++ b/Python/compile.c @@ -323,6 +323,99 @@ intern_strings(PyObject *tuple) return 0; } +#define GETARG(arr, i) ((int)((arr[i+2]<<8) + arr[i+1])) +#define UNCONDITIONAL_JUMP(op) (op==JUMP_ABSOLUTE || op==JUMP_FORWARD) +#define GETJUMPTGT(arr, i) (GETARG(arr,i) + (arr[i]==JUMP_ABSOLUTE ? 0 : i+3)) +#define SETARG(arr, i, val) arr[i+2] = val>>8; arr[i+1] = val & 255 + +static PyObject * +optimize_code(PyObject *code, PyObject* consts) +{ + int i, j, codelen; + int tgt, tgttgt, opcode; + unsigned char *codestr; + + /* Make a modifiable copy of the code string */ + if (!PyString_Check(code)) + goto exitUnchanged; + codelen = PyString_Size(code); + codestr = PyMem_Malloc(codelen); + if (codestr == NULL) + goto exitUnchanged; + codestr = memcpy(codestr, PyString_AS_STRING(code), codelen); + assert(PyTuple_Check(consts)); + + for (i=0 ; ico_nlocals = nlocals; co->co_stacksize = stacksize; co->co_flags = flags; - Py_INCREF(code); - co->co_code = code; + co->co_code = optimize_code(code, consts); Py_INCREF(consts); co->co_consts = consts; Py_INCREF(names);