[pypy-svn] r67070 - pypy/branch/pyjitpl5/pypy/jit/metainterp

arigo at codespeak.net arigo at codespeak.net
Fri Aug 21 15:46:51 CEST 2009


Author: arigo
Date: Fri Aug 21 15:46:51 2009
New Revision: 67070

Modified:
   pypy/branch/pyjitpl5/pypy/jit/metainterp/codewriter.py
   pypy/branch/pyjitpl5/pypy/jit/metainterp/history.py
Log:
(iko, arigo)
Removed the __hash__ on Consts.  This has the effect that equal Consts as keys
in a dict are not shared any more (they already weren't after translation).
The (hopefully) only place that cared is codewriter.py, which can be fixed
with an r_dict.


Modified: pypy/branch/pyjitpl5/pypy/jit/metainterp/codewriter.py
==============================================================================
--- pypy/branch/pyjitpl5/pypy/jit/metainterp/codewriter.py	(original)
+++ pypy/branch/pyjitpl5/pypy/jit/metainterp/codewriter.py	Fri Aug 21 15:46:51 2009
@@ -6,7 +6,7 @@
 from pypy.rlib import objectmodel
 from pypy.rlib.objectmodel import we_are_translated
 from pypy.rlib.jit import _we_are_jitted
-from pypy.jit.metainterp.history import Const, getkind
+from pypy.jit.metainterp.history import Const, getkind, dict_equal_consts
 from pypy.jit.metainterp import heaptracker, support, history
 from pypy.tool.udir import udir
 from pypy.translator.simplify import get_funcobj, get_functype
@@ -81,7 +81,7 @@
     portal_graph = None
 
     def __init__(self, metainterp_sd, policy, ts):
-        self.all_prebuilt_values = {}
+        self.all_prebuilt_values = dict_equal_consts()
         self.all_graphs = {}
         self.all_indirectcallsets = {}
         self.all_methdescrs = {}

Modified: pypy/branch/pyjitpl5/pypy/jit/metainterp/history.py
==============================================================================
--- pypy/branch/pyjitpl5/pypy/jit/metainterp/history.py	(original)
+++ pypy/branch/pyjitpl5/pypy/jit/metainterp/history.py	Fri Aug 21 15:46:51 2009
@@ -173,29 +173,27 @@
         return 'Const(%s)' % self._getrepr_()
 
     def __eq__(self, other):
+        "NOT_RPYTHON"
+        # Remember that you should not compare Consts with '==' in RPython.
+        # Consts have no special __hash__, in order to force different Consts
+        # from being considered as different keys when stored in dicts
+        # (as they always are after translation).  Use a dict_equal_consts()
+        # to get the other behavior (i.e. using this __eq__).
         if self.__class__ is not other.__class__:
             return False
         if isinstance(self.value, Symbolic):
-            v = id(self.value)
+            v1 = "symbolic", id(self.value)
         else:
-            v = self.value
+            v1 = self.value
         if isinstance(other.value, Symbolic):
-            v2 = id(other.value)
+            v2 = "symbolic", id(other.value)
         else:
             v2 = other.value
-        return v == v2
+        return v1 == v2
 
     def __ne__(self, other):
         return not (self == other)
 
-    def __hash__(self):
-        if isinstance(self.value, Symbolic):
-            return id(self.value)
-        try:
-            return self.get_()
-        except lltype.DelayedPointer:
-            return -2      # xxx risk of changing hash...
-
 
 class ConstInt(Const):
     type = INT
@@ -541,6 +539,27 @@
 
 # ____________________________________________________________
 
+def dict_equal_consts():
+    "NOT_RPYTHON"
+    # Returns a dict in which Consts that compare as equal
+    # are identified when used as keys.
+    return r_dict(dc_eq, dc_hash)
+
+def dc_eq(c1, c2):
+    return c1 == c2
+
+def dc_hash(c):
+    if not isinstance(c, Const):
+        return hash(c)
+    if isinstance(c.value, Symbolic):
+        return id(c.value)
+    try:
+        return c.get_()
+    except lltype.DelayedPointer:
+        return -2      # xxx risk of changing hash...
+
+# ____________________________________________________________
+
 # The TreeLoop class contains a loop or a generalized loop, i.e. a tree
 # of operations.  Each branch ends in a jump which can go either to
 # the top of the same loop, or to another TreeLoop; or it ends in a FAIL.



More information about the Pypy-commit mailing list