Rcpp11 / attributes

Standalone implementation of Rcpp attributes.
Other
3 stars 1 forks source link

automatic registration for exported C/C++ functions #2

Open kevinushey opened 10 years ago

kevinushey commented 10 years ago

Just an idea, one thing that would be really nice to see for a package using attributes: automatic registration of C routines that we want exported to a package's namespace. So, if I write something like

// [[attributes::register]]
SEXP do_something(SEXP x) { /* ... */ }

we can auto-generate a file, say <pkgname>_init.c in src that contains

#include <R.h>
#include <Rinternals.h>

#include <R_ext/Rdynload.h>

SEXP do_something(SEXP x);

R_CallMethodDef callMethods[] = {
  {"Cdo_something", (DL_INIT) &do_something, 1},
  {NULL, NULL, 0}
};

void R_init_<pkg>(DllInfo *info) {
  R_registerRoutines(info, NULL, callMethods, NULL, NULL);
  R_useDynamicSymbols(info, FALSE);
}

We could then avoid the small overhead with dynamic symbol lookup and automate the drudgery of writing this ourselves.

Rcpp-exported functions could be parsed from the RcppExports.cpp file generated from compileAttributes, so the pre-package-build workflow would just be

compileAttributes()
registerExports()

Worth the investment?

romainfrancois commented 10 years ago

Maybe I'm not following, but is this not what [[Rcpp::interfaces(cpp)]] gives you ?

kevinushey commented 10 years ago

That's a bit different, I think -- it makes the C/C++ functions callable by other packages, whereas I'm thinking about influencing how one calls a C/C++ function within their own package. Ie, being able to use

.Call(Cdo_something, ..., PACKAGE=<pkg>)

vs

.Call("do_something", ..., PACKAGE=<pkg>)

in my own package <pkg>.

The compileAttributes()-generated RcppExports.R defaults to the second form, while the first form is a bit faster as it avoids symbol lookup. This could be used with both vanilla C code and Rcpp code.

This is mostly motivated by 5.4 and 5.4.1 in the R-exts manual.

romainfrancois commented 10 years ago

Ah. ok. With you now. I definitely find this interesting.

Once I'm done with #1 we should have attributes parsing files and structuring the data in some way, possibly just R lists ...

The idea eventually would be that package foo can create foo::bar attributes to do stuff with the parsed data.