[pypy-svn] r66424 - pypy/branch/parser-compiler/pypy/interpreter/astcompiler

benjamin at codespeak.net benjamin at codespeak.net
Mon Jul 20 01:29:38 CEST 2009


Author: benjamin
Date: Mon Jul 20 01:29:37 2009
New Revision: 66424

Modified:
   pypy/branch/parser-compiler/pypy/interpreter/astcompiler/assemble.py
   pypy/branch/parser-compiler/pypy/interpreter/astcompiler/codegen.py
   pypy/branch/parser-compiler/pypy/interpreter/astcompiler/misc.py
Log:
turn static dict lookups into switches

Modified: pypy/branch/parser-compiler/pypy/interpreter/astcompiler/assemble.py
==============================================================================
--- pypy/branch/parser-compiler/pypy/interpreter/astcompiler/assemble.py	(original)
+++ pypy/branch/parser-compiler/pypy/interpreter/astcompiler/assemble.py	Mon Jul 20 01:29:37 2009
@@ -489,10 +489,12 @@
 _stack_effect_computers = {}
 for name, func in globals().items():
     if name.startswith("_compute_"):
+        func._always_inline_ = True
         _stack_effect_computers[getattr(ops, name[9:])] = func
 for op, value in _static_opcode_stack_effects.iteritems():
     def func(arg, _value=value):
         return _value
+    func._always_inline_ = True
     _stack_effect_computers[op] = func
 del name, func, op, value
 

Modified: pypy/branch/parser-compiler/pypy/interpreter/astcompiler/codegen.py
==============================================================================
--- pypy/branch/parser-compiler/pypy/interpreter/astcompiler/codegen.py	(original)
+++ pypy/branch/parser-compiler/pypy/interpreter/astcompiler/codegen.py	Mon Jul 20 01:29:37 2009
@@ -14,38 +14,38 @@
     return TopLevelCodeGenerator(space, module, symbols, info).assemble()
 
 
-name_ops_default = {
+name_ops_default = misc.dict_to_switch({
     ast.Load : ops.LOAD_NAME,
     ast.Store : ops.STORE_NAME,
     ast.Del : ops.DELETE_NAME
-}
+})
 
-name_ops_fast = {
+name_ops_fast = misc.dict_to_switch({
     ast.Load : ops.LOAD_FAST,
     ast.Store : ops.STORE_FAST,
     ast.Del : ops.DELETE_FAST
-}
+})
 
-name_ops_deref = {
+name_ops_deref = misc.dict_to_switch({
     ast.Load : ops.LOAD_DEREF,
     ast.Store : ops.STORE_DEREF,
-}
+})
 
-name_ops_global = {
+name_ops_global = misc.dict_to_switch({
     ast.Load : ops.LOAD_GLOBAL,
     ast.Store : ops.STORE_GLOBAL,
     ast.Del : ops.DELETE_GLOBAL
-}
+})
 
 
-unary_operations = {
+unary_operations = misc.dict_to_switch({
     ast.Invert : ops.UNARY_INVERT,
     ast.Not : ops.UNARY_NOT,
     ast.UAdd : ops.UNARY_POSITIVE,
     ast.USub : ops.UNARY_NEGATIVE
-}
+})
 
-binary_operations = {
+binary_operations = misc.dict_to_switch({
     ast.Add : ops.BINARY_ADD,
     ast.Sub : ops.BINARY_SUBTRACT,
     ast.Mult : ops.BINARY_MULTIPLY,
@@ -57,9 +57,9 @@
     ast.BitAnd : ops.BINARY_AND,
     ast.BitXor : ops.BINARY_XOR,
     ast.FloorDiv : ops.BINARY_FLOOR_DIVIDE
-}
+})
 
-inplace_operations = {
+inplace_operations = misc.dict_to_switch({
     ast.Add : ops.INPLACE_ADD,
     ast.Sub : ops.INPLACE_SUBTRACT,
     ast.Mult : ops.INPLACE_MULTIPLY,
@@ -71,9 +71,9 @@
     ast.BitAnd : ops.INPLACE_AND,
     ast.BitXor : ops.INPLACE_XOR,
     ast.FloorDiv : ops.INPLACE_FLOOR_DIVIDE
-}
+})
 
-compare_operations = {
+compare_operations = misc.dict_to_switch({
     ast.Eq : 2,
     ast.NotEq : 3,
     ast.Lt : 0,
@@ -84,23 +84,23 @@
     ast.NotIn : 7,
     ast.Is : 8,
     ast.IsNot : 9
-}
+})
 
-subscr_operations = {
+subscr_operations = misc.dict_to_switch({
     ast.AugLoad : ops.BINARY_SUBSCR,
     ast.Load : ops.BINARY_SUBSCR,
     ast.AugStore : ops.STORE_SUBSCR,
     ast.Store : ops.STORE_SUBSCR,
     ast.Del : ops.DELETE_SUBSCR
-}
+})
 
-slice_operations = {
+slice_operations = misc.dict_to_switch({
     ast.AugLoad : ops.SLICE,
     ast.Load : ops.SLICE,
     ast.AugStore : ops.STORE_SLICE,
     ast.Store : ops.STORE_SLICE,
     ast.Del : ops.DELETE_SLICE
-}
+})
 
 
 F_BLOCK_LOOP = 0
@@ -168,7 +168,7 @@
         elif scope == symtable.SCOPE_GLOBAL_EXPLICIT:
             kind = name_ops_global
         try:
-            op = kind[ctx]
+            op = kind(ctx)
         except KeyError:
             if kind is name_ops_deref and ctx == ast.Del:
                 raise SyntaxError("Can't delete variable used in "
@@ -275,7 +275,7 @@
                 return ops.INPLACE_TRUE_DIVIDE
             else:
                 return ops.INPLACE_DIVIDE
-        return inplace_operations[op]
+        return inplace_operations(op)
 
     def visit_AugAssign(self, assign):
         self.update_position(assign.lineno)
@@ -325,7 +325,7 @@
                 return ops.BINARY_TRUE_DIVIDE
             else:
                 return ops.BINARY_DIVIDE
-        return binary_operations[op]
+        return binary_operations(op)
 
     def visit_BinOp(self, binop):
         self.update_position(binop.lineno)
@@ -716,7 +716,7 @@
     def visit_UnaryOp(self, op):
         self.update_position(op.lineno)
         op.operand.walkabout(self)
-        self.emit_op(unary_operations[op.op])
+        self.emit_op(unary_operations(op.op))
 
     def visit_BoolOp(self, op):
         self.update_position(op.lineno)
@@ -743,14 +743,14 @@
         for i in range(1, ops_count):
             self.emit_op(ops.DUP_TOP)
             self.emit_op(ops.ROT_THREE)
-            op_kind = compare_operations[comp.ops[i - 1]]
+            op_kind = compare_operations(comp.ops[i - 1])
             self.emit_op_arg(ops.COMPARE_OP, op_kind)
             self.emit_jump(ops.JUMP_IF_FALSE, cleanup)
             self.emit_op(ops.POP_TOP)
             if i < (ops_count - 1):
                 comp.comparators[i].walkabout(self)
         comp.comparators[-1].walkabout(self)
-        last_kind = compare_operations[comp.ops[-1]]
+        last_kind = compare_operations(comp.ops[-1])
         self.emit_op_arg(ops.COMPARE_OP, last_kind)
         if ops_count > 1:
             end = self.new_block()
@@ -1047,7 +1047,7 @@
                 self.emit_op(ops.ROT_THREE)
             elif stack_count == 2:
                 self.emit_op(ops.ROT_FOUR)
-        self.emit_op(slice_operations[ctx] + slice_offset)
+        self.emit_op(slice_operations(ctx) + slice_offset)
 
     def _complex_slice(self, slc, ctx):
         if slc.lower:
@@ -1102,7 +1102,7 @@
             self.emit_op_arg(ops.DUP_TOPX, 2)
         elif ctx == ast.AugStore:
             self.emit_op(ops.ROT_THREE)
-        self.emit_op(subscr_operations[ctx])
+        self.emit_op(subscr_operations(ctx))
 
     def visit_Subscript(self, sub):
         self.update_position(sub.lineno)

Modified: pypy/branch/parser-compiler/pypy/interpreter/astcompiler/misc.py
==============================================================================
--- pypy/branch/parser-compiler/pypy/interpreter/astcompiler/misc.py	(original)
+++ pypy/branch/parser-compiler/pypy/interpreter/astcompiler/misc.py	Mon Jul 20 01:29:37 2009
@@ -1,5 +1,7 @@
 from pypy.interpreter import gateway
 from pypy.interpreter.astcompiler import ast2 as ast
+from pypy.rlib.objectmodel import we_are_translated
+from pypy.rlib.unroll import unrolling_iterable
 
 
 app = gateway.applevel("""
@@ -38,6 +40,21 @@
     return False
 
 
+def dict_to_switch(d):
+    def lookup(query):
+        if we_are_translated():
+            for key, value in unrolling_iteritems:
+                if key == query:
+                    return value
+            else:
+                raise KeyError
+        else:
+            return d[query]
+    lookup._always_inline_ = True
+    unrolling_iteritems = unrolling_iterable(d.iteritems())
+    return lookup
+
+
 def flatten(tup):
     elts = []
     for elt in tup:



More information about the Pypy-commit mailing list