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

Søren Højsgaard sorenh at math.aau.dk
Mon Dec 9 23:14:20 CET 2013


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?

All the best
Søren


More information about the Rcpp-devel mailing list