<div dir="ltr"><div>Hey there,</div><div><br></div><div>Looking at some old R code that I have I found this C++ function that allows evaluating R-written functions within C++ using Rcpp. While this is no news, the neat thing of it is that it seems to be faster than Rcpp::Function. Using microbenchmark I compared using Rcpp::function vs my implementation vs calling the function from R itself and this is what I got</div><div><br></div><div><div>Unit: relative</div><div>                expr min  lq     mean median  uq max neval</div><div>  cppFuncall(x, fun) 1.3 1.3 1.39    1.3 1.4  83 10000</div><div> RcppFuncall(x, fun) 7.2 7.1 7.13    6.9 6.8  89 10000</div><div>              fun(x) 1.0 1.0 1.00    1.0 1.0   1 10000</div></div><div><br></div>So, on average, while Rcpp::Function took ~7 times the R call took, my implementation took ~1.3 times. To be sure I was not breaking anything I ran the example using valgrind and there is no memory leak. The source code for the test follows:<div><br></div><div>-------- example_calling_r_functions.cpp ----</div><div><br></div><div><div>#include <Rinternals.h></div><div>#include <Rcpp.h></div><div><br></div><div>// [[Rcpp::export]]</div><div>SEXP cppFuncall(SEXP par, SEXP fn)</div><div>{</div><div>  SEXP R_fcall, ans;</div><div><br></div><div>  if(!isFunction(fn)) error("'fn' must be a function");</div><div>  R_fcall = PROTECT(lang2(fn, R_NilValue));</div><div>  </div><div>  SETCADR(R_fcall, par);</div><div>  ans=eval(R_fcall, R_GlobalEnv);</div><div>  UNPROTECT(1);</div><div>  return ans;</div><div>}</div><div> </div><div>using namespace Rcpp;</div><div><br></div><div>// [[Rcpp::export]]</div><div>SEXP RcppFuncall(NumericVector par, Function fn)</div><div>{</div><div>  return fn(par);</div><div>}</div><div><br></div><div><br></div><div><br></div><div>/*** R</div><div># R function to be called</div><div>fun <- function(x) {</div><div>  -cos(x[1])*cos(x[2])*exp(-((x[1] - pi)^2 + (x[2] - pi)^2))</div><div>}</div><div><br></div><div># Input data</div><div>set.seed(3331)</div><div>x <- runif(1e3)</div><div><br></div><div># Benchmarking</div><div>library(microbenchmark)</div><div>microbenchmark(</div><div>  cppFuncall(x, fun), RcppFuncall(x,fun), fun(x), times=1e4,</div><div>  unit="relative", control = list(warmup=100)</div><div>)</div><div>*/</div><div><br></div><div>-------- example_calling_r_functions.cpp ----<br></div></div><div><br></div><div>I've asked around about how to make things faster for function calls in Rcpp but, from what I've been told it is difficult since the implementation of Rcpp::Function actually has to go back to R to work (or something like that :P). Now, this implementation, -cppFuncall-, has no problem when it comes to passing wrong arguments, e.g. if you pass a character vector to it R will complain but there won't be any system crash. One big difference is that here I'm relying on passing all the function's arguments in a single object while Rcpp::Function does not. Either way, if this is OK this could be a nice extra feature for Rcpp, I'm thinking of optimization routines (or other kinds of algorithms) that rely on calling R functions multiple times.</div><div><br></div><div>The thing is that I'm still learning C++  and  I'm not Rinternals expert at all! So I would love to get some feedback from you guys. Does this function, -cppFuncall-, looks OK? in other words, am I doing/getting something wrong here?</div><div><br></div><div>Thanks,</div><div><br></div><div><div><div class="gmail_signature" data-smartmail="gmail_signature"><div dir="ltr"><div>George G. Vega Yon<br>+1 (626) 381 8171<br><a href="http://www.its.caltech.edu/~gvegayon/" target="_blank">http://www.its.caltech.edu/~gvegayon/</a></div></div></div></div>
</div></div>