Przeglądaj źródła

Optimize PythonGenerator's append method

jonathanvdc 8 lat temu
rodzic
commit
1b597a3b83
1 zmienionych plików z 6 dodań i 4 usunięć
  1. 6 4
      kernel/modelverse_jit/tree_ir.py

+ 6 - 4
kernel/modelverse_jit/tree_ir.py

@@ -75,21 +75,20 @@ class Instruction(object):
     def __str__(self):
         code_generator = PythonGenerator()
         self.generate_python_def(code_generator)
-        return code_generator.code
+        return str(code_generator)
 
 class PythonGenerator(object):
     """Generates Python code from instructions."""
 
     def __init__(self):
-        self.code = ''
+        self.code = []
         self.indentation_string = ' ' * 4
         self.indentation = 0
         self.result_value_dict = {}
 
     def append(self, text):
         """Appends the given string to this code generator."""
-
-        self.code += text
+        self.code.append(text)
 
     def append_indentation(self):
         """Appends indentation to the code generator."""
@@ -160,6 +159,9 @@ class PythonGenerator(object):
                 opcode,
                 ', '.join([arg_i.generate_python_use(self) for arg_i in args])))
 
+    def __str__(self):
+        return ''.join(self.code)
+
 class VoidInstruction(Instruction):
     """A base class for instructions that do not return a value."""