From ff5bc50bb0f12d5203173c7ee6840cc77a3bbe7a Mon Sep 17 00:00:00 2001 From: Raymond Hettinger Date: Sun, 21 Mar 2004 15:12:00 +0000 Subject: [PATCH] Improve byte coding for multiple assignments. Gives 30% speedup on "a,b=1,2" and 25% on "a,b,c=1,2,3". --- Misc/NEWS | 3 ++ Python/compile.c | 77 ++++++++++++++++++++++++++++++++++++++++++++++-- 2 files changed, 77 insertions(+), 3 deletions(-) diff --git a/Misc/NEWS b/Misc/NEWS index 9f27fd7365..f543eca373 100644 --- a/Misc/NEWS +++ b/Misc/NEWS @@ -12,6 +12,9 @@ What's New in Python 2.4 alpha 1? Core and builtins ----------------- +- Optimized the byte coding for multiple assignments like "a,b=b,a" and + "a,b,c=1,2,3". Improves their speed by 25% to 30%. + - Limit the nested depth of a tuple for the second argument to isinstance() and issubclass() to the recursion limit of the interpreter. Fixes bug #858016 . diff --git a/Python/compile.c b/Python/compile.c index f58fb83efc..328c57bb0f 100644 --- a/Python/compile.c +++ b/Python/compile.c @@ -327,6 +327,43 @@ intern_strings(PyObject *tuple) #define ABSOLUTE_JUMP(op) (op==JUMP_ABSOLUTE || op==CONTINUE_LOOP) #define GETJUMPTGT(arr, i) (GETARG(arr,i) + (ABSOLUTE_JUMP(arr[i]) ? 0 : i+3)) #define SETARG(arr, i, val) arr[i+2] = val>>8; arr[i+1] = val & 255 +#define CODESIZE(op) (HAS_ARG(op) ? 3 : 1) +#define ISBASICBLOCK(blocks, start, bytes) (blocks[start]==blocks[start+bytes-1]) + +static unsigned int * +markblocks(unsigned char *code, int len) +{ + unsigned int *blocks = PyMem_Malloc(len*sizeof(int)); + int i,j, opcode, oldblock, newblock, blockcnt = 0; + + if (blocks == NULL) + return NULL; + memset(blocks, 0, len*sizeof(int)); + for (i=0 ; i