[Python-Dev] LOAD_CONST POP_TOP

Georg Brandl g.brandl at gmx.net
Fri Jun 30 20:39:13 CEST 2006


Hi,

the following patch tries to fix the LOAD_CONST POP_TOP optimization
lost in 2.5 (bug #1333982).

An example for this is:

def f():
  'a' # docstring
  'b'

Georg

PS: Hmm. While looking, I see that 2.4 doesn't optimize away other constants like

def g():
  1


Index: Python/compile.c
===================================================================
--- Python/compile.c    (Revision 47150)
+++ Python/compile.c    (Arbeitskopie)
@@ -775,10 +775,16 @@
                                 }
                                 break;

+                       case LOAD_CONST:
+                               cumlc = lastlc + 1;
+                               /* Skip over LOAD_CONST POP_TOP */
+                               if (codestr[i+3] == POP_TOP) {
+                                       memset(codestr+i, NOP, 4);
+                                       cumlc = 0;
+                                       break;
+                               }
                                 /* Skip over LOAD_CONST trueconst
                                     JUMP_IF_FALSE xx  POP_TOP */
-                       case LOAD_CONST:
-                               cumlc = lastlc + 1;
                                 j = GETARG(codestr, i);
                                 if (codestr[i+3] != JUMP_IF_FALSE  ||
                                     codestr[i+6] != POP_TOP  ||



More information about the Python-Dev mailing list