holoviz/panel · #9063
perf: Reduce pn.cache per-call overhead
panel/io/cache.py350 + / 131 −
@@ -25,6 +25,7 @@ import unittest.mock import uuid +from collections.abc import Callable from contextlib import contextmanager import param@@ -39,7 +40,7 @@ #--------------------------------------------------------------------- if t.TYPE_CHECKING:- from collections.abc import Awaitable, Callable, Hashable+ from collections.abc import Awaitable, Hashable _P = t.ParamSpec("_P") _R = t.TypeVar("_R") _CallableT = t.TypeVar("_CallableT", bound=Callable)@@ -68,6 +69,37 @@ def clear(self, func_hashes: list[str | None]=[None]) -> None: bytes, str, float, int, bool, bytearray, type(None) ) +_NATIVE_TYPE_SET = frozenset(_NATIVE_TYPES)++# Native types whose equal values are interchangeable as memo keys,+# which excludes float since 0.0 == -0.0 while they hash differently+_KEY_TYPES = frozenset({int, str, bool, bytes, type(None)})++# Hash functions for the native scalar types, resolved from _hash_funcs+# whenever it changes so that a scalar is hashed with one dict lookup+_SCALAR_HASHERS: dict[type, Callable[[t.Any], bytes]] = {}++_DEFAULT_SCALAR_HASHERS: dict[type, Callable[[t.Any], bytes]] = {}++# Whether flat containers of native scalars may be hashed from their+# repr in one go, which is only sound while every native type is+# hashed by its default hash function+_REPR_FAST_PATH = False++# The entries of _hash_funcs not keyed by name, in order, each marked+# as a predicate or a type to match with isinstance+_SCAN_ENTRIES: list[tuple[bool, t.Any, t.Any]] = []++# Memo of _find_hash_func results for types whose lookup does not+# depend on the instance, invalidated whenever _hash_funcs changes+_HASH_FUNC_CACHE: dict[type, Callable[[t.Any], bytes] | None] = {}++_HASH_FUNC_CACHE_MAX_ITEMS = 1024++# Bounds the number of cache buckets a decorated function remembers,+# which grows with the sessions seen when caching per session+_MAX_BUCKETS = 256+ _ARRAY_SIZE_LARGE = 100_000 _ARRAY_SAMPLE_SIZE = 100_000@@ -122,10 +154,31 @@ def _is_native(obj: t.Any) -> bool: return isinstance(obj, _NATIVE_TYPES) def _container_hash(obj: t.Any) -> bytes:- h = hashlib.new("md5")- h.update(_generate_hash(f'__{type(obj).__name__}'))+ obj_type = type(obj)+ if _REPR_FAST_PATH:+ if obj_type is dict:+ flat = (+ _NATIVE_TYPE_SET.issuperset(map(type, obj)) and+ _NATIVE_TYPE_SET.issuperset(map(type, obj.values()))+ )+ else:+ flat = (+ (obj_type is list or obj_type is tuple) and+ _NATIVE_TYPE_SET.issuperset(map(type, obj))+ )+ if flat:+ # The repr of a flat container of natives is an unambiguous+ # literal, so it can be hashed without visiting each item+ return hashlib.md5(b'repr:' + repr(obj).encode()).digest()+ h = hashlib.md5()+ h.update(_generate_hash(f'__{obj_type.__name__}'))+ hash_stack = state._current_stack for item in (obj.items() if isinstance(obj, dict) else obj):- h.update(_generate_hash(item))+ hash_func = _SCALAR_HASHERS.get(type(item))+ if hash_func is not None:+ h.update(_call_hash_func(hash_func, item))+ else:+ h.update(_generate_hash_tracked(item, hash_stack)) return h.digest() def _slice_hash(x: slice) -> bytes:@@ -250,7 +303,63 @@ def _io_hash(obj): h.update(_generate_hash(obj.getvalue())) return h.digest() -_hash_funcs: dict[str | type[t.Any] | tuple[type, ...] | Callable[[t.Any], bool], bytes | Callable[[t.Any], bytes]] = {+_HashFuncKey: t.TypeAlias = str | type[t.Any] | tuple[type, ...] | Callable[[t.Any], bool]+_HashFuncValue: t.TypeAlias = bytes | Callable[[t.Any], bytes]++class _HashFuncs(dict[_HashFuncKey, _HashFuncValue]):+ """+ The registry of hash functions, which rebuilds the lookup tables+ derived from it whenever it is modified, since downstream libraries+ register their own hash functions by mutating it directly.+ """++ def __init__(self, mapping: dict[_HashFuncKey, _HashFuncValue]) -> None:+ super().__init__(mapping)++ def __setitem__(self, key, value):+ super().__setitem__(key, value)+ _rebuild_hash_tables()++ def __delitem__(self, key):+ super().__delitem__(key)+ _rebuild_hash_tables()++ # The same [misc] error is ignored on dict.__ior__ in typeshed, since an+ # in-place update cannot widen the key and value types the way | can.+ def __ior__(self, other): # type: ignore[misc]+ super().update(other)+ _rebuild_hash_tables()+ return self++ def update(self, *args, **kwargs):+ super().update(*args, **kwargs)+ _rebuild_hash_tables()++ def clear(self):+ super().clear()+ _rebuild_hash_tables()++ def pop(self, *args):+ value = super().pop(*args)+ _rebuild_hash_tables()+ return value++ def popitem(self):+ item = super().popitem()+ _rebuild_hash_tables()+ return item++ def setdefault(self, key, default=None):+ value = super().setdefault(key, default)+ _rebuild_hash_tables()+ return value++ def replace(self, mapping):+ super().clear()+ super().update(mapping)+ _rebuild_hash_tables()++_hash_funcs: _HashFuncs = _HashFuncs({ # Types # NOTE: Enum comes first so that IntEnum and StrEnum members are not # matched by plain int or str.@@ -302,44 +411,90 @@ def _io_hash(obj): inspect.ismodule : lambda obj: obj.__name__, lambda x: dataclasses.is_dataclass(x) and not isinstance(x, type): _dataclass_hash, lambda x: hasattr(x, "tobytes") and x.shape == (): lambda x: x.tobytes(), # Single numpy dtype like: np.int32-}--for name in _FFI_TYPE_NAMES:- _hash_funcs[name] = b'0'+ **{name: b'0' for name in _FFI_TYPE_NAMES},+}) -def _find_hash_func(obj):- fqn_type = _get_fqn(obj)- if fqn_type in _hash_funcs:- return _hash_funcs[fqn_type]+def _lookup_hash_func(obj):+ """+ Resolves the hash function registered for an object, and whether+ the match depends on its type alone, so that it may be memoized+ per type.+ """ obj_type = type(obj)- if obj_type in _hash_funcs:- # Exact type matches avoid the scan below and ensure a subclass- # does not pick up the hash function of its base, e.g. bool- # being hashed as an int- return _hash_funcs[obj_type]- for otype, hash_func in _hash_funcs.items():- if isinstance(otype, str):- if otype == fqn_type:- return hash_func- elif inspect.isfunction(otype):+ # Exact type matches come first so that a subclass does not pick up+ # the hash function of its base, e.g. bool being hashed as an int,+ # and so that a custom hash function for a type the defaults+ # register by name, e.g. pandas.DataFrame, takes precedence+ hash_func = _hash_funcs.get(obj_type)+ if hash_func is not None:+ return hash_func, True+ hash_func = _hash_funcs.get(_get_fqn(obj))+ if hash_func is not None:+ return hash_func, True+ by_type = not isinstance(obj, type)+ for is_predicate, otype, hash_func in _SCAN_ENTRIES:+ if is_predicate:+ # Predicates may depend on the instance, not just its type+ by_type = False if otype(obj):- return hash_func+ return hash_func, False elif isinstance(obj, otype):- return hash_func+ return hash_func, by_type+ return None, by_type++def _find_hash_func(obj):+ obj_type = type(obj)+ try:+ return _HASH_FUNC_CACHE[obj_type]+ except KeyError:+ pass+ hash_func, by_type = _lookup_hash_func(obj)+ if by_type:+ if len(_HASH_FUNC_CACHE) >= _HASH_FUNC_CACHE_MAX_ITEMS:+ _HASH_FUNC_CACHE.clear()+ _HASH_FUNC_CACHE[obj_type] = hash_func+ return hash_func++_NATIVE_SAMPLES = (b'', '', 0.0, 0, False, bytearray(), None)++def _rebuild_hash_tables():+ global _REPR_FAST_PATH+ _HASH_FUNC_CACHE.clear()+ _SCAN_ENTRIES[:] = [+ (not isinstance(key, (type, tuple)), key, hash_func)+ for key, hash_func in _hash_funcs.items() if not isinstance(key, str)+ ]+ hashers = {}+ for sample in _NATIVE_SAMPLES:+ hash_func, _ = _lookup_hash_func(sample)+ if hash_func is not None:+ hashers[type(sample)] = hash_func+ _SCALAR_HASHERS.clear()+ _SCALAR_HASHERS.update(hashers)+ _REPR_FAST_PATH = all(+ hashers.get(native) is hash_func+ for native, hash_func in _DEFAULT_SCALAR_HASHERS.items()+ )++_rebuild_hash_tables()++_DEFAULT_SCALAR_HASHERS.update(_SCALAR_HASHERS)++def _call_hash_func(hash_func, obj):+ try:+ return hash_func(obj)+ except BaseException as e:+ raise ValueError(+ f'User hash function {hash_func!r} failed for input '+ f'{obj!r} with following error: {type(e).__name__}("{e}").'+ ) from e def _generate_hash_inner(obj): hash_func = _find_hash_func(obj) if hash_func is not None:- try:- output = hash_func(obj)- except BaseException as e:- raise ValueError(- f'User hash function {hash_func!r} failed for input '- f'{obj!r} with following error: {type(e).__name__}("{e}").'- ) from e- return output+ return _call_hash_func(hash_func, obj) if hasattr(obj, '__reduce__') and inspect.isclass(obj):- h = hashlib.new("md5")+ h = hashlib.md5() try: reduce_data = obj.__reduce__() except BaseException:@@ -349,20 +504,25 @@ def _generate_hash_inner(obj): return h.digest() return _int_to_bytes(id(obj)) -def _generate_hash(obj):+def _generate_hash_tracked(obj, hash_stack): # Break recursive cycles.- hash_stack = state._current_stack if obj in hash_stack: # Encodes which ancestor is referenced, otherwise distinct cyclic # structures, e.g. a reference to the root vs. to a nested # container, would hash alike. return _CYCLE_PLACEHOLDER + _int_to_bytes(hash_stack.distance(obj)) hash_stack.push(obj) try:- hash_value = _generate_hash_inner(obj)+ return _generate_hash_inner(obj) finally: hash_stack.pop()- return hash_value++def _generate_hash(obj):+ # Scalars cannot take part in a cycle, so they skip the cycle stack+ hash_func = _SCALAR_HASHERS.get(type(obj))+ if hash_func is not None:+ return _call_hash_func(hash_func, obj)+ return _generate_hash_tracked(obj, state._current_stack) def _key(obj, _seen=None): """@@ -376,9 +536,22 @@ def _key(obj, _seen=None): not be derived from the identity of a mutable object, since the address may be recycled and the contents may change in place. """- if obj is None:- return None- elif isinstance(obj, bytearray):+ obj_type = type(obj)+ if obj_type in _KEY_TYPES:+ return (obj_type.__name__, obj)+ elif obj_type is tuple or obj_type is list:+ if _KEY_TYPES.issuperset(map(type, obj)):+ # Equal natives of one type are interchangeable, so the types+ # and the values need not be paired up+ return (f'__{obj_type.__name__}', tuple(map(type, obj)), tuple(obj))+ elif obj_type is dict:+ if (_KEY_TYPES.issuperset(map(type, obj)) and+ _KEY_TYPES.issuperset(map(type, obj.values()))):+ return (+ '__dict', tuple(map(type, obj)), tuple(obj),+ tuple(map(type, obj.values())), tuple(obj.values())+ )+ if isinstance(obj, bytearray): return ('bytearray', bytes(obj)) elif isinstance(obj, float): return (type(obj).__name__, _float_to_bytes(obj))@@ -415,11 +588,9 @@ def _memoize_hash(key, hash_value): with _HASH_MAP_LOCK: if key in _HASH_MAP: return- excess = (len(_HASH_MAP) + 1) - _HASH_MAP_MAX_ITEMS- if excess > 0:+ while len(_HASH_MAP) >= _HASH_MAP_MAX_ITEMS: # dicts preserve insertion order, so this evicts FIFO- for stale in list(_HASH_MAP)[:excess]:- del _HASH_MAP[stale]+ del _HASH_MAP[next(iter(_HASH_MAP))] _HASH_MAP[key] = hash_value def _closure_key(func):@@ -456,38 +627,48 @@ def _closure_key(func): def _cleanup_cache(cache, policy, max_items, time): """- Deletes items in the cache if the exceed the number of items or- their TTL (time-to-live) has expired.+ Deletes items from the cache according to the (lowercase) policy+ until there is room for one more. """ while len(cache) >= max_items:- if policy.lower() == 'fifo':- key = list(cache.keys())[0]- elif policy.lower() == 'lru':- key = sorted(((k, time-t) for k, (_, _, _, t) in cache.items()),- key=lambda o: o[1])[-1][0]- elif policy.lower() == 'lfu':- key = sorted(cache.items(), key=lambda o: o[1][2])[0][0]+ if policy == 'fifo' or (policy == 'lru' and type(cache) is dict):+ # Hits move the entry of a dict to its end, so the first key+ # is the oldest and the least recently used one+ key = next(iter(cache))+ elif policy == 'lru':+ key = min(reversed(list(cache.items())), key=lambda o: o[1][3])[0]+ else:+ key = min(cache.items(), key=lambda o: o[1][2])[0] del cache[key] def _cleanup_ttl(cache, ttl, time): """ Deletes items in the cache if their TTL (time-to-live) has expired. """- for key, (_, ts, _, _) in list(cache.items()):- if (time-ts) > ttl:- del cache[key]+ expired = [key for key, (_, ts, _, _) in cache.items() if (time-ts) > ttl]+ for key in expired:+ cache.pop(key, None) @contextmanager def _hash_context(hash_funcs, approximate=True):- backup = dict(_hash_funcs)- _hash_funcs.update(hash_funcs) token = _APPROXIMATE.set(approximate)+ if hash_funcs:+ backup = dict(_hash_funcs)+ _hash_funcs.update(hash_funcs) try: yield finally: _APPROXIMATE.reset(token)- _hash_funcs.clear()- _hash_funcs.update(backup)+ if hash_funcs:+ _hash_funcs.replace(backup)++def _hash_arguments(args, kwargs):+ hasher = hashlib.md5()+ if args:+ hasher.update(_generate_hash(args))+ if kwargs:+ hasher.update(_generate_hash(kwargs))+ return hasher.hexdigest() #--------------------------------------------------------------------- # Public API@@ -517,22 +698,24 @@ def compute_hash(func, hash_funcs, args, kwargs, approximate=True): # hash for the very same arguments. key = _INDETERMINATE if not hash_funcs:- args_key, kwargs_key = _key(args), _key(kwargs)- if args_key is not _INDETERMINATE and kwargs_key is not _INDETERMINATE:- key = (func, args_key, kwargs_key, approximate)- try:- if key in _HASH_MAP:- return _HASH_MAP[key]- except TypeError:- # The callable itself is not hashable- key = _INDETERMINATE- hasher = hashlib.new("md5")- with _hash_context(hash_funcs, approximate):- if args:- hasher.update(_generate_hash(args))- if kwargs:- hasher.update(_generate_hash(kwargs))- hash_value = hasher.hexdigest()+ args_key = _key(args)+ if args_key is not _INDETERMINATE:+ kwargs_key = _key(kwargs)+ if kwargs_key is not _INDETERMINATE:+ key = (func, args_key, kwargs_key, approximate)+ try:+ hash_value = _HASH_MAP.get(key)+ except TypeError:+ # The callable itself is not hashable+ key = _INDETERMINATE+ else:+ if hash_value is not None:+ return hash_value+ if hash_funcs or _APPROXIMATE.get() != approximate:+ with _hash_context(hash_funcs, approximate):+ hash_value = _hash_arguments(args, kwargs)+ else:+ hash_value = _hash_arguments(args, kwargs) if key is not _INDETERMINATE: _memoize_hash(key, hash_value) return hash_value@@ -636,6 +819,7 @@ def cache( raise ValueError( f"Cache policy must be one of 'FIFO', 'LRU' or 'LFU', not {policy}." )+ policy = policy.lower() # type: ignore[assignment] if cache_path is None: cache_path = config.cache_path@@ -662,91 +846,126 @@ def decorator(func: Callable[_P, _R]) -> _CachedFunc[Callable[_P, _R]]: with _hash_context(hash_funcs, approximate): closure_key = _closure_key(func) - fallback_fname = uuid.uuid4().hex+ # If the function is defined inside a bokeh/panel application it is+ # recreated for each session, therefore we cache by file, class and+ # qualified function name. Since that is shared by all functions a+ # factory generates we also key on the values the function closes+ # over, and on the settings so that two decorations of one function+ # with e.g. a different ttl or to_disk do not share a cache.+ func_name = func.__name__+ qualname = getattr(func, '__qualname__', func_name)+ module = sys.modules.get(func.__module__)+ fname = '__main__' if func.__module__ == '__main__' else getattr(module, '__file__', None)+ if fname is None:+ fname = uuid.uuid4().hex+ settings = (+ max_items, policy, ttl, to_disk, str(cache_path) if to_disk else None,+ per_session, approximate+ )+ # The usage counts and access times only serve eviction+ track_usage = max_items is not None+ lru_dict = track_usage and policy == 'lru' and not to_disk+ # The bucket and the time of its last ttl sweep per method owner and+ # session, since the bucket hash is otherwise fixed for the function+ buckets: dict[tuple, tuple[str, list[float]]] = {} lock = threading.RLock() - def hash_func(*args, **kwargs):+ def lookup(args, kwargs): # Handle param.depends method by adding parameters to arguments- func_name = func.__name__- is_method = (- args and isinstance(args[0], object) and- getattr(type(args[0]), func_name, None) is wrapped_func- )+ is_method = args and getattr(type(args[0]), func_name, None) is wrapped_func hash_args, hash_kwargs = args, kwargs- if (is_method and isinstance(args[0], param.Parameterized)):+ if is_method and isinstance(args[0], param.Parameterized): dinfo = getattr(wrapped_func, '_dinfo', {}) hash_args = tuple(getattr(args[0], d) for d in dinfo.get('dependencies', ())) + args[1:] hash_kwargs = dict(dinfo.get('kw', {}), **kwargs) hash_value = compute_hash(func, hash_funcs, hash_args, hash_kwargs, approximate) time = _TIME_FN() - # If the function is defined inside a bokeh/panel application- # it is recreated for each session, therefore we cache by- # file, class and qualified function name. Since that is shared- # by all functions a factory generates we also key on the values- # the function closes over.- module = sys.modules.get(func.__module__)- fname = '__main__' if func.__module__ == '__main__' else getattr(module, '__file__', None)- if fname is None:- fname = fallback_fname- qualname = getattr(func, '__qualname__', func_name)- if is_method:- func_hash = (fname, type(args[0]).__name__, qualname)- else:- func_hash = (fname, qualname)- func_hash += (closure_key,)- if per_session:- func_hash += (id(state.curdoc),)- func_hash = hashlib.sha256(_generate_hash(func_hash)).hexdigest()+ scope = (+ type(args[0]).__name__ if is_method else None,+ id(state.curdoc) if per_session else None,+ )+ bucket = buckets.get(scope)+ if bucket is None:+ if len(buckets) >= _MAX_BUCKETS:+ buckets.clear()+ if is_method:+ func_hash = (fname, scope[0], qualname)+ else:+ func_hash = (fname, qualname)+ func_hash += (closure_key, settings)+ if per_session:+ func_hash += (scope[1],)+ func_hash = hashlib.sha256(_generate_hash(func_hash)).hexdigest()+ buckets[scope] = bucket = (func_hash, [time])+ func_hash, sweep = bucket func_hashes[0] = func_hash func_cache = state._memoize_cache.get(func_hash) if func_cache is None: if to_disk: from diskcache import Index- cache = Index(os.path.join(cache_path, func_hash))+ func_cache = Index(os.path.join(cache_path, func_hash)) else:- cache = {}- state._memoize_cache[func_hash] = func_cache = cache-- if ttl is not None:- _cleanup_ttl(func_cache, ttl, time)+ func_cache = {}+ state._memoize_cache[func_hash] = func_cache - if hash_value in func_cache:- return func_cache, hash_value, time+ entry = func_cache.get(hash_value) - if max_items is not None:+ if ttl is not None:+ if entry is not None and (time - entry[1]) > ttl:+ func_cache.pop(hash_value, None)+ entry = None+ if (time - sweep[0]) > ttl:+ # An expired hit is dropped above, so the sweep that+ # frees the other expired entries only has to run+ # once per ttl rather than on every call+ sweep[0] = time+ _cleanup_ttl(func_cache, ttl, time)++ if entry is None and track_usage and len(func_cache) >= max_items:+ if ttl is not None:+ # Expired entries should not cost live ones their slot+ _cleanup_ttl(func_cache, ttl, time) _cleanup_cache(func_cache, policy, max_items, time) - return func_cache, hash_value, time+ return func_cache, hash_value, time, entry++ def record_hit(func_cache, hash_value, time):+ with lock:+ entry = func_cache.get(hash_value)+ if entry is None:+ return+ ret, ts, count, _ = entry+ if lru_dict:+ del func_cache[hash_value]+ func_cache[hash_value] = (ret, ts, count+1, time) if iscoroutinefunction(func): @functools.wraps(func) async def wrapped_func(*args: _P.args, **kwargs: _P.kwargs) -> _R:- func_cache, hash_value, time = hash_func(*args, **kwargs)- if hash_value in func_cache:- with lock:- ret, ts, count, _ = func_cache[hash_value]- func_cache[hash_value] = (ret, ts, count+1, time)- else:- ret = await t.cast("Awaitable[t.Any]", func(*args, **kwargs))- with lock:- func_cache[hash_value] = (ret, time, 0, time)+ func_cache, hash_value, time, entry = lookup(args, kwargs)+ if entry is not None:+ if track_usage:+ record_hit(func_cache, hash_value, time)+ return entry[0]+ ret = await t.cast("Awaitable[t.Any]", func(*args, **kwargs))+ with lock:+ func_cache[hash_value] = (ret, time, 0, time) return ret else: @functools.wraps(func) def wrapped_func(*args: _P.args, **kwargs: _P.kwargs) -> _R:- func_cache, hash_value, time = hash_func(*args, **kwargs)- if hash_value in func_cache:- with lock:- ret, ts, count, _ = func_cache[hash_value]- func_cache[hash_value] = (ret, ts, count+1, time)- else:- ret = func(*args, **kwargs)- with lock:- func_cache[hash_value] = (ret, time, 0, time)+ func_cache, hash_value, time, entry = lookup(args, kwargs)+ if entry is not None:+ if track_usage:+ record_hit(func_cache, hash_value, time)+ return entry[0]+ ret = func(*args, **kwargs)+ with lock:+ func_cache[hash_value] = (ret, time, 0, time) return ret def clear(func_hashes=func_hashes):panel/tests/io/test_cache.py187 + / 0 −
@@ -112,6 +112,78 @@ def test_list_hash(): assert not hashes_equal([0], [1]) assert not hashes_equal(['a', ['b']], ['a', ['c']]) +def test_flat_container_hash_distinguishes_types():+ assert not hashes_equal([1], [True])+ assert not hashes_equal([1], [1.0])+ assert not hashes_equal(['1'], [b'1'])+ assert not hashes_equal([0.0], [-0.0])+ assert not hashes_equal([None], [0])+ assert not hashes_equal([1], (1,))+ assert not hashes_equal({'a': 1}, {'a': True})+ assert not hashes_equal({1: 'a'}, {True: 'a'})+ assert not hashes_equal([1, 2], [1, [2]])+ assert hashes_equal({'a': 1, 'b': 2}, {'a': 1, 'b': 2})+ assert not hashes_equal({'a': 1, 'b': 2}, {'b': 2, 'a': 1})++def test_flat_container_hash_subclasses_not_treated_as_natives():+ class Name(str):+ pass++ Point = namedtuple('Point', ['x'])+ assert not hashes_equal(['a'], [Name('a')])+ assert not hashes_equal((1,), Point(1))++def test_custom_scalar_hash_func_applies_inside_containers():+ def f(a):+ return a++ same = {int: lambda obj: b'int'}+ assert compute_hash(f, same, ([1, 2],), {}) == compute_hash(f, same, ([3, 4],), {})+ assert compute_hash(f, same, ({'a': 1},), {}) == compute_hash(f, same, ({'a': 2},), {})+ assert compute_hash(f, {}, ([1, 2],), {}) != compute_hash(f, {}, ([3, 4],), {})++def test_hash_funcs_exact_type_overrides_named_default():+ # The docs recommend hashing frames by a version, which must take+ # precedence over the default registered under the type's name+ df = pd.DataFrame({'a': [1]})+ df.attrs['version'] = 1+ other = pd.DataFrame({'a': [2]})+ other.attrs['version'] = 1+ calls = []++ @cache(hash_funcs={pd.DataFrame: lambda df: str(df.attrs['version']).encode()})+ def process(df):+ calls.append(1)+ return len(df)++ assert process(df) == 1+ assert process(other) == 1+ assert len(calls) == 1++def test_hash_funcs_registered_directly_apply_immediately():+ class Versioned:+ def __init__(self, version):+ self.version = version++ a, b = Versioned(1), Versioned(1)+ assert not hashes_equal(a, b)+ cache_module._hash_funcs[Versioned] = lambda obj: str(obj.version).encode()+ try:+ assert hashes_equal(a, b)+ assert not hashes_equal(a, Versioned(2))+ finally:+ del cache_module._hash_funcs[Versioned]+ assert not hashes_equal(a, b)++def test_hash_func_lookup_not_memoized_for_instance_dependent_matches():+ # Dataclass instances are matched by a predicate, which may depend+ # on the instance rather than its type+ assert hashes_equal(_Point(1), _Point(1))+ assert not hashes_equal(_Point(1), _Point(2))+ assert _Point not in cache_module._HASH_FUNC_CACHE+ _generate_hash([1])+ assert cache_module._HASH_FUNC_CACHE[list] is cache_module._container_hash+ def test_list_hash_recursive(): # Recursion l = [0]@@ -523,6 +595,121 @@ def test_cache_ttl(to_disk, tmp_path): time.sleep(0.2) assert fn(0, 0) == 1 +def test_cache_ttl_expired_entries_never_returned(monkeypatch):+ # The sweep that frees expired entries runs once per ttl, an+ # expired entry must still never be returned in between+ clock = [0.0]+ monkeypatch.setattr(cache_module, '_TIME_FN', lambda: clock[0])+ calls = []++ @cache(ttl=10)+ def identity(value):+ calls.append(value)+ return value++ buckets = set(state._memoize_cache)+ identity(0)+ bucket, = (state._memoize_cache[k] for k in set(state._memoize_cache) - buckets)+ for value in range(1, 5):+ clock[0] = value+ identity(value)+ assert len(bucket) == 5++ clock[0] = 10+ assert identity(0) == 0+ assert len(calls) == 5++ clock[0] = 10.5+ assert identity(0) == 0+ assert len(calls) == 6+ # The sweep that ran found nothing else expired+ assert len(bucket) == 5++ clock[0] = 15+ assert identity(1) == 1+ assert len(calls) == 7+ # 2-4 have expired but linger until the next sweep+ assert len(bucket) == 5++ clock[0] = 21+ assert identity(0) == 0+ assert len(calls) == 8+ # Swept, only 0 and 1 remain+ assert len(bucket) == 2++def test_cache_ttl_with_max_items_evicts_expired_first(monkeypatch):+ clock = [0.0]+ monkeypatch.setattr(cache_module, '_TIME_FN', lambda: clock[0])+ calls = []++ @cache(ttl=10, max_items=3, policy='lru')+ def identity(value):+ calls.append(value)+ return value++ identity(0)+ clock[0] = 5+ identity(1)+ clock[0] = 6+ identity(2)+ clock[0] = 10.5+ # Sweeps 0 and makes 1 the most recently used+ assert identity(1) == 1+ clock[0] = 11+ identity(3)+ clock[0] = 16+ # The cache is full, 1 has expired and 2 is the least recently+ # used: 1 must go rather than 2+ identity(4)+ assert identity(2) == 2+ assert calls == [0, 1, 2, 3, 4]+ assert identity(1) == 1+ assert calls == [0, 1, 2, 3, 4, 1]++def test_cache_lru_tracks_hits():+ global OFFSET+ OFFSET.clear()+ fn = cache(function_with_args, max_items=3, policy='lru')+ assert fn(0, 0) == 0+ assert fn(0, 1) == 1+ assert fn(0, 2) == 2+ assert fn(0, 1) == 1+ assert fn(0, 0) == 0+ assert fn(0, 3) == 3 # (0, 2) should be evicted+ assert fn(0, 0) == 0+ assert fn(0, 1) == 1+ assert fn(0, 2) == 3++def test_cache_decorations_with_different_settings_do_not_share(monkeypatch):+ clock = [0.0]+ monkeypatch.setattr(cache_module, '_TIME_FN', lambda: clock[0])+ calls = []++ def identity(value):+ calls.append(value)+ return value++ plain = cache(identity)+ expiring = cache(identity, ttl=1)+ assert plain(1) == 1+ clock[0] = 2+ # A separate bucket, so this neither hits the plain entry nor+ # expires it+ assert expiring(1) == 1+ assert plain(1) == 1+ assert len(calls) == 2++@diskcache_available+def test_cache_to_disk_not_shadowed_by_memory_cache(tmp_path):+ def identity(value):+ return value++ in_memory = cache(identity)+ on_disk = cache(identity, to_disk=True, cache_path=tmp_path)+ in_memory(1)+ on_disk(1)+ assert list(tmp_path.glob('*'))+ def test_cache_on_undecorated_parameterized_method(): class Model(param.Parameterized): data = param.Parameter(default=1)