summary refs log tree commit diff
path: root/synapse/util/caches/treecache.py
diff options
context:
space:
mode:
authorErik Johnston <erik@matrix.org>2017-01-17 11:18:13 +0000
committerErik Johnston <erik@matrix.org>2017-01-17 11:18:13 +0000
commitf85b6ca494ae587731d99196020cc74d7eca012a (patch)
tree453615672d4125641e192bb92cf9a7abdd68d345 /synapse/util/caches/treecache.py
parentAdd ExpiringCache tests (diff)
downloadsynapse-f85b6ca494ae587731d99196020cc74d7eca012a.tar.xz
Speed up cache size calculation
Instead of calculating the size of the cache repeatedly, which can take
a long time now that it can use a callback, instead cache the size and
update that on insertion and deletion.

This requires changing the cache descriptors to have two caches, one for
pending deferreds and the other for the actual values. There's no reason
to evict from the pending deferreds as they won't take up any more
memory.
Diffstat (limited to 'synapse/util/caches/treecache.py')
-rw-r--r--synapse/util/caches/treecache.py14
1 files changed, 13 insertions, 1 deletions
diff --git a/synapse/util/caches/treecache.py b/synapse/util/caches/treecache.py
index c31585aea3..460e98a92d 100644
--- a/synapse/util/caches/treecache.py
+++ b/synapse/util/caches/treecache.py
@@ -65,12 +65,24 @@ class TreeCache(object):
         return popped
 
     def values(self):
-        return [e.value for e in self.root.values()]
+        return list(popped_to_iterator(self.root))
 
     def __len__(self):
         return self.size
 
 
+def popped_to_iterator(d):
+    if isinstance(d, dict):
+        for value_d in d.itervalues():
+            for value in popped_to_iterator(value_d):
+                yield value
+    else:
+        if isinstance(d, _Entry):
+            yield d.value
+        else:
+            yield d
+
+
 class _Entry(object):
     __slots__ = ["value"]