0

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.

Cauchy
  • 1,677
  • 2
  • 21
  • 30
  • You ask: _does Rcpp copy the entire some-mat object instance onto the stack?_ Did you memory-profile? And you point to the *wrong repository*. Try [this one](https://github.com/RcppCore/Rcpp). – Dirk Eddelbuettel Nov 25 '15 at 01:19

1 Answers1

2

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.

Community
  • 1
  • 1
Dirk Eddelbuettel
  • 360,940
  • 56
  • 644
  • 725
  • sorry I missed that post. I've been trying hard to fully understand your work. Is there a specific directory in the package available on github where I can read the code behind the proxy model? – Cauchy Nov 25 '15 at 04:58
  • You know where the sources are, what more do you expect me to say? Are you expecting this to be in a single line of code, magically? A very rough recursive `wc -l` over `src/` and `inst/include/` counts 95k lines. – Dirk Eddelbuettel Nov 25 '15 at 05:08
  • To a certain extent yes, I guess I am trying to see where you put the code relating to the proxy model, but it seems to be the wrong way to think about it. I'll continue to read the source code to try to see where you are coming from. Thank you sir. – Cauchy Nov 25 '15 at 05:25