System.Interactive: Difference between Memoize() and MemoizeAll()?
In System.Interactive.dll (v1.0.2521.0) from Reactive Extensions, EnumerableEx
has both a 开发者_如何学CMemoize
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.
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
精彩评论