sympy/sympy — pull request #7464 by pbrady (2014-05-04 13:42:48+00:00)
Title: New Caching

I was recently bit by the caching mechanism in Sympy which grew without bounds and crashed my machine repeatedly (i.e. https://github.com/sympy/sympy/issues/6321). While this could have been avoided with calls to clear_cache, this only became clear after several days of digging around. In general, the default behavior should probably be to not eat all of my memory. I've attempted to solve this with a new caching mechanism based on weakrefs and fixed length deques. I've tested this on my machine (Python 3.4) and it appears to work well (i.e. similar performance and no memory gobbling).

The implementation involves both a WeakKeyDictionary and a WeakValueDictionary since we cannot create weakrefs to most of our function arguments or return values. This dicts are tied together with a dummy object for which the only strong reference is in the deque. Once the dummy object gets pushed out of the deque, the items are deleted from both dicts..

I'm not to sure about the optimal size for the deque. I've set it to 2000 without any adverse effects.