spey.system.cache._PerInstanceCacheDescriptor

spey.system.cache._PerInstanceCacheDescriptor#

class spey.system.cache._PerInstanceCacheDescriptor(fn: Callable, maxsize: int, copy_on_return: bool)[source]#

Non-data descriptor that provides per-instance caching for instance methods.

On the first instance.method attribute lookup the descriptor builds a dedicated cached callable whose internal cache storage is private to that instance. The wrapper is stored in instance.__dict__[method_name] so that Python’s attribute-lookup rules (instance dict beats non-data descriptor) bypass the descriptor on all subsequent accesses – zero overhead after the first call.

For classes that use __slots__ without __dict__, the descriptor falls back to a descriptor-level mapping keyed by id(obj). If the instance supports weak references a callback is registered to clean up the mapping entry when the instance is garbage-collected; otherwise the entry persists (bounded, and equivalent to the prior behaviour where self was serialised into a closure-level cache key).

Two distinct instances therefore maintain completely independent caches even when they invoke the method with identical arguments, and instances of different subclasses that happen to share the same method name are equally isolated.

self is never serialised into cache keys. The cache dict holds only non-self argument tokens as keys and computed results as values; no reference to the instance is stored in the cache dictionary.

This class is an implementation detail of cache_results(); obtain one via cache_results(..., per_instance=True) or by decorating a method whose first parameter is self (auto-detected).

__init__(fn: Callable, maxsize: int, copy_on_return: bool) None[source]#

Methods

__init__(fn, maxsize, copy_on_return)