Reputation: 1747
Suppose I have the following simple function
void test(NumericMatrix some_mat){
}
When test is called, does Rcpp copy the entire some-mat
object instance onto the stack? I am wondering if it is better to pass parameters by reference to prevent copying the object on the stack. So is this a better way to do it?
void test(const NumericMatrix &some_mat){
}
Also, I am reading the source code for Rcpp on github. Where can I find the code of the proxy model for parameter passing?
Thanks for any advice.
Upvotes: 0
Views: 183
Reputation: 368399
A NumericMatrix
is a proxy object to a SEXP
where the P stands for pointer. So no, a copy is not made.
You can easily verify that by passing vector or matrices of 10 to the power of 0, 1, 2, 3, 4, 5, ... elements to the function and measuring the run time of the code. Were these copies, the time would go up a lot. I conjecture you will at best see small increases due to working with more data in the main part.
And no, sorry, I cannot summarize all the code (or the book) in a few lines here. But when I google the terms "Rcpp proxy object", I get 51.3 thousands hits. And this has been discussed here before too, see eg this answer of mine from a year ago.
Upvotes: 2