[Rcpp-devel] How many copies of my object do I make?

Dirk Eddelbuettel edd at debian.org
Tue Dec 10 18:33:08 CET 2013

On 9 December 2013 at 22:14, Søren Højsgaard wrote:
| Dear all,
| Using RcppEigen I've created a function for converting a sparse matrix (a dgCMatrix) to a standard dense matrix:
| typedef Eigen::SparseMatrix<double> SpMatd;
| typedef Eigen::MappedSparseMatrix<double> MSpMat;
| // [[Rcpp::export]]
| SEXP do_dgCMatrix2matrix ( SEXP XX_ ){
|   S4 DD(wrap(XX_));
|   List dn = clone(List(DD.slot("Dimnames")));
|   SpMatd X(as<SpMatd>(XX_));
|   Eigen::MatrixXd dMat;
|   dMat = Eigen::MatrixXd( X );
|   NumericMatrix Xout(wrap(dMat));
|   Xout.attr("dimnames") = dn;
|   return Xout;
| }
| The function does what I expect it to, but I am curious about how many times I really create some sort of "copy" of my input matrix and when all I do is put a small layer of "vernish" over my input matrix?
| 1) dMat is dense matrix, and that must (I guess) refer to new "object"??
| 2) X is a sparse matrix, and I suppose it is a new object?? 
| 2') If I had declared it to be MSpMat, then it would really just be the input object with a few bells and whistles??
| 3) But how about DD and Xout? Are they new objects (in terms of memory usage)?
| Put differently, if my input matrix occupies B bytes of memory, how many times B have I then created along the way?

Unsure. You really need to look. 

For sure when i) clone is called, ii) each time you pass from sparse to dense
and back.  For the others, you need to dig deeper.


Dirk Eddelbuettel | edd at debian.org | http://dirk.eddelbuettel.com

More information about the Rcpp-devel mailing list