Lazy caching map()?

aliak something at something.com
Wed Mar 14 00:12:44 UTC 2018


On Tuesday, 13 March 2018 at 17:24:35 UTC, H. S. Teoh wrote:
> Very nice. Using memoize did occur to me, but I needed an array 
> interface to it.  Didn't think of using memoize with map, for 
> some reason.  Thanks for the idea!
>
> However, looking at the implementation of memoize, it seems 
> that it's caching the result globally, with very little control 
> over the cache except the size. I wonder if there's a way to 
> control it better, i.e., free the cache once there are no more 
> references to it, and have the cache size depend on the data 
> size.  Basically, in my use case, once I map an array it's not 
> predictable in advance how many elements will be accessed 
> (i.e., it's hard to decide on an optimal cache size for 
> memoize), but this access will happen pretty soon afterwards, 
> and then the array will be discarded (i.e., no point holding on 
> old entries in the cache).  I would prefer that the cache will 
> be cleared once the mapped array has been GC'd, but memoize() 
> seems to hold on to the cached results indefinitely.
>
>
> T

No worries! And ugh, can't think off the top of my head how to 
improve it other than to make it a type and give it scope so that 
it can die at some point. But that would make it uglier to use as 
well.

Btw, I just saw someone posted a link to an old forum post of 
yours: 
https://forum.dlang.org/post/mailman.2562.1403196857.2907.digitalmars-d@puremagic.com

Mind if I add that (or a version of it) to a library I'm writing? 
(it's an optional type on dub)

Cheers
- Ali


More information about the Digitalmars-d mailing list