[pypy-svn] r13205 - pypy/dist/pypy/rpython

arigo at codespeak.net arigo at codespeak.net
Wed Jun 8 23:35:38 CEST 2005


Author: arigo
Date: Wed Jun  8 23:35:32 2005
New Revision: 13205

Modified:
   pypy/dist/pypy/rpython/rbool.py
   pypy/dist/pypy/rpython/rbuiltin.py
   pypy/dist/pypy/rpython/rclass.py
   pypy/dist/pypy/rpython/rfloat.py
   pypy/dist/pypy/rpython/rint.py
   pypy/dist/pypy/rpython/rlist.py
   pypy/dist/pypy/rpython/rmodel.py
   pypy/dist/pypy/rpython/robject.py
   pypy/dist/pypy/rpython/rpbc.py
   pypy/dist/pypy/rpython/rptr.py
   pypy/dist/pypy/rpython/rstr.py
   pypy/dist/pypy/rpython/rtuple.py
   pypy/dist/pypy/rpython/rtyper.py
Log:
Fix the caching logic in getrepr().  Now each rtyper_makerepr() method must
come together with a rtyper_makekey(), which must return something hashable
with the following rule: if two SomeXxx() instances would produce different
representations with rtyper_makerepr(), then the keys must be different.  In
other words, rtyper_makekey() should return something like a tuple of all the
data from the SomeXxx() instance that are relevant for choosing the
representation.

All this because SomeXxx() are not hashable -- which is good but not here :-/


Modified: pypy/dist/pypy/rpython/rbool.py
==============================================================================
--- pypy/dist/pypy/rpython/rbool.py	(original)
+++ pypy/dist/pypy/rpython/rbool.py	Wed Jun  8 23:35:32 2005
@@ -9,6 +9,8 @@
 class __extend__(annmodel.SomeBool):
     def rtyper_makerepr(self, rtyper):
         return bool_repr
+    def rtyper_makekey(self):
+        return None
 
 bool_repr = BoolRepr()
 

Modified: pypy/dist/pypy/rpython/rbuiltin.py
==============================================================================
--- pypy/dist/pypy/rpython/rbuiltin.py	(original)
+++ pypy/dist/pypy/rpython/rbuiltin.py	Wed Jun  8 23:35:32 2005
@@ -20,6 +20,11 @@
             assert self.methodname is not None
             return BuiltinMethodRepr(rtyper.getrepr(self.s_self),
                                      self.methodname)
+    def rtyper_makekey(self):
+        key = (getattr(self, 'const', None), self.methodname)
+        if self.s_self is not None:
+            key += (self.s_self.rtyper_makekey(),)
+        return key
 
 
 class BuiltinFunctionRepr(Repr):

Modified: pypy/dist/pypy/rpython/rclass.py
==============================================================================
--- pypy/dist/pypy/rpython/rclass.py	(original)
+++ pypy/dist/pypy/rpython/rclass.py	Wed Jun  8 23:35:32 2005
@@ -245,6 +245,8 @@
 class __extend__(annmodel.SomeInstance):
     def rtyper_makerepr(self, rtyper):
         return getinstancerepr(rtyper, self.classdef)
+    def rtyper_makekey(self):
+        return self.classdef
 
 
 class InstanceRepr(Repr):

Modified: pypy/dist/pypy/rpython/rfloat.py
==============================================================================
--- pypy/dist/pypy/rpython/rfloat.py	(original)
+++ pypy/dist/pypy/rpython/rfloat.py	Wed Jun  8 23:35:32 2005
@@ -10,6 +10,9 @@
 class __extend__(annmodel.SomeFloat):
     def rtyper_makerepr(self, rtyper):
         return float_repr
+    def rtyper_makekey(self):
+        return None
+
 
 float_repr = FloatRepr()
 

Modified: pypy/dist/pypy/rpython/rint.py
==============================================================================
--- pypy/dist/pypy/rpython/rint.py	(original)
+++ pypy/dist/pypy/rpython/rint.py	Wed Jun  8 23:35:32 2005
@@ -13,6 +13,8 @@
             return unsigned_repr
         else:
             return signed_repr
