I have noticed many cases where the contents of a loop in Rcpp (or pure C) can be 'easily' unswitched, but even with -O3 optimization there is a significant performance advantage to manually unswitching. Take the following simple example, where I see 12% difference when unswitching.
Is this something peculiar to Rcpp or R, or am I making an assumption about my code or compiler optimization that is incorrect?
#include <Rcpp.h>
using namespace Rcpp;
// [[Rcpp::export]]
int which_first_equal_A(IntegerVector x, int a, bool not_equal = false) {
int n = x.length();
for (int i = 0; i < n; ++i) {
if (not_equal) {
if (x[i] != a) {
return i + 1;
}
} else {
if (x[i] == a) {
return i + 1;
}
}
}
return 0;
}
// [[Rcpp::export]]
int which_first_equal_B(IntegerVector x, int a, bool not_equal = false) {
int n = x.length();
if (not_equal) {
for (int i = 0; i < n; ++i) {
if (x[i] != a) {
return i + 1;
}
}
} else {
for (int i = 0; i < n; ++i) {
if (x[i] == a) {
return i + 1;
}
}
}
return 0;
}
/***R
x <- integer(1e9)
bench::mark(which_first_equal_A(x, 1L),
which_first_equal_B(x, 1L))
*/
/.R/Makevars
PKG_CXXFLAGS = -O3 -funswitch-loops
PKG_LIBS = -O3 -funswitch-loops
Rcpp::sourceCpp('~/rollers.cpp', verbose = TRUE, rebuild = TRUE)
#>
#> Generated extern "C" functions
#> --------------------------------------------------------
#>
#>
#> #include <Rcpp.h>
#> // which_first_equal_A
#> int which_first_equal_A(IntegerVector x, int a, bool not_equal);
#> RcppExport SEXP sourceCpp_1_which_first_equal_A(SEXP xSEXP, SEXP aSEXP, SEXP not_equalSEXP) {
#> BEGIN_RCPP
#> Rcpp::RObject rcpp_result_gen;
#> Rcpp::RNGScope rcpp_rngScope_gen;
#> Rcpp::traits::input_parameter< IntegerVector >::type x(xSEXP);
#> Rcpp::traits::input_parameter< int >::type a(aSEXP);
#> Rcpp::traits::input_parameter< bool >::type not_equal(not_equalSEXP);
#> rcpp_result_gen = Rcpp::wrap(which_first_equal_A(x, a, not_equal));
#> return rcpp_result_gen;
#> END_RCPP
#> }
#> // which_first_equal_B
#> int which_first_equal_B(IntegerVector x, int a, bool not_equal);
#> RcppExport SEXP sourceCpp_1_which_first_equal_B(SEXP xSEXP, SEXP aSEXP, SEXP not_equalSEXP) {
#> BEGIN_RCPP
#> Rcpp::RObject rcpp_result_gen;
#> Rcpp::RNGScope rcpp_rngScope_gen;
#> Rcpp::traits::input_parameter< IntegerVector >::type x(xSEXP);
#> Rcpp::traits::input_parameter< int >::type a(aSEXP);
#> Rcpp::traits::input_parameter< bool >::type not_equal(not_equalSEXP);
#> rcpp_result_gen = Rcpp::wrap(which_first_equal_B(x, a, not_equal));
#> return rcpp_result_gen;
#> END_RCPP
#> }
#>
#> Generated R functions
#> -------------------------------------------------------
#>
#> `.sourceCpp_1_DLLInfo` <- dyn.load('C:/Users/hughp/AppData/Local/Temp/RtmpGYnUKa/sourceCpp-x86_64-w64-mingw32-1.0.5/sourcecpp_538057654375/sourceCpp_2.dll')
#`>
#> which_first_equal_A <- Rcpp:::sourceCppFunction(function(x, a, not_equal = FALSE) {}, FALSE, `.sourceCpp_1_DLLInfo`, 'sourceCpp_1_which_first_equal_A')
#> which_first_equal_B <- Rcpp:::sourceCppFunction(function(x, a, not_equal = FALSE) {}, FALSE, `.sourceCpp_1_DLLInfo`, 'sourceCpp_1_which_first_equal_B')
#>
#> rm(`.sourceCpp_1_DLLInfo`)
#>
#> Building shared library
#> --------------------------------------------------------
#>
#> DIR: C:/Users/hughp/AppData/Local/Temp/RtmpGYnUKa/sourceCpp-x86_64-w64-mingw32-1.0.5/sourcecpp_538057654375
#>
#> C:/R/R-40~1.0/bin/x64/R CMD SHLIB --preclean -o "sourceCpp_2.dll" "rollers.cpp"
#> "C:/rtools40/mingw64/bin/"g++ -std=gnu++11 -I"C:/R/R-40~1.0/include" -DNDEBUG -I"C:/R/R-4.0.0/library/Rcpp/include" -I"C:/Users/hughp/Documents" -I"C:/Users/hughp/inst/include" -O3 -funswitch-loops -O2 -Wall -mfpmath=sse -msse2 -mstackrealign -c rollers.cpp -o rollers.o
#> C:/rtools40/mingw64/bin/g++ -std=gnu++11 -shared -s -static-libgcc -o sourceCpp_6.dll tmp.def rollers.o -O3 -funswitch-loops -LC:/R/R-40~1.0/bin/x64 -lR
#>
#> > x <- integer(1e9)
#>
#> > bench::mark(which_first_equal_A(x, 1L),
#> + which_first_equal_B(x, 1L))
#> # A tibble: 2 x 14
#> expression min mean median max `itr/sec` mem_alloc n_gc
#> <chr> <bch:tm> <bch:tm> <bch:tm> <bch:tm> <dbl> <bch:byt> <dbl>
#> 1 which_fir~ 576.724ms 576.724ms 576.724ms 576.724ms 1.73 2.492KB 0
#> 2 which_fir~ 504.358ms 504.358ms 504.358ms 504.358ms 1.98 2.492KB 0
#> # ... with 6 more variables: n_itr <int>, total_time <bch:tm>, result <list>,
#> # memory <list>, time <list>, gc <list>
Created on 2020-11-28 by the reprex package (v0.3.0)