[Rcpp-devel] Template instantiation of commonly-used types

Kevin Thornton krthornt at uci.edu
Wed Oct 15 19:24:36 CEST 2014


In looking through the Rcpp/0.11.3 code, I came across this file:

inst/include/Rcpp/vector/instantiation.h

The file contains the declarations for IntegerVector, NumericVector, etc.  It seems reasonable to assume that these types will be used in basically any code using Rcpp.

The declarations are aliases to templates.  For example:

typedef Vector<REALSXP> NumericVector ;

The effect of this is that every .cpp file declaring a NumericVector must recompile the template, because these declarations rely on implicit instantiation.  However, from the perspective of an Rcpp user, these types are actually not templates.  In other words, there is no NumericVector<T> template providing compile-time polymorphism, and NumericVector is a fully-specified type.  The implicit instantiation increases both compile time and object file size ("code bloat") for packages using Rcpp.  As such, I'd suggest that these types should be provided pre-compiled in the Rcpp dynamic object, which can be done via explicit template instantiation (http://en.cppreference.com/w/cpp/language/class_template).

I've put a modified instantation.h at https://gist.github.com/5180d60689fa8d6cb353.git and the corresponding (and incomplete) .cpp file at https://gist.github.com/c13135659e0b27421a3a.git.  This implementation should result in a situation where the object code for these types is present only once on a user's system, rather than being replicated once per package that is using Rcpp.

While I appreciate that Rcpp is "header-only", I think that the current declaration of these types is better-served with compiled code, for the reasons mentioned above.  However, I may be missing something--perhaps there is something about the way dynamic objects in R do things that means this approach would not work?

___________________________
Kevin Thornton
Associate Professor
Ecology and Evolutionary Biology
University of California, Irvine
http://www.molpopgen.org






More information about the Rcpp-devel mailing list