[pypy-commit] pypy default: replace some dicts used as sets in the translator with actual sets

cfbolz noreply at buildbot.pypy.org
Mon Apr 1 20:32:01 CEST 2013


Author: Carl Friedrich Bolz <cfbolz at gmx.de>
Branch: 
Changeset: r62912:5454495ac023
Date: 2013-04-01 20:28 +0200
http://bitbucket.org/pypy/pypy/changeset/5454495ac023/

Log:	replace some dicts used as sets in the translator with actual sets

diff --git a/rpython/memory/gctransform/transform.py b/rpython/memory/gctransform/transform.py
--- a/rpython/memory/gctransform/transform.py
+++ b/rpython/memory/gctransform/transform.py
@@ -86,9 +86,9 @@
 
     def __init__(self, translator, inline=False):
         self.translator = translator
-        self.seen_graphs = {}
+        self.seen_graphs = set()
         self.prepared = False
-        self.minimal_transform = {}
+        self.minimal_transform = set()
         if translator:
             self.mixlevelannotator = MixLevelHelperAnnotator(translator.rtyper)
         else:
@@ -109,8 +109,8 @@
         return exceptiondata.lltype_of_exception_value
 
     def need_minimal_transform(self, graph):
-        self.seen_graphs[graph] = True
-        self.minimal_transform[graph] = True
+        self.seen_graphs.add(graph)
+        self.minimal_transform.add(graph)
 
     def prepare_inline_helpers(self, graphs):
         from rpython.translator.backendopt.inline import iter_callsites
@@ -210,11 +210,11 @@
         if graph in self.minimal_transform:
             if self.minimalgctransformer:
                 self.minimalgctransformer.transform_graph(graph)
-            del self.minimal_transform[graph]
+            self.minimal_transform.remove(graph)
             return
         if graph in self.seen_graphs:
             return
-        self.seen_graphs[graph] = True
+        self.seen_graphs.add(graph)
 
         self.links_to_split = {} # link -> vars to pop_alive across the link
 
diff --git a/rpython/translator/backendopt/support.py b/rpython/translator/backendopt/support.py
--- a/rpython/translator/backendopt/support.py
+++ b/rpython/translator/backendopt/support.py
@@ -59,18 +59,18 @@
     if block is None:
         block = graph.startblock
     if seen is None:
-        seen = {block: None}
+        seen = set([block])
     if seeing is None:
-        seeing = {}
-    seeing[block] = True
+        seeing = set()
+    seeing.add(block)
     for link in block.exits:
         if link.target in seen:
             if link.target in seeing:
                 backedges.append(link)
         else:
-            seen[link.target] = None
+            seen.add(link.target)
             backedges.extend(find_backedges(graph, link.target, seen, seeing))
-    del seeing[block]
+    seeing.remove(block)
     return backedges
 
 def compute_reachability(graph):
diff --git a/rpython/translator/c/database.py b/rpython/translator/c/database.py
--- a/rpython/translator/c/database.py
+++ b/rpython/translator/c/database.py
@@ -115,7 +115,7 @@
         elif isinstance(T, (Struct, Array, _WeakRefType)):
             node = self.gettypedefnode(T, varlength=varlength)
             if who_asks is not None:
-                who_asks.dependencies[node] = True
+                who_asks.dependencies.add(node)
             return node.gettype()
         elif isinstance(T, FuncType):
             resulttype = self.gettype(T.RESULT)
@@ -136,7 +136,7 @@
             elif T.hints.get("render_structure", False):
                 node = self.gettypedefnode(T, varlength=varlength)
                 if who_asks is not None:
-                    who_asks.dependencies[node] = True
+                    who_asks.dependencies.add(node)
                 return 'struct %s @' % node.name
             elif T.hints.get('external', None) == 'C':
                 return '%s @' % T.hints['c_name']
@@ -364,15 +364,15 @@
     def getstructdeflist(self):
         # return the StructDefNodes sorted according to dependencies
         result = []
-        seen = {}
+        seen = set()
         def produce(node):
             if node not in seen:
-                deps = node.dependencies.keys()
+                deps = list(node.dependencies)
                 deps.sort(key=lambda x: x.name)
                 for othernode in deps:
                     produce(othernode)
                 result.append(node)
-                seen[node] = True
+                seen.add(node)
         nodes = self.structdefnodes.values()
         nodes.sort(key=lambda x: x.name)
         for node in nodes:
diff --git a/rpython/translator/c/funcgen.py b/rpython/translator/c/funcgen.py
--- a/rpython/translator/c/funcgen.py
+++ b/rpython/translator/c/funcgen.py
@@ -184,15 +184,15 @@
 
     def cfunction_declarations(self):
         # declare the local variables, excluding the function arguments
-        seen = {}
+        seen = set()
         for a in self.graph.getargs():
-            seen[a.name] = True
+            seen.add(a.name)
 
         result_by_name = []
         for v in self.allvariables():
             name = v.name
             if name not in seen:
-                seen[name] = True
+                seen.add(name)
                 result = cdecl(self.lltypename(v), LOCALVAR % name) + ';'
                 if self.lltypemap(v) is Void:
                     continue  #result = '/*%s*/' % result
diff --git a/rpython/translator/c/gcc/trackgcroot.py b/rpython/translator/c/gcc/trackgcroot.py
--- a/rpython/translator/c/gcc/trackgcroot.py
+++ b/rpython/translator/c/gcc/trackgcroot.py
@@ -342,12 +342,12 @@
 
     def walk_instructions_backwards(self, walker, initial_insn, initial_state):
         pending = []
-        seen = {}
+        seen = set()
         def schedule(insn, state):
             for previnsn in insn.previous_insns:
                 key = previnsn, state
                 if key not in seen:
-                    seen[key] = True
+                    seen.add(key)
                     pending.append(key)
         schedule(initial_insn, initial_state)
         while pending:
diff --git a/rpython/translator/c/node.py b/rpython/translator/c/node.py
--- a/rpython/translator/c/node.py
+++ b/rpython/translator/c/node.py
@@ -32,13 +32,23 @@
         else:
             return self.fget(obj)
 
+class Node(object):
+    __slots__ = ("db", )
+    def __init__(self, db):
+        self.db = db
 
-class StructDefNode:
+class NodeWithDependencies(Node):
+    __slots__ = ("dependencies", )
+    def __init__(self, db):
+        Node.__init__(self, db)
+        self.dependencies = set()
+
+class StructDefNode(NodeWithDependencies):
     typetag = 'struct'
     extra_union_for_varlength = True
 
     def __init__(self, db, STRUCT, varlength=1):
-        self.db = db
+        NodeWithDependencies.__init__(self, db)
         self.STRUCT = STRUCT
         self.LLTYPE = STRUCT
         self.varlength = varlength
@@ -66,7 +76,6 @@
                                                   with_number=with_number,
                                                   bare=True)
             self.prefix = somelettersfrom(STRUCT._name) + '_'
-        self.dependencies = {}
         #
         self.fieldnames = STRUCT._names
         if STRUCT._hints.get('typeptr', False):
@@ -188,12 +197,12 @@
                                                    self.name, cname)
 
 
-class ArrayDefNode:
+class ArrayDefNode(NodeWithDependencies):
     typetag = 'struct'
     extra_union_for_varlength = True
 
     def __init__(self, db, ARRAY, varlength=1):
-        self.db = db
+        NodeWithDependencies.__init__(self, db)
         self.ARRAY = ARRAY
         self.LLTYPE = ARRAY
         self.gcfields = []
@@ -208,7 +217,6 @@
         (self.barename,
          self.name) = db.namespace.uniquename(basename, with_number=with_number,
                                               bare=True)
-        self.dependencies = {}
         self.fulltypename =  '%s %s @' % (self.typetag, self.name)
         self.fullptrtypename = '%s %s *@' % (self.typetag, self.name)
 
@@ -315,7 +323,7 @@
             yield '-1'
 
 
-class BareBoneArrayDefNode:
+class BareBoneArrayDefNode(NodeWithDependencies):
     """For 'simple' array types which don't need a length nor GC headers.
     Implemented directly as a C array instead of a struct with an items field.
     rffi kind of expects such arrays to be 'bare' C arrays.
@@ -326,11 +334,10 @@
     extra_union_for_varlength = False
 
     def __init__(self, db, ARRAY, varlength=1):
-        self.db = db
+        NodeWithDependencies.__init__(self, db)
         self.ARRAY = ARRAY
         self.LLTYPE = ARRAY
         self.varlength = varlength
-        self.dependencies = {}
         contained_type = ARRAY.OF
         # There is no such thing as an array of voids:
         # we use a an array of chars instead; only the pointer can be void*.
@@ -379,17 +386,16 @@
         yield 'sizeof(%s)' % (cdecl(self.itemtypename, ''),)
 
 
-class FixedSizeArrayDefNode:
+class FixedSizeArrayDefNode(NodeWithDependencies):
     gcinfo = None
     name = None
     typetag = 'struct'
     extra_union_for_varlength = False
 
     def __init__(self, db, FIXEDARRAY):
-        self.db = db
+        NodeWithDependencies.__init__(self, db)
         self.FIXEDARRAY = FIXEDARRAY
         self.LLTYPE = FIXEDARRAY
-        self.dependencies = {}
         self.itemtypename = db.gettype(FIXEDARRAY.OF, who_asks=self)
         self.fulltypename = self.itemtypename.replace('@', '(@)[%d]' %
                                                       FIXEDARRAY.length)
@@ -456,14 +462,13 @@
         return []
 
 
-class ExtTypeOpaqueDefNode:
+class ExtTypeOpaqueDefNode(NodeWithDependencies):
     """For OpaqueTypes created with the hint render_structure."""
     typetag = 'struct'
 
     def __init__(self, db, T):
-        self.db = db
+        NodeWithDependencies.__init__(self, db)
         self.T = T
-        self.dependencies = {}
         self.name = 'RPyOpaque_%s' % (T.tag,)
 
     def setup(self):
@@ -475,7 +480,7 @@
 # ____________________________________________________________
 
 
-class ContainerNode(object):
+class ContainerNode(Node):
     if USESLOTS:      # keep the number of slots down!
         __slots__ = """db obj 
                        typename implementationtypename
@@ -485,9 +490,8 @@
     eci_name = '_compilation_info'
 
     def __init__(self, db, T, obj):
-        self.db = db
+        Node.__init__(self, db)
         self.obj = obj
-        #self.dependencies = {}
         self.typename = db.gettype(T)  #, who_asks=self)
         self.implementationtypename = db.gettype(T, varlength=self.getlength())
         parent, parentindex = parentlink(obj)
@@ -813,8 +817,8 @@
     # be necessary
 
     def __init__(self, db, T, obj, forcename=None):
+        Node.__init__(self, db)
         self.globalcontainer = True
-        self.db = db
         self.T = T
         self.obj = obj
         callable = getattr(obj, '_callable', None)
@@ -827,7 +831,6 @@
             self.name = (forcename or
                          db.namespace.uniquename('g_' + self.basename()))
         self.make_funcgens()
-        #self.dependencies = {}
         self.typename = db.gettype(T)  #, who_asks=self)
 
     def getptrname(self):
diff --git a/rpython/translator/exceptiontransform.py b/rpython/translator/exceptiontransform.py
--- a/rpython/translator/exceptiontransform.py
+++ b/rpython/translator/exceptiontransform.py
@@ -284,13 +284,13 @@
         return need_exc_matching, n_gen_exc_checks
 
     def comes_from_last_exception(self, entrymap, link):
-        seen = {}
+        seen = set()
         pending = [(link, link.args[1])]
         while pending:
             link, v = pending.pop()
             if (link, v) in seen:
                 continue
-            seen[link, v] = True
+            seen.add((link, v))
             if link.last_exc_value is not None and v is link.last_exc_value:
                 return True
             block = link.prevblock
diff --git a/rpython/translator/gensupp.py b/rpython/translator/gensupp.py
--- a/rpython/translator/gensupp.py
+++ b/rpython/translator/gensupp.py
@@ -3,14 +3,14 @@
 Another name could be genEric, but well...
 """
 
-def uniquemodulename(name, SEEN={}):
+def uniquemodulename(name, SEEN=set()):
     # never reuse the same module name within a Python session!
     i = 0
     while True:
         i += 1
         result = '%s_%d' % (name, i)
         if result not in SEEN:
-            SEEN[result] = True
+            SEEN.add(result)
             return result
 
 # a translation table suitable for str.translate() to remove
diff --git a/rpython/translator/transform.py b/rpython/translator/transform.py
--- a/rpython/translator/transform.py
+++ b/rpython/translator/transform.py
@@ -12,12 +12,12 @@
 from rpython.rtyper.lltypesystem import lltype
 
 def checkgraphs(self, blocks):
-    seen = {}
+    seen = set()
     for block in blocks:
         graph = self.annotated[block]
         if graph not in seen:
             checkgraph(graph)
-            seen[graph] = True
+            seen.add(graph)
 
 def fully_annotated_blocks(self):
     """Ignore blocked blocks."""


More information about the pypy-commit mailing list