Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Created function with memoization causes partial cache miss with lapply #12

Open
russellpierce opened this issue Nov 24, 2015 · 1 comment

Comments

@russellpierce
Copy link

If we create a daughter function with a function and then memorize then we (sometimes?!) miss the cache on lapply.

Minimal Example:

> library(R.cache)
R.cache v0.12.0 (2015-11-12) successfully loaded. See ?R.cache for help.
> sessionInfo()
R version 3.2.2 (2015-08-14)
Platform: x86_64-pc-linux-gnu (64-bit)
Running under: Ubuntu 14.04.3 LTS

locale:
 [1] LC_CTYPE=en_US.UTF-8       LC_NUMERIC=C              
 [3] LC_TIME=en_US.UTF-8        LC_COLLATE=en_US.UTF-8    
 [5] LC_MONETARY=en_US.UTF-8    LC_MESSAGES=en_US.UTF-8   
 [7] LC_PAPER=en_US.UTF-8       LC_NAME=C                 
 [9] LC_ADDRESS=C               LC_TELEPHONE=C            
[11] LC_MEASUREMENT=en_US.UTF-8 LC_IDENTIFICATION=C       

attached base packages:
[1] stats     graphics  grDevices utils     datasets  methods   base     

other attached packages:
[1] R.cache_0.12.0

loaded via a namespace (and not attached):
[1] R.methodsS3_1.7.0 digest_0.6.8.3    R.utils_2.1.0     R.oo_1.19.0      
> 
> # wrapped example, one misses cache
> creator <- function(delay) {
+   function(foo=list(bar=1)) {
+     Sys.sleep(delay)
+     return(foo)
+   }
+ }
> 
> slowFunction.l <- creator(2)
> slowFunction.l.m <- addMemoization(slowFunction.l)
> 
> system.time(slowFunction.l.m(list(bar=1)))
   user  system elapsed 
  0.005   0.000   2.005 
> system.time(slowFunction.l.m(list(bar=2)))
   user  system elapsed 
  0.004   0.000   2.015 
> system.time(lapply(list(list(bar=1),list(bar=2)),slowFunction.l.m))
   user  system elapsed 
  0.006   0.000   2.018 

Compare with

slowFunction.l.raw <- function(foo=list(bar=1)) {
   Sys.sleep(2)
   return(foo)
}

slowFunction.l.raw.m <- addMemoization(slowFunction.l.raw) 
system.time(slowFunction.l.raw.m(list(bar=1)))
system.time(slowFunction.l.raw.m(list(bar=2)))
system.time(lapply(list(list(bar=1),list(bar=2)),slowFunction.l.m))

Which works as expected and seems functionally equivalent. Possibly related to #11 or shares in a misunderstanding on my part?

@russellpierce russellpierce changed the title Created function with memoization causes partial cache miss Created function with memoization causes partial cache miss with lapply Nov 24, 2015
@HenrikBengtsson
Copy link
Owner

Thanks. I've got little time to look into this for the next couple weeks, but it's an important point. Just a quick reply for now:

I think this is the expected behavior with the current implementation of addMemoization(); it's simply not clever enough to identify the necessary globals that are part of promises when constructing the key for the memoization. It could probably be improved, particularly with help of the globals package. FYI, the dominant use of R.cache is through the explicit load/saveCache() mechanism (millions of CPU hours by now) and less so through the addMemoization() (which therefore has less real-world CPU mileage).

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants