[pypy-svn] r55409 - pypy/dist/pypy/interpreter/astcompiler

fijal at codespeak.net fijal at codespeak.net
Thu May 29 22:58:51 CEST 2008


Author: fijal
Date: Thu May 29 22:58:50 2008
New Revision: 55409

Modified:
   pypy/dist/pypy/interpreter/astcompiler/misc.py
Log:
Port 55408 from parser-complexity branch.
Improves a bit performance of attaching filename


Modified: pypy/dist/pypy/interpreter/astcompiler/misc.py
==============================================================================
--- pypy/dist/pypy/interpreter/astcompiler/misc.py	(original)
+++ pypy/dist/pypy/interpreter/astcompiler/misc.py	Thu May 29 22:58:50 2008
@@ -45,11 +45,31 @@
 
     return "_%s%s" % (klass, name)
 
+class Queue(object):
+    def __init__(self, item):
+        self.head = [item]
+        self.tail = []
+
+    def pop(self):
+        if self.head:
+            return self.head.pop()
+        else:
+            for i in range(len(self.tail)-1, -1, -1):
+                self.head.append(self.tail[i])
+            self.tail = []
+            return self.head.pop()
+
+    def extend(self, items):
+        self.tail.extend(items)
+
+    def nonempty(self):
+        return self.tail or self.head
+
 def set_filename(filename, tree):
     """Set the filename attribute to filename on every node in tree"""
-    worklist = [tree]
-    while worklist:
-        node = worklist.pop(0)
+    worklist = Queue(tree)
+    while worklist.nonempty():
+        node = worklist.pop()
         assert isinstance(node, ast.Node)
         node.filename = filename
         worklist.extend(node.getChildNodes())



More information about the Pypy-commit mailing list