Skip to content

[Rcpp-devel] sugar performance

3 messages · Romain Francois, Gabor Grothendieck, Douglas Bates

#
Hello, 

Following Gabor's thread yesterday, I looked into R source code and figure that we don't need to test for NA when using numeric vector. See this: 

require(inline)
require(Rcpp)

pl <- cxxfunction( , '
    return List::create( 
        NA_REAL + NA_REAL, 
        NA_REAL + 1.0,
        1.0 + NA_REAL 
        ); 
', plugin = "Rcpp" )
pl()

As a consequence, I've updated plus.h to skip the tests when dealing with numeric sugar expressions. 


Also, I'm trying to write the looping internally in order to get a better comparison. With the attached file, I get: 

romain@ /tmp $ Rscript gab.R
Le chargement a n?cessit? le package : inline
Le chargement a n?cessit? le package : methods
Le chargement a n?cessit? le package : Rcpp
Le chargement a n?cessit? le package : rbenchmark
   test replications elapsed relative user.self sys.self user.child sys.child
1 sugar            1   0.002      1.0     0.001        0          0         0
2     R            1   0.005      2.5     0.004        0          0         0
   test replications elapsed relative user.self sys.self user.child sys.child
1 sugar            1   0.035      1.0     0.034    0.000          0         0
2     R            1   0.049      1.4     0.049    0.001          0         0
   test replications elapsed relative user.self sys.self user.child sys.child
1 sugar            1   4.699 1.000000     3.701    0.999          0         0
2     R            1   5.324 1.133007     4.267    1.056          0         0
   test replications elapsed relative user.self sys.self user.child sys.child
2     R            1  45.995 1.000000    39.076    6.911          0         0
1 sugar            1  61.249 1.331645    46.178   15.067          0         0


Another contributor to the time difference is the handling of garbage collection. Each time the constructor of an Rcpp class is called, a call to R_PreserveObject is issued. And each time the destructor is called, R_ReleaseObject is called. In this R-devel thread last year: 
http://thread.gmane.org/gmane.comp.lang.r.devel/23167 , some inefficiency of these functions were revealed, with a patch from Simon, but this has not been incorporated into R yet. 

Maybe we can send a reminder to sponsor the patch or use something else in Rcpp instead. 


The benchmark shows that sugar beats R first, and then R gets better as the size of the vectors grow. I think this is related to loop unrolling. it seems that it gives good performance for smaller lengths and not as good for bigger lengths. 


Romain
#
On Wed, Dec 22, 2010 at 6:33 AM, <romain at r-enthusiasts.com> wrote:
The attachment does not seem to have made it through.
#
On Wed, Dec 22, 2010 at 5:33 AM, <romain at r-enthusiasts.com> wrote:
Technically I think that you supposed to have code that assumes that
numeric NA's propagate inside a check for

#if  IEEE_754

#endif

The number of systems running R that don't follow the IEEE floating
point standard is vanishingly small but either very old systems
(SPARC/Solaris, I believe, possibly AIX on some esoteric hardware) or
newer architectures (some versions of ARM, various graphics chips
using CUDA and similar systems) might not so it is best to test.