quaint-telephone-89068
03/07/2023, 6:05 PMcls -> function decorator -> function cache -> instance key. It would be nice to ensure:
1. Instances were not strongly referenced by cache keys to allow instances to be GC'd
2. cache entries themselves were not strongly referenced and could be GC'd once instances were GC'd
1 is easy to achieve post #6554 but 2 is trickier. The cache keys in question are compound, with the instance only forming the most significant "bit" and no other part of the system having a handle to the full key. This makes WeakKeyDictionary unlikely to be useful. A WeakValueDictionary would also likely lead to thrash of the cache:
consumer1.result = memoized.result()
<consumer1 done, gc'd - memoized result also now gc'd>
consumer2.result = memoized.result() # Need to recompute
Likely leveraging the ability to register a callback with weakref is the solution. That callback could clear the cache entry associated with the weakref'd instance - although the callback having a handle on the cache itself needs careful ref cycle consideration too. Generally - tricky.
pantsbuild/pantsuser
03/07/2023, 6:05 PM