[pypy-commit] pypy set-strategies: in intersection_multiple start with the smallest to avoid unnecessary comparisons

l.diekmann noreply at buildbot.pypy.org
Thu Nov 10 13:52:32 CET 2011


Author: Lukas Diekmann <lukas.diekmann at uni-duesseldorf.de>
Branch: set-strategies
Changeset: r49263:001538c05f0e
Date: 2011-11-03 17:19 +0100
http://bitbucket.org/pypy/pypy/changeset/001538c05f0e/

Log:	in intersection_multiple start with the smallest to avoid
	unnecessary comparisons

diff --git a/pypy/objspace/std/setobject.py b/pypy/objspace/std/setobject.py
--- a/pypy/objspace/std/setobject.py
+++ b/pypy/objspace/std/setobject.py
@@ -626,7 +626,29 @@
     def intersect_multiple(self, w_set, others_w):
         #XXX find smarter implementations
         result = w_set.copy_real()
+
+        # find smallest set in others_w to reduce comparisons
+        # XXX maybe we can do this smarter
+        if len(others_w) > 1:
+            startset, startlength = None, 0
+            for w_other in others_w:
+                try:
+                    length = self.space.len(w_other)
+                except OperationError, e:
+                    if not e.match(self.space, self.space.w_TypeError):
+                        raise
+                    continue
+
+                if startset is None or self.space.is_true(self.space.lt(length, startlength)):
+                    startset = w_other
+                    startlength = length
+
+            others_w[others_w.index(startset)] = others_w[0]
+            others_w[0] = startset
+
         for w_other in others_w:
+            if result.length() == 0:
+                break
             if isinstance(w_other, W_BaseSetObject):
                 # optimization only
                 result.intersect_update(w_other)


More information about the pypy-commit mailing list