dstl / Stone-Soup

A software project to provide the target tracking community with a framework for the development and testing of tracking algorithms.
https://stonesoup.rtfd.io
MIT License
385 stars 127 forks source link

lru_cache on instance methods #801

Open bpdavis86 opened 1 year ago

bpdavis86 commented 1 year ago

This repo is good work, thanks for providing a valuable resource for tracking researchers!

I noticed you are using lru_cache for instance methods in the repo, e.g. in updater.kalman.KalmanUpdater.predict_measurement. It has been noted that this can cause memory leaks due to cyclic dependency in the garbage collection (the cache never releases reference to the instance).

https://stackoverflow.com/questions/33672412/python-functools-lru-cache-with-instance-methods-release-object

In my own code, I've worked around this by implementing a plain function (or static method) that is lru_cached and then redirecting the instance method to it. Some alternative solutions are provided at the above post. (I see you've used the staticmethod technique in some places as well).

Don't know if you have actually encountered issues with this in practice, but I thought I'd bring it up.

-Ben Davis

sdhiscocks commented 1 year ago

Thanks for raising this @bpdavis86. It is something we were aware of, but as we don't create many Predictor or Updater objects, shouldn't really be an issue. I think looking at a per instance cache solution is probably best.