+    def rtyper_makekey(self):
+        return self.unsigned
 
 signed_repr = IntegerRepr()
 unsigned_repr = IntegerRepr()

Modified: pypy/dist/pypy/rpython/rlist.py
==============================================================================
--- pypy/dist/pypy/rpython/rlist.py	(original)
+++ pypy/dist/pypy/rpython/rlist.py	Wed Jun  8 23:35:32 2005
@@ -26,12 +26,14 @@
             # cannot do the rtyper.getrepr() call immediately, for the case
             # of recursive structures -- i.e. if the listdef contains itself
             return ListRepr(lambda: rtyper.getrepr(listitem.s_value),
-                            self.listdef)
+                            listitem)
+    def rtyper_makekey(self):
+        return self.listdef.listitem
 
 
 class ListRepr(Repr):
 
-    def __init__(self, item_repr, listdef=None):
+    def __init__(self, item_repr, listitem=None):
         self.LIST = GcForwardReference()
         self.lowleveltype = Ptr(self.LIST)
         if not isinstance(item_repr, Repr):  # not computed yet, done by setup()
@@ -39,7 +41,7 @@
             self._item_repr_computer = item_repr
         else:
             self.item_repr = item_repr
-        self.listdef = listdef
+        self.listitem = listitem
         # setup() needs to be called to finish this initialization
 
     def setup(self):
@@ -75,9 +77,9 @@
 
 class __extend__(pairtype(ListRepr, ListRepr)):
     def convert_from_to((r_lst1, r_lst2), v, llops):
-        if r_lst1.listdef is None or r_lst2.listdef is None:
+        if r_lst1.listitem is None or r_lst2.listitem is None:
             return NotImplemented
-        if not r_lst1.listdef.same_as(r_lst2.listdef):
+        if r_lst1.listitem is not r_lst2.listitem:
             return NotImplemented
         return v
 

Modified: pypy/dist/pypy/rpython/rmodel.py
==============================================================================
--- pypy/dist/pypy/rpython/rmodel.py	(original)
+++ pypy/dist/pypy/rpython/rmodel.py	Wed Jun  8 23:35:32 2005
@@ -84,10 +84,14 @@
     def rtyper_makerepr(self, rtyper):
         r_container = rtyper.getrepr(self.s_container)
         return r_container.make_iterator_repr()
+    def rtyper_makekey(self):
+        return self.s_container.rtyper_makekey()
 
 class __extend__(annmodel.SomeImpossibleValue):
     def rtyper_makerepr(self, rtyper):
         return impossible_repr
+    def rtyper_makekey(self):
+        return None
 
 # ____________________________________________________________
 

Modified: pypy/dist/pypy/rpython/robject.py
==============================================================================
--- pypy/dist/pypy/rpython/robject.py	(original)
+++ pypy/dist/pypy/rpython/robject.py	Wed Jun  8 23:35:32 2005
@@ -13,6 +13,13 @@
             return rclass.get_type_repr(rtyper)
         else:
             return pyobj_repr
+    def rtyper_makekey(self):
+        if self.is_constant():
+            return "const"
+        if self.knowntype is type:
+            return "type"
+        else:
+            return "pyobj"
 
 
 class PyObjRepr(Repr):

Modified: pypy/dist/pypy/rpython/rpbc.py
==============================================================================
--- pypy/dist/pypy/rpython/rpbc.py	(original)
+++ pypy/dist/pypy/rpython/rpbc.py	Wed Jun  8 23:35:32 2005
@@ -51,6 +51,11 @@
         reprcls, = choices
         return reprcls(rtyper, self)
 
+    def rtyper_makekey(self):
+        lst = self.prebuiltinstances.items()
+        lst.sort()
+        return tuple(lst)
+
 
 # ____________________________________________________________
 

