Compiling R-functions used in production

I was just wondering if it is recommended to compile functions in R (with the "compiler" package) for R-functions that are used in a production environment, in order to improve speed & efficiency.

According to this book, compiling functions may result in a modest speed boost:

However, I have found some conflicting info online when searching for this (some say it improves speed, other say it doesn't matter anymore). Most articles I find about compiling R-functions appear to be quite old, so I feel this might be an outdated procedure.

Also, are there any reasons not to compile the functions, given that compiling increases speed? E.g. could the function become more unstable?

5 Likes

You should only put things into production that you have thoroughly tested in unit tests, integration tests and staging tests. And even prior to this, during development, you need to evaluate the cost/benefit of doing any activity.

In the case of using the byte-code compiler, during the development stage you must establish whether the byte-code compiler gives a performance advantage, given the cost of implementation.

  • As you (and the chapter in the book) already pointed out, the performance benefits of the byte-code compiler are quite modest (unless you wrote really bad R code to start with).
  • However, the cost of implementing the byte-code compiler is also low, especially if your code is in a package, in which case it's trivial to enable the byte-code compilation.

Some reliable resources you should consult:

Note that the compiler will not always save you time, especially if you compile dynamically (just in time). If you precompile in a package, you should always see some (marginal) improvement and not degradation.

3 Likes

The book was just published before 3.4.0 released. This is significant because R 3.4.0 introduced the JIT compiler by default (if I remember correctly, I added managed to sneak a footnote into the print version). This means that when you write a script, all functions are automatically compiled.

However, packages aren't byte compiled by default (only user code). As I mentioned in my book, you might only get a tiny speed up (say 20%), but for a single line change, that still seems worthwhile. I suspect that a future version of R will implement this, but for now, it's up to you.

For info, I've been byte compiling all external packages for over three years, with no issues.


It seems from R 3.5.0 packages will byte compiled by default

2 Likes