From fdc203bdae26aecb475a03c3da17cd44ee376a9c Mon Sep 17 00:00:00 2001 From: "W. Trevor King" Date: Wed, 20 Jan 2010 14:24:51 -0500 Subject: [PATCH] Optimized BugDir.uuids, caching on-disk bug uuids. Output of python -m cProfile -o profile be list python -c "import pstats; p=pstats.Stats('profile'); p.sort_stats('cumulative').print_stats()" on my slow netbook before optimization: ncalls tottime percall cumtime percall filename:lineno(function) 10290 0.417 0.000 10.832 0.001 libbe/bugdir.py:237(uuids) after optimization: 105 0.063 0.001 0.250 0.002 libbe/bugdir.py:237(uuids) The old generator produced many more calls than the new implementation, but the number of calls was not the source of the slowdown (data not shown ;). --- libbe/bugdir.py | 23 ++++++++++------------- 1 file changed, 10 insertions(+), 13 deletions(-) diff --git a/libbe/bugdir.py b/libbe/bugdir.py index 5967a7e..4711a8f 100644 --- a/libbe/bugdir.py +++ b/libbe/bugdir.py @@ -234,19 +234,16 @@ class BugDir (list, settings_object.SavedSettingsObject): # methods for managing bugs - def uuids(self): - uuids = [] - # list the uuids in memory - for bug in self: - uuids.append(bug.uuid) - yield bug.uuid - if self.storage != None and self.storage.is_readable(): - # and the ones that are still just in storage - child_uuids = libbe.util.id.child_uuids( - self.storage.children(self.id.storage())) - for id in child_uuids: - if id not in uuids: - yield id + 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)) def _clear_bugs(self): while len(self) > 0: -- 2.26.2