[Rcpp-devel] [Rcpp-commits] r374 - in pkg: inst src src/Rcpp

noreply at r-forge.r-project.org noreply at r-forge.r-project.org
Fri Jan 15 09:32:41 CET 2010


Author: romain
Date: 2010-01-15 09:32:40 +0100 (Fri, 15 Jan 2010)
New Revision: 374

Modified:
   pkg/inst/ChangeLog
   pkg/src/ExpressionVector.cpp
   pkg/src/NumericVector.cpp
   pkg/src/RObject.cpp
   pkg/src/Rcpp/NumericVector.h
   pkg/src/Rcpp/RObject.h
Log:
caching in NumericVector to improve dramatically performance of operator[]

Modified: pkg/inst/ChangeLog
===================================================================
--- pkg/inst/ChangeLog	2010-01-14 21:00:22 UTC (rev 373)
+++ pkg/inst/ChangeLog	2010-01-15 08:32:40 UTC (rev 374)
@@ -1,3 +1,9 @@
+2010-01-15  Romain Francois <francoisromain at free.fr>
+
+	* src/Rcpp/NumericVector.h: cache the start of the array to
+	improve performance of operator[]
+	* src/NumericVector.cpp: idem
+
 2010-01-13  Romain Francois <francoisromain at free.fr>
 
 	* R/cpp.package.skeleton.R: new function cpp.package.skeleton

Modified: pkg/src/ExpressionVector.cpp
===================================================================
--- pkg/src/ExpressionVector.cpp	2010-01-14 21:00:22 UTC (rev 373)
+++ pkg/src/ExpressionVector.cpp	2010-01-15 08:32:40 UTC (rev 374)
@@ -37,7 +37,7 @@
 				{
 					SEXP res = R_NilValue ;
 					try{
-						SEXP res = Evaluator::run( Rf_lang2( Rf_install("as.expression"), x ) ) ;
+						res = Evaluator::run( Rf_lang2( Rf_install("as.expression"), x ) ) ;
 					} catch( const Evaluator::eval_error& e){
 						throw not_compatible( "could not convert to an expression vector" ) ;
 					}

Modified: pkg/src/NumericVector.cpp
===================================================================
--- pkg/src/NumericVector.cpp	2010-01-14 21:00:22 UTC (rev 373)
+++ pkg/src/NumericVector.cpp	2010-01-15 08:32:40 UTC (rev 374)
@@ -25,36 +25,41 @@
 
 namespace Rcpp{
 	
-	NumericVector::NumericVector(SEXP x) throw(not_compatible) : VectorBase() {
+	NumericVector::NumericVector(SEXP x) throw(not_compatible) : VectorBase(), start(0) {
 		switch( TYPEOF( x ) ){
 			case REALSXP:
 				setSEXP( x ) ;
+				update() ;
 				break ;
 			case INTSXP:
 			case LGLSXP:
 			case RAWSXP:
 				setSEXP( Rf_coerceVector( x, REALSXP) ) ;
+				update() ;
 				break ;
 			default:
 				throw not_compatible( "cannot convert to numeric vector" ) ;
 		}
 	}
 	
-	NumericVector::NumericVector(int size) : VectorBase() {
+	NumericVector::NumericVector(int size) : VectorBase(), start(0) {
 		setSEXP( Rf_allocVector(REALSXP, size) ) ;
+		start = REAL(m_sexp) ;
 	}
 
 #ifdef HAS_INIT_LISTS	
-NumericVector::NumericVector( std::initializer_list<int> list ) : VectorBase() {
+NumericVector::NumericVector( std::initializer_list<int> list ) : VectorBase(), start(0) {
 		SEXP x = PROTECT( Rf_allocVector( REALSXP, list.size() ) ) ;
 		std::copy( list.begin(), list.end(), REAL(x) ); 
-		setSEXP(x) ;
+		setSEXP(x);
+		update() ;
 		UNPROTECT( 1 ); /* x */
 	}
-	NumericVector::NumericVector( std::initializer_list<double> list ) : VectorBase() {
+	NumericVector::NumericVector( std::initializer_list<double> list ) : VectorBase(), start(0) {
 		SEXP x = PROTECT( Rf_allocVector( REALSXP, list.size() ) ) ;
 		std::copy( list.begin(), list.end(), REAL(x) ); 
-		setSEXP(x) ;
+		setSEXP(x);
+		update() ;
 		UNPROTECT( 1 ); /* x */
 	}
 #endif

Modified: pkg/src/RObject.cpp
===================================================================
--- pkg/src/RObject.cpp	2010-01-14 21:00:22 UTC (rev 373)
+++ pkg/src/RObject.cpp	2010-01-15 08:32:40 UTC (rev 374)
@@ -143,6 +143,5 @@
 	return "array or list out of bounds" ;
 }
 
-
 } // namespace Rcpp
 

Modified: pkg/src/Rcpp/NumericVector.h
===================================================================
--- pkg/src/Rcpp/NumericVector.h	2010-01-14 21:00:22 UTC (rev 373)
+++ pkg/src/Rcpp/NumericVector.h	2010-01-15 08:32:40 UTC (rev 374)
@@ -44,14 +44,21 @@
 	NumericVector( std::initializer_list<double> list ) ;
 #endif
 
-	inline double& operator[]( const int& i ) { return REAL(m_sexp)[i]; }
-	inline const double& operator[]( const int& i ) const { return REAL(m_sexp)[i]; }
-
-	inline double* begin() const { return REAL(m_sexp) ; } 
-	inline double* end() const   { return REAL(m_sexp)+LENGTH(m_sexp);}
-
+	// inline double& operator[]( const int& i ) { return REAL(m_sexp)[i]; }
+	// inline const double& operator[]( const int& i ) const { return REAL(m_sexp)[i]; }
+	// inline double* begin() const { return REAL(m_sexp) ; } 
+	// inline double* end() const   { return REAL(m_sexp)+LENGTH(m_sexp);}
+	
+	inline double& operator[]( const int& i ) { return start[i] ; }
+	inline const double& operator[]( const int& i ) const { return start[i]; }
+	inline double* begin() const { return start ; } 
+	inline double* end() const   { return start+LENGTH(m_sexp);}
+	
 	typedef double* iterator ;
 
+private:
+	double *start ;
+	inline void update(){ start = REAL(m_sexp);}
 } ;
 
 } // namespace

Modified: pkg/src/Rcpp/RObject.h
===================================================================
--- pkg/src/Rcpp/RObject.h	2010-01-14 21:00:22 UTC (rev 373)
+++ pkg/src/Rcpp/RObject.h	2010-01-15 08:32:40 UTC (rev 374)
@@ -223,7 +223,7 @@
      * to change it, use setSEXP
      */
     SEXP m_sexp ;
-
+    
 private:
 
     void preserve(){ if( m_sexp != R_NilValue ) R_PreserveObject(m_sexp) ; } 

_______________________________________________
Rcpp-commits mailing list
Rcpp-commits at lists.r-forge.r-project.org
https://lists.r-forge.r-project.org/cgi-bin/mailman/listinfo/rcpp-commits


More information about the Rcpp-devel mailing list