diff options
author | Erik Johnston <erik@matrix.org> | 2017-01-17 11:18:13 +0000 |
---|---|---|
committer | Erik Johnston <erik@matrix.org> | 2017-01-17 11:18:13 +0000 |
commit | f85b6ca494ae587731d99196020cc74d7eca012a (patch) | |
tree | 453615672d4125641e192bb92cf9a7abdd68d345 /synapse/util/caches/dictionary_cache.py | |
parent | Add ExpiringCache tests (diff) | |
download | synapse-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/dictionary_cache.py')
-rw-r--r-- | synapse/util/caches/dictionary_cache.py | 6 |
1 files changed, 4 insertions, 2 deletions
diff --git a/synapse/util/caches/dictionary_cache.py b/synapse/util/caches/dictionary_cache.py index b0ca1bb79d..cb6933c61c 100644 --- a/synapse/util/caches/dictionary_cache.py +++ b/synapse/util/caches/dictionary_cache.py @@ -23,7 +23,9 @@ import logging logger = logging.getLogger(__name__) -DictionaryEntry = namedtuple("DictionaryEntry", ("full", "value")) +class DictionaryEntry(namedtuple("DictionaryEntry", ("full", "value"))): + def __len__(self): + return len(self.value) class DictionaryCache(object): @@ -32,7 +34,7 @@ class DictionaryCache(object): """ def __init__(self, name, max_entries=1000): - self.cache = LruCache(max_size=max_entries) + self.cache = LruCache(max_size=max_entries, size_callback=len) self.name = name self.sequence = 0 |