Skip to content
This repository was archived by the owner on Apr 26, 2024. It is now read-only.

Commit f057ad8

Browse files
committed
Only use GLOBAL_LIST if time based eviction is enabled
1 parent d8b04e8 commit f057ad8

File tree

1 file changed

+18
-4
lines changed

1 file changed

+18
-4
lines changed

synapse/util/caches/lrucache.py

Lines changed: 18 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -190,6 +190,10 @@ def get_cache_entry(self) -> Optional[P]:
190190
return self.cache_entry()
191191

192192

193+
# Whether to insert new cache entries to the global list. We only add to it if
194+
# time based eviction is enabled.
195+
USE_GLOBAL_LIST = False
196+
193197
# A linked list of all cache entries, allowing efficient time based eviction.
194198
GLOBAL_ROOT = _ListNode[_CacheEntry]()
195199

@@ -247,6 +251,9 @@ def expire_lru_cache_entries_after(hs: "HomeServer"):
247251
"""Start a background job that expires all cache entries if they have not
248252
been accessed for the given number of seconds.
249253
"""
254+
global USE_GLOBAL_LIST
255+
USE_GLOBAL_LIST = True
256+
250257
clock = hs.get_clock()
251258
clock.looping_call(
252259
_expire_old_entries, 30 * 1000, clock, hs.config.caches.expiry_time_msec / 1000
@@ -275,7 +282,9 @@ def __init__(
275282
):
276283
self_ref = weakref.ref(self, lambda _: self.drop_from_lists())
277284
self.list_node = _ListNode.insert_after(self_ref, root)
278-
self.global_list_node = _ListNode.insert_after(self_ref, GLOBAL_ROOT)
285+
self.global_list_node = None
286+
if USE_GLOBAL_LIST:
287+
self.global_list_node = _ListNode.insert_after(self_ref, GLOBAL_ROOT)
279288

280289
# We store a weak reference to the cache object so that this _Node can
281290
# remove itself from the cache. If the cache is dropped we ensure we
@@ -304,12 +313,14 @@ def __init__(
304313
_get_size_of(key)
305314
+ _get_size_of(value)
306315
+ _get_size_of(self.list_node, recurse=False)
307-
+ _get_size_of(self.global_list_node, recurse=False)
308316
+ _get_size_of(self.callbacks, recurse=False)
309317
+ _get_size_of(self, recurse=False)
310318
)
311319
self.memory += _get_size_of(self.memory, recurse=False)
312320

321+
if self.global_list_node:
322+
self.memory += _get_size_of(self.global_list_node, recurse=False)
323+
313324
def add_callbacks(self, callbacks: Collection[Callable[[], None]]) -> None:
314325
"""Add to stored list of callbacks, removing duplicates."""
315326

@@ -347,7 +358,9 @@ def drop_from_cache(self) -> None:
347358
def drop_from_lists(self) -> None:
348359
"""Remove this node from the cache lists."""
349360
self.list_node.remove_from_list()
350-
self.global_list_node.remove_from_list()
361+
362+
if self.global_list_node:
363+
self.global_list_node.remove_from_list()
351364

352365

353366
class LruCache(Generic[KT, VT]):
@@ -478,7 +491,8 @@ def add_node(key, value, callbacks: Collection[Callable[[], None]] = ()):
478491

479492
def move_node_to_front(node: _Node):
480493
node.list_node.move_after(list_root)
481-
node.global_list_node.move_after(GLOBAL_ROOT)
494+
if node.global_list_node:
495+
node.global_list_node.move_after(GLOBAL_ROOT)
482496

483497
def delete_node(node: _Node) -> int:
484498
node.drop_from_lists()

0 commit comments

Comments
 (0)