[Rd] caching frequently used values

Henrik Bengtsson hb at stat.berkeley.edu
Thu Dec 14 08:02:28 CET 2006


I use the R.oo Object class for what has been suggested previously.
The Object class can be thought of as utility wrapper class for
environments (actually environments gained much of its behavior some
time ago when "$" etc was being mapped to get() calls).

For caching to file, take a look at the R.cache package.  From ?loadCache:

 simulate <- function(mean, sd) {
       # 1. Try to load cached data, if already generated
       key <- list(mean, sd)
       data <- loadCache(key)
       if (!is.null(data)) {
         cat("Loaded cached data\n")
         return(data);
       }

       # 2. If not available, generate it.
       cat("Generating data from scratch...")
       data <- rnorm(1000, mean=mean, sd=sd)
       Sys.sleep(1)             # Emulate slow algorithm
       cat("ok\n")
       saveCache(data, key=key, comment="simulate()")

       data;
     }

     data <- simulate(2.3, 3.0)
     data <- simulate(2.3, 3.5)
     data <- simulate(2.3, 3.0) # Will load cached data

>From the 'key' list, loadCache()/saveCache() will calculate a 32-char
hexadecimal hashcode (using the digest package), which is used as a
filename in the .Rcache/ directory.  Thus, you don't have to worry
about coming up with unique cache names; the chances for a false hit
using the digest algorithms are very small. As long as the 'key'
object is not too large, this procedure is very fast.

Then, combining both in-memory and on-file caching you can start doing
clever this.  In my already too long wish list, I have some ideas on a
special class for in-memory/on-file caching which can be used
globally. For instance, when we run out of memory, in-memory cached
values can be stored to file or discarded and so on.  Now probably
someone says this is just about reinventing the swap of already well
developed OSes; I would say we can do much better than a generic swap
since we know what the data is and how long it takes to recalculate it
etc.

Hope this helps

Henrik



Combining the k

On 12/14/06, Tamas K Papp <tpapp at princeton.edu> wrote:
> On Wed, Dec 13, 2006 at 03:05:46PM -0800, Robert Gentleman wrote:
>
> > e1 = new.env(hash=TRUE)
> >
> > e1[["1"]] = whateveryouwant
> >
> > ie. just transform to characters, but I don't see why you want to do
> > that - surely there are more informative names to be used -
>
> Because they are derivatives, and best indexed by numbers.  I wrote an
> example to demonstrate what I think the solution is (for memoizing
> powers of numbers).  It works, but I am not an experienced programmer:
> can you please look at it to check that I do things right and do not
> abuse any feature of R?
>
> ## memoize powers of integers
>
> createpowerlist <- function(n) {
>   list(n=n,env=new.env(hash=TRUE))
> }
>
> getpower <- function(powerlist,i) {
>   iname <- as.character(i)
>   if (exists(iname,powerlist$env))
>     get(iname,powerlist$env)
>   else {
>     res <- i^powerlist$n                # result
>     assign(iname,res,powerlist$env)
>     res
>   }
> }
>
> cubelist <- createpowerlist(3)
> exists("12",cubelist$env)               # FALSE
> getpower(cubelist,12)                   # 1728
> exists("12",cubelist$env)               # TRUE
>
>
> Thanks,
>
> Tamas
>
> ______________________________________________
> R-devel at r-project.org mailing list
> https://stat.ethz.ch/mailman/listinfo/r-devel
>
>



More information about the R-devel mailing list