r-lib/pkgload

Passes through `-O0` because of unset default `debug=T`

JZL opened this issue · 3 comments

JZL commented

Hi,

I've been chasing this performance hit when using load_all to quickly test out a RCPP package. Finally tracing it down, if I do
devtools::load_all("PKG_NAME") it goes

  • devtools::load_all("PKG_NAME")
  • pkgbuild::compile_dll(path, quiet = quiet) - here
  • compile_dll which has debug=T as the unchanged default here
  • which is passed to compiler_flags(debug)
  • which sets -O0 in the compile vars so nothing is inlined or optimized

So it uses a lot of the packages within r-lib. Currently there's no way of overriding this because compile_dll doesn't allow passing a changed debug onwards. Arguably this could be changed in pkgbuild's default because if anyone is going to the trouble of using RCPP, they probably want all optimizations. Personally, I'm just loading a fork of pkgbuild which always sets Debug=F. But -O0 is so debugger friendly, it leads to very strange performance penalties.

EDIT: Wow, yeah -O3 is so much faster than -O0 and I needed to play so many fewer tricks to make it happen

Development builds are not optimised by default so that you get full backtraces and full browsing in debuggers. You can set -O3 in your personal Makevars file.

JZL commented

Ok great, thanks

bnprks commented

I have found that setting -O3 or -O2 in my ~/.R/Makevars file is insufficient because the compiler_flags settings overwrite the CFLAGS and CXXFLAGS causing -O0 to still get used.

For others in the same situation, here is an ugly workaround that overrides the pkgbuild::compiler_flags function in the pkgbuild namespace to allow manually setting the desired flags. Only for use during personal development, of course, but it's helpful when debugging code that runs very slowly with -O0

# Overwrite devtools::load_all() default to use -O2 optimization level rather than -O0
compiler_flags <- function(debug=FALSE) {
    c(
        CFLAGS = "-UNDEBUG -Wall -pedantic -g -O2",
        CXXFLAGS = "-UNDEBUG -Wall -pedantic -g -O2",
        CXX11FLAGS = "-UNDEBUG -Wall -pedantic -g -O2",
        CXX14FLAGS = "-UNDEBUG -Wall -pedantic -g -O2",
        CXX17FLAGS = "-UNDEBUG -Wall -pedantic -g -O2",
        CXX20FLAGS = "-UNDEBUG -Wall -pedantic -g -O2",
        FFLAGS = "-g -O2",
        FCFLAGS = "-g -O2"
    )
}
assignInNamespace("compiler_flags", compiler_flags, "pkgbuild")