Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

System.Interactive: Difference between Memoize() and MemoizeAll()?

In System.Interactive.dll (v1.0.2521.0) from Reactive Extensions, EnumerableEx has both a Memoize method and a MemoizeAll method. The API documentation is identical for both of them:

Creates an enumerable that enumerates the original enumerable only once and caches its results.

However, these methods are clearly not identical. If I use Memoize, my enumerable has values the first time I enumerate it, and seems to be empty the second time. If I use MemoizeAll then I get the behavior I would expect from the description of either method - I can enumerate the result as many times as I want and get the same results each time, but the source is only enumerated once.

Can anyone tell me what the intended difference between these methods is? What is the use-case for Memoize? It seems like a fairly useless method with really confusing documentation.

like image 399
Joel Mueller Avatar asked May 28 '10 18:05

Joel Mueller


1 Answers

to put it really simply, MemoizeAll only caches items that you actually use -- but it holds on to all the cached items forever. Memoize caches items immediately, but only up to the specified buffer size. This lets you be a lot more efficient with memory usage, etc, but you only want to use the latter if you understand how all consumers of the enumerable source will use it.

There's a really great explanation of the difference in this blog post: http://bartdesmet.net/blogs/bart/archive/2010/01/07/more-linq-with-system-interactive-functional-fun-and-taming-side-effects.aspx

like image 95
Warren Rumak Avatar answered Oct 20 '22 18:10

Warren Rumak