[Checkins] SVN: zope.interface/trunk/src/zope/interface/ro.py replaced `_flatten` with a non-recursive implementation, effectively making it 3x faster

Andreas Zeidler az at zitc.de
Thu Nov 1 11:39:19 EDT 2007


Log message for revision 81359:
  replaced `_flatten` with a non-recursive implementation, effectively making it  3x faster

Changed:
  U   zope.interface/trunk/src/zope/interface/ro.py

-=-
Modified: zope.interface/trunk/src/zope/interface/ro.py
===================================================================
--- zope.interface/trunk/src/zope/interface/ro.py	2007-11-01 14:29:27 UTC (rev 81358)
+++ zope.interface/trunk/src/zope/interface/ro.py	2007-11-01 15:39:18 UTC (rev 81359)
@@ -21,7 +21,7 @@
 def ro(object):
     """Compute a "resolution order" for an object
     """
-    return mergeOrderings([_flatten(object, [])])
+    return mergeOrderings([_flatten(object)])
 
 def mergeOrderings(orderings, seen=None):
     """Merge multiple orderings so that within-ordering order is preserved
@@ -57,9 +57,15 @@
     result.reverse()
     return result
 
-def _flatten(ob, result):
-    result.append(ob)
-    for base in ob.__bases__:
-        _flatten(base, result)
-
+def _flatten(ob):
+    result = [ob]
+    i = 0
+    for ob in iter(result):
+        i += 1
+        # the recursive calls can be avoided by inserting the base classes
+        # into the dynamically growing list directly after the currently
+        # considered object;  the iterator makes sure this will keep working
+        # in the future, since it cannot rely on the length of the list
+        # by definition...
+        result[i:i] = ob.__bases__
     return result



More information about the Checkins mailing list