[e-lang] Looser memoization and constant-folding: will this work?

Mark S. Miller markm at cs.jhu.edu
Sat May 6 12:50:14 EDT 2006


Kevin Reid wrote:
>    - Is this correct?

I think so.


>    - Is there a way to handle the DeepFrozen-returns-DeepFrozen case  
> which does not involve two possibly-arbitrarily-expensive evaluations?

Relevant excerpt of original:

       def cache := [].asMap().diverge()

       def memoized implements DeepFrozenStamp {
         match msg :DeepFrozen {
           cache.fetch(msg, thunk {
             def result := E.callWithPair(base, msg)
             if (result =~ dpbc :DeepPassByCopy) {
               cache[msg] := dpbc
             } else {
               result
             }
           })
         }

The following seems much too complicated, but perhaps it can be simplified:

       # map from msgs to good results
       def cache := [].asMap().diverge()
       # map from msgs to either null or [possible result]
       def dfCache := [].asMap().diverge()

       def memoized implements DeepFrozenStamp {
         match msg :DeepFrozen {
           cache.fetch(msg, thunk {
             def result := E.callWithPair(base, msg)
             if (result =~ dpbc :DeepPassByCopy) {
               cache[msg] := dpbc
             } else {
               if (result =~ df :DeepFrozen) {
                 if (dfCache.maps(msg)) {
                   if (dfCache[msg] =~ [oldResult]) {
                     if (df == oldResult) {
                       # Happened again, so memoize it for real
                       cache[msg] := df
                       dfCache.removeKey(msg)
                     } else {
                       # Not the same, so remember not to bother
                       dfCache[msg] := null
                     }
                   } else {
                     # I remembered not to bother, so I don't
                   }
                 } else {
                   # Remember possible result
                   dfCache[msg] := [result]
                 }
               } else {
                 # not DeepFrozen. Should I remember not to bother?
                 # dfCache[msg] := null
               }
               result
             }
           })
         }



-- 
Text by me above is hereby placed in the public domain

     Cheers,
     --MarkM



More information about the e-lang mailing list