mercurial/smartset.py
changeset 31015 1076f7eba964
parent 30881 1be65deb3d54
child 31019 74f77f1c2215
--- a/mercurial/smartset.py	Thu Feb 16 11:34:50 2017 -0500
+++ b/mercurial/smartset.py	Fri Feb 17 20:59:29 2017 -0800
@@ -171,8 +171,12 @@
                 self._set = data
                 # set has no order we pick one for stability purpose
                 self._ascending = True
-            data = list(data)
-        self._list = data
+                # converting set to list has a cost, do it lazily
+                data = None
+            else:
+                data = list(data)
+        if data is not None:
+            self._list = data
         self._datarepr = datarepr
 
     @util.propertycache
@@ -185,6 +189,12 @@
         asclist.sort()
         return asclist
 
+    @util.propertycache
+    def _list(self):
+        # _list is only lazily constructed if we have _set
+        assert '_set' in self.__dict__
+        return list(self._set)
+
     def __iter__(self):
         if self._ascending is None:
             return iter(self._list)
@@ -204,7 +214,7 @@
         return self._set.__contains__
 
     def __nonzero__(self):
-        return bool(self._list)
+        return bool(len(self))
 
     def sort(self, reverse=False):
         self._ascending = not bool(reverse)
@@ -218,7 +228,10 @@
         self._istopo = False
 
     def __len__(self):
-        return len(self._list)
+        if '_list' in self.__dict__:
+            return len(self._list)
+        else:
+            return len(self._set)
 
     def isascending(self):
         """Returns True if the collection is ascending order, False if not.