Commits

Alex Gaynor  committed 247a9d2

introduce caching of constant floats

  • Participants
  • Parent commits 8181d1a

Comments (0)

Files changed (2)

File minijs/astcompiler.py

         self.data = []
         self.consts = []
         self.names = {}
+        self.float_consts = {}
 
     def emit(self, bc, *args):
         self.data.append(chr(bc))
         return self.names[name]
 
     def create_float_const(self, floatval):
-        pos = len(self.consts)
-        self.consts.append(self.space.newfloat(floatval))
-        return pos
+        if floatval not in self.float_consts:
+            self.float_consts[floatval] = len(self.consts)
+            self.consts.append(self.space.newfloat(floatval))
+        return self.float_consts[floatval]
 
     def create_bytecode(self):
         bcs = "".join(self.data)

File tests/test_compiler.py

         LOAD_CONST 1
         BINARY_MUL
         LOAD_CONST 2
-        LOAD_CONST 3 # should be 1 after we merge consts
+        LOAD_CONST 1
         BINARY_DIV
         BINARY_SUB
         DISCARD_TOP
         LOAD_CONST 0
         JUMP_IF_FALSE 10
         LOAD_CONST 1
-        LOAD_CONST 2 # will be 1 after merge consts
+        LOAD_CONST 1
         BINARY_ADD
         DISCARD_TOP
         RETURN_NULL