[pypy-svn] r17783 - in pypy/dist/pypy/objspace/flow: . test

arigo at codespeak.net arigo at codespeak.net
Fri Sep 23 12:04:05 CEST 2005


Author: arigo
Date: Fri Sep 23 12:04:04 2005
New Revision: 17783

Modified:
   pypy/dist/pypy/objspace/flow/model.py
   pypy/dist/pypy/objspace/flow/test/test_model.py
Log:
Further simplification of traverse(), the full flexibility of which
is never used anywhere and the speed of which is crucial during
translation.


Modified: pypy/dist/pypy/objspace/flow/model.py
==============================================================================
--- pypy/dist/pypy/objspace/flow/model.py	(original)
+++ pypy/dist/pypy/objspace/flow/model.py	Fri Sep 23 12:04:04 2005
@@ -344,52 +344,65 @@
 #_________________________________________________________
 # a visitor for easy traversal of the above model
 
-import inspect   # for getmro
+##import inspect   # for getmro
 
-class traverse:
+##class traverse:
 
-    def __init__(self, visitor, functiongraph):
-        """ send the visitor over all (reachable) nodes. 
-            the visitor needs to have either callable attributes 'visit_typename'
-            or otherwise is callable itself.  
-        """
-        self.visitor = visitor
-        self.visitor_cache = {}
-        self.seen = {}
-        self.visit(functiongraph)
-
-    def visit(self, node):
-        if id(node) in self.seen:
-            return
+##    def __init__(self, visitor, functiongraph):
+##        """ send the visitor over all (reachable) nodes. 
+##            the visitor needs to have either callable attributes 'visit_typename'
+##            or otherwise is callable itself.  
+##        """
+##        self.visitor = visitor
+##        self.visitor_cache = {}
+##        self.seen = {}
+##        self.visit(functiongraph)
+
+##    def visit(self, node):
+##        if id(node) in self.seen:
+##            return
+
+##        # do the visit
+##        cls = node.__class__
+##        try:
+##            consume = self.visitor_cache[cls]
+##        except KeyError:
+##            for subclass in inspect.getmro(cls):
+##                consume = getattr(self.visitor, "visit_" + subclass.__name__, None)
+##                if consume:
+##                    break
+##            else:
+##                consume = getattr(self.visitor, 'visit', self.visitor)
+
+##                assert callable(consume), "visitor not found for %r on %r" % (cls, self.visitor)
+
+##                self.visitor_cache[cls] = consume
+
+##        self.seen[id(node)] = consume(node)
+
+##        # recurse
+##        if isinstance(node, Block):
+##            for obj in node.exits:
+##                self.visit(obj)
+##        elif isinstance(node, Link):
+##            self.visit(node.target)
+##        elif isinstance(node, FunctionGraph):
+##            self.visit(node.startblock)
+##        else:
+##            raise ValueError, "could not dispatch %r" % cls
+
+def traverse(visit, functiongraph):
+    pending = [functiongraph.startblock]
+    seen = {id(functiongraph.startblock): True}
+    for block in pending:
+        visit(block)
+        for link in block.exits:
+            visit(link)
+            targetid = id(link.target)
+            if targetid not in seen:
+                pending.append(link.target)
+                seen[targetid] = True
 
-        # do the visit
-        cls = node.__class__
-        try:
-            consume = self.visitor_cache[cls]
-        except KeyError:
-            for subclass in inspect.getmro(cls):
-                consume = getattr(self.visitor, "visit_" + subclass.__name__, None)
-                if consume:
-                    break
-            else:
-                consume = getattr(self.visitor, 'visit', self.visitor)
-
-                assert callable(consume), "visitor not found for %r on %r" % (cls, self.visitor)
-
-                self.visitor_cache[cls] = consume
-
-        self.seen[id(node)] = consume(node)
-
-        # recurse
-        if isinstance(node, Block):
-            for obj in node.exits:
-                self.visit(obj)
-        elif isinstance(node, Link):
-            self.visit(node.target)
-        elif isinstance(node, FunctionGraph):
-            self.visit(node.startblock)
-        else:
-            raise ValueError, "could not dispatch %r" % cls
 
 def flatten(funcgraph):
     l = []

Modified: pypy/dist/pypy/objspace/flow/test/test_model.py
==============================================================================
--- pypy/dist/pypy/objspace/flow/test/test_model.py	(original)
+++ pypy/dist/pypy/objspace/flow/test/test_model.py	Fri Sep 23 12:04:04 2005
@@ -28,27 +28,27 @@
         graph = self.getflow(self.simplefunc)
         assert all_operations(graph) == {'add': 1}
 
-    def test_class(self):
-        graph = self.getflow(self.simplefunc)
+##    def test_class(self):
+##        graph = self.getflow(self.simplefunc)
 
-        class MyVisitor:
-            def __init__(self):
-                self.blocks = []
-                self.links = []
-
-            def visit_FunctionGraph(self, graph):
-                self.graph = graph
-            def visit_Block(self, block):
-                self.blocks.append(block)
-            def visit_Link(self, link):
-                self.links.append(link)
-
-        v = MyVisitor()
-        traverse(v, graph)
-        #assert len(v.blocks) == 2
-        #assert len(v.links) == 1
-        assert v.graph == graph
-        assert v.links[0] == graph.startblock.exits[0]
+##        class MyVisitor:
+##            def __init__(self):
+##                self.blocks = []
+##                self.links = []
+
+##            def visit_FunctionGraph(self, graph):
+##                self.graph = graph
+##            def visit_Block(self, block):
+##                self.blocks.append(block)
+##            def visit_Link(self, link):
+##                self.links.append(link)
+
+##        v = MyVisitor()
+##        traverse(v, graph)
+##        #assert len(v.blocks) == 2
+##        #assert len(v.links) == 1
+##        assert v.graph == graph
+##        assert v.links[0] == graph.startblock.exits[0]
 
 ##    def test_partial_class(self):
 ##        graph = self.getflow(self.simplefunc)



More information about the Pypy-commit mailing list