Modified: pypy/dist/pypy/rpython/rptr.py
==============================================================================
--- pypy/dist/pypy/rpython/rptr.py	(original)
+++ pypy/dist/pypy/rpython/rptr.py	Wed Jun  8 23:35:32 2005
@@ -11,6 +11,11 @@
             return nullptr_repr
         else:
             return PtrRepr(self.ll_ptrtype)
+    def rtyper_makekey(self):
+        if self.is_constant():
+            return None
+        else:
+            return self.ll_ptrtype
 
 
 class PtrRepr(Repr):

Modified: pypy/dist/pypy/rpython/rstr.py
==============================================================================
--- pypy/dist/pypy/rpython/rstr.py	(original)
+++ pypy/dist/pypy/rpython/rstr.py	Wed Jun  8 23:35:32 2005
@@ -24,10 +24,14 @@
 class __extend__(annmodel.SomeString):
     def rtyper_makerepr(self, rtyper):
         return string_repr
+    def rtyper_makekey(self):
+        return None
 
 class __extend__(annmodel.SomeChar):
     def rtyper_makerepr(self, rtyper):
         return char_repr
+    def rtyper_makekey(self):
+        return None
 
 
 CONST_STR_CACHE = WeakValueDictionary()

Modified: pypy/dist/pypy/rpython/rtuple.py
==============================================================================
--- pypy/dist/pypy/rpython/rtuple.py	(original)
+++ pypy/dist/pypy/rpython/rtuple.py	Wed Jun  8 23:35:32 2005
@@ -18,6 +18,9 @@
 class __extend__(annmodel.SomeTuple):
     def rtyper_makerepr(self, rtyper):
         return TupleRepr([rtyper.getrepr(s_item) for s_item in self.items])
+    def rtyper_makekey(self):
+        keys = [s_item.rtyper_makekey() for s_item in self.items]
+        return tuple(keys)
 
 
 class TupleRepr(Repr):

Modified: pypy/dist/pypy/rpython/rtyper.py
==============================================================================
--- pypy/dist/pypy/rpython/rtyper.py	(original)
+++ pypy/dist/pypy/rpython/rtyper.py	Wed Jun  8 23:35:32 2005
@@ -24,8 +24,7 @@
 
     def __init__(self, annotator):
         self.annotator = annotator
-        self.reprs_by_id = {}
-        self.reprs_by_content = {}
+        self.reprs = {}
         self.reprs_must_call_setup = []
         self.specialized_ll_functions = {}
         self.class_reprs = {}
@@ -38,28 +37,17 @@
             self.primitive_to_repr[r.lowleveltype] = r
 
     def getrepr(self, s_obj):
-        # s_objs are not hashable... try hard to find a hash anyway
+        # s_objs are not hashable... try hard to find a unique key anyway
+        key = s_obj.__class__, s_obj.rtyper_makekey()
         try:
-            result, s_obj = self.reprs_by_id[id(s_obj)]
+            result = self.reprs[key]
         except KeyError:
-            key = [s_obj.__class__]
-            items = s_obj.__dict__.items()
-            items.sort()
-            for name, value in items:
-                key.append(name)
-                key.append(Constant(value))
-            key = tuple(key)
-            try:
-                result = self.reprs_by_content[key]
-            except KeyError:
-                # here is the code that actually builds a Repr instance
-                result = s_obj.rtyper_makerepr(self)
-                assert not isinstance(result.lowleveltype, ContainerType), (
-                    "missing a Ptr in the type specification "
-                    "of %s:\n%r" % (s_obj, result.lowleveltype))
-                self.reprs_by_content[key] = result
-                self.reprs_must_call_setup.append(result)
-            self.reprs_by_id[id(s_obj)] = result, s_obj
+            result = s_obj.rtyper_makerepr(self)
+            assert not isinstance(result.lowleveltype, ContainerType), (
+                "missing a Ptr in the type specification "
+                "of %s:\n%r" % (s_obj, result.lowleveltype))
+            self.reprs[key] = result
+            self.reprs_must_call_setup.append(result)
         return result
 
     def binding(self, var):



More information about the Pypy-commit mailing list