diff options
author | Florian Bruhin <me@the-compiler.org> | 2022-03-26 10:42:43 +0100 |
---|---|---|
committer | Florian Bruhin <me@the-compiler.org> | 2022-03-26 10:42:43 +0100 |
commit | 0a835ecd926cc334f5f0347e6d0b809dcfd86d0f (patch) | |
tree | 4ae8992e1388c281d71bd878022a17da7e2b0872 | |
parent | 6341154aeb4f08272177dbc7a42ee7762a39b464 (diff) | |
download | qutebrowser-0a835ecd926cc334f5f0347e6d0b809dcfd86d0f.tar.gz qutebrowser-0a835ecd926cc334f5f0347e6d0b809dcfd86d0f.zip |
Use a weakref.WeakValueDictionary
-rw-r--r-- | qutebrowser/misc/debugcachestats.py | 15 |
1 files changed, 5 insertions, 10 deletions
diff --git a/qutebrowser/misc/debugcachestats.py b/qutebrowser/misc/debugcachestats.py index 4d15a6fa3..a682b53d1 100644 --- a/qutebrowser/misc/debugcachestats.py +++ b/qutebrowser/misc/debugcachestats.py @@ -25,13 +25,13 @@ dependencies as possible to avoid cyclic dependencies. import weakref import sys -from typing import Any, Callable, List, Optional, Tuple, TypeVar +from typing import Any, Callable, Optional, TypeVar, Mapping from qutebrowser.utils import log -# The second element of each tuple should be a lru_cache wrapped function -_CACHE_FUNCTIONS: List[Tuple[str, Any]] = [] +# The callable should be a lru_cache wrapped function +_CACHE_FUNCTIONS: Mapping[str, Any] = weakref.WeakValueDictionary() _T = TypeVar('_T', bound=Callable[..., Any]) @@ -49,17 +49,12 @@ def register(name: Optional[str] = None) -> Callable[[_T], _T]: return fn else: - fn_ref = weakref.ref(fn) - _CACHE_FUNCTIONS.append((fn_name, fn_ref)) + _CACHE_FUNCTIONS[fn_name] = fn return fn return wrapper def debug_cache_stats() -> None: """Print LRU cache stats.""" - for idx, (name, fn_ref) in reversed(list(enumerate(_CACHE_FUNCTIONS))): - fn = fn_ref() - if not fn: - _CACHE_FUNCTIONS.pop(idx) - continue + for name, fn in _CACHE_FUNCTIONS.items(): log.misc.info('{}: {}'.format(name, fn.cache_info())) |