Make BugDir._uuids_cache a set.
authorW. Trevor King <wking@drexel.edu>
Thu, 12 May 2011 13:23:14 +0000 (09:23 -0400)
committerW. Trevor King <wking@drexel.edu>
Thu, 12 May 2011 13:23:19 +0000 (09:23 -0400)
For `be list` on a bugdir with 4096 open bugs, this reduced the
cumulative time spend in 8194 calls to BugDir.uuids() from 41 seconds
to 33 seconds.

Of the 33 cumulative seconds, 24 were spend in uuids() itself (and not
in child functions), which is probably from the list comprehension
extracting in-memory Bug uuids.  With fancier accounting, you could
probably trust _uuids_cache to already contain all the in-memory
uuids and dispense with the union altogether.

libbe/bugdir.py

index 97412391ee2891aa73de2a013c441b5d6557170f..c73e097209f6934c78a347125555f7b84043bb7f 100644 (file)
@@ -220,14 +220,18 @@ class BugDir (list, settings_object.SavedSettingsObject):
 
     def uuids(self, use_cached_disk_uuids=True):
         if use_cached_disk_uuids==False or not hasattr(self, '_uuids_cache'):
-            self._uuids_cache = []
-            # list bugs that are in storage
-            if self.storage != None and self.storage.is_readable():
-                child_uuids = libbe.util.id.child_uuids(
-                    self.storage.children(self.id.storage()))
-                for id in child_uuids:
-                    self._uuids_cache.append(id)
-        return list(set([bug.uuid for bug in self] + self._uuids_cache))
+            self._refresh_uuid_cache()
+        self._uuids_cache = self._uuids_cache.union([bug.uuid for bug in self])
+        return self._uuids_cache
+
+    def _refresh_uuid_cache(self):
+        self._uuids_cache = set()
+        # list bugs that are in storage
+        if self.storage != None and self.storage.is_readable():
+            child_uuids = libbe.util.id.child_uuids(
+                self.storage.children(self.id.storage()))
+            for id in child_uuids:
+                self._uuids_cache.add(id)
 
     def _clear_bugs(self):
         while len(self) > 0:
@@ -248,7 +252,7 @@ class BugDir (list, settings_object.SavedSettingsObject):
         self.append(bg)
         self._bug_map_gen()
         if hasattr(self, '_uuids_cache') and not bg.uuid in self._uuids_cache:
-            self._uuids_cache.append(bg.uuid)
+            self._uuids_cache.add(bg.uuid)
         return bg
 
     def remove_bug(self, bug):