From: Raymond Hettinger Date: Wed, 26 Mar 2003 01:07:54 +0000 (+0000) Subject: SF patch #707257: Improve code generation X-Git-Tag: v2.3c1~1366 X-Git-Url: https://granicus.if.org/sourcecode?a=commitdiff_plain;h=f6f575ae6fc4b58f8735b6aebaa422d48bedcef4;p=python 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". --- diff --git a/Python/compile.c b/Python/compile.c index 6616c58882..8c259c1230 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);