What are you trying to use this for? It seems to me that for memo tables you almost never have references to they keys outside the lookup table since the keys are usually computed right at the last minute, and then discarded (otherwise it might be easier to just cache stuff outside the function). <br>
<br>For example with a naive fibs, the values you are passing in are computed, and probably don&#39;t exist before you do the recursive call, and then are discarded shortly afterward.<br><br>It seems like putting a cap on the cache size, and then just overwriting old entries would be better.<br>
Am I missing something?<br><br>- Job<br><br><br><br><div class="gmail_quote">On Wed, Sep 16, 2009 at 4:48 PM, Rodney Price <span dir="ltr">&lt;<a href="mailto:rodprice@raytheon.com">rodprice@raytheon.com</a>&gt;</span> wrote:<br>
<blockquote class="gmail_quote" style="border-left: 1px solid rgb(204, 204, 204); margin: 0pt 0pt 0pt 0.8ex; padding-left: 1ex;">How does garbage collection work in an example like the one below?  You<br>
memoize a function with some sort of lookup table, which stores function<br>
arguments as keys and function results as values.  As long as the<br>
function remains in scope, the keys in the lookup table remain in<br>
memory, which means that the keys themselves always remain reachable<br>
and they cannot be garbage collected.  Right?<br>
<br>
So what do you do in the case where you know that, after some period of<br>
time, some entries in the lookup table will never be accessed?  That is,<br>
there are no references to the keys for some entries remaining, except<br>
for the references in the lookup table itself.  You&#39;d like to allow the<br>
memory occupied by the keys to be garbage collected.  Otherwise, if the<br>
function stays around for a long time, the size of the lookup table<br>
always grows.  How do you avoid the space leak?<br>
<br>
I notice that there is a function in Data.IORef,<br>
<br>
mkWeakIORef :: IORef a -&gt; IO () -&gt; IO (Weak (IORef a))<br>
<br>
which looks promising.  In the code below, however, there&#39;s only one<br>
IORef, so either the entire table gets garbage collected or none of it<br>
does.<br>
<br>
I&#39;ve been reading the paper &quot;Stretching the storage manager: weak<br>
pointers and stable names in Haskell,&quot; which seems to answer my<br>
question.  When I attempt to run the memoization code in the paper on<br>
the simple fib example, I find that -- apparently due to lazy<br>
evaluation -- no new entries are entered into the lookup table, and<br>
therefore no lookups are ever successful!<br>
<br>
So apparently there is some interaction between lazy evaluation and<br>
garbage collection that I don&#39;t understand.  My head hurts.  Is it<br>
necessary to make the table lookup operation strict?  Or is it<br>
something entirely different that I am missing?<br>
<br>
-Rod<br>
<br>
<br>
On Thu, 10 Sep 2009 18:33:47 -0700<br>
Ryan Ingram &lt;<a href="mailto:ryani.spam@gmail.com">ryani.spam@gmail.com</a>&gt; wrote:<br>
<br>
&gt;<br>
&gt; memoIO :: Ord a =&gt; (a -&gt; b) -&gt; IO (a -&gt; IO b)<br>
&gt; memoIO f = do<br>
&gt;    cache &lt;- newIORef M.empty<br>
&gt;    return $ \x -&gt; do<br>
&gt;        m &lt;- readIORef cache<br>
&gt;        case M.lookup x m of<br>
&gt;            Just y -&gt; return y<br>
&gt;            Nothing -&gt; do let res = f x<br>
&gt;                          writeIORef cache $ M.insert x res m<br>
&gt;                          return res<br>
&gt;<br>
&gt; memo :: Ord a =&gt; (a -&gt; b) -&gt; (a -&gt; b)<br>
&gt; memo f = unsafePerformIO $ do<br>
&gt;     fmemo &lt;- memoIO f<br>
&gt;     return (unsafePerformIO . fmemo)<br>
&gt;<br>
&gt; I don&#39;t think there is any valid transformation that breaks this,<br>
&gt; since the compiler can&#39;t lift anything through unsafePerformIO.  Am I<br>
&gt; mistaken?<br>
&gt;<br>
&gt;   -- ryan<br>
<br>
_______________________________________________<br>
Haskell-Cafe mailing list<br>
<a href="mailto:Haskell-Cafe@haskell.org">Haskell-Cafe@haskell.org</a><br>
<a href="http://www.haskell.org/mailman/listinfo/haskell-cafe" target="_blank">http://www.haskell.org/mailman/listinfo/haskell-cafe</a><br>
</blockquote></div><br>