[Rcpp-commits] r2160 - in pkg/Rcpp/inst: . examples/ConvolveBenchmarks include/Rcpp/sugar include/Rcpp/sugar/operators include/Rcpp/vector

noreply at r-forge.r-project.org noreply at r-forge.r-project.org
Fri Sep 24 13:06:12 CEST 2010


Author: romain
Date: 2010-09-24 13:06:12 +0200 (Fri, 24 Sep 2010)
New Revision: 2160

Modified:
   pkg/Rcpp/inst/ChangeLog
   pkg/Rcpp/inst/examples/ConvolveBenchmarks/exampleRCode.r
   pkg/Rcpp/inst/include/Rcpp/sugar/Range.h
   pkg/Rcpp/inst/include/Rcpp/sugar/operators/times.h
   pkg/Rcpp/inst/include/Rcpp/vector/RangeIndexer.h
   pkg/Rcpp/inst/include/Rcpp/vector/VectorBase.h
Log:
speed improvements for sugar operator*

Modified: pkg/Rcpp/inst/ChangeLog
===================================================================
--- pkg/Rcpp/inst/ChangeLog	2010-09-24 09:52:34 UTC (rev 2159)
+++ pkg/Rcpp/inst/ChangeLog	2010-09-24 11:06:12 UTC (rev 2160)
@@ -2,8 +2,11 @@
 
     * inst/include/Rcpp/sugar/Range.h : Range gains some operators (++,--,n etc ...)
     
-    * inst/examples/ConvolveBenchmarks/convolve3_cpp.cpp: uisng the new Range
+    * inst/examples/ConvolveBenchmarks/convolve3_cpp.cpp: using the new Range
     operators
+    
+    * inst/include/Rcpp/sugar/operators/times.h: speed improvements. Not using 
+    pointer to member functions seems to be beneficial.
 
 2010-09-22  Romain Francois <romain at r-enthusiasts.com>
 

Modified: pkg/Rcpp/inst/examples/ConvolveBenchmarks/exampleRCode.r
===================================================================
--- pkg/Rcpp/inst/examples/ConvolveBenchmarks/exampleRCode.r	2010-09-24 09:52:34 UTC (rev 2159)
+++ pkg/Rcpp/inst/examples/ConvolveBenchmarks/exampleRCode.r	2010-09-24 11:06:12 UTC (rev 2160)
@@ -47,7 +47,7 @@
 
 ## load benchmarkin helper function
 suppressMessages(library(rbenchmark))
-REPS <- 20000L
+REPS <- 10000L
 bm <- benchmark(R_API_optimised(REPS,a,b),
                 R_API_naive(REPS,a,b),
                 Rcpp_Classic(REPS,a,b),

Modified: pkg/Rcpp/inst/include/Rcpp/sugar/Range.h
===================================================================
--- pkg/Rcpp/inst/include/Rcpp/sugar/Range.h	2010-09-24 09:52:34 UTC (rev 2159)
+++ pkg/Rcpp/inst/include/Rcpp/sugar/Range.h	2010-09-24 11:06:12 UTC (rev 2160)
@@ -75,6 +75,8 @@
 		    return Range( start - n, end_ - n ) ;
 		}
 		
+		inline int get_start() const { return start ; }
+		inline int get_end() const { return end_ ; }
 		
 	private:
 		int start ;

Modified: pkg/Rcpp/inst/include/Rcpp/sugar/operators/times.h
===================================================================
--- pkg/Rcpp/inst/include/Rcpp/sugar/operators/times.h	2010-09-24 09:52:34 UTC (rev 2159)
+++ pkg/Rcpp/inst/include/Rcpp/sugar/operators/times.h	2010-09-24 11:06:12 UTC (rev 2160)
@@ -33,7 +33,7 @@
 		typedef typename Rcpp::VectorBase<RTYPE,RHS_NA,RHS_T> RHS_TYPE ;
 		
 		Times_Vector_Vector( const LHS_TYPE& lhs_, const RHS_TYPE& rhs_ ) : 
-			lhs(lhs_), rhs(rhs_){}
+			lhs(lhs_.get_ref()), rhs(rhs_.get_ref()){}
 		
 		inline STORAGE operator[]( int i ) const {
 			STORAGE lhs_ = lhs[i] ;
@@ -45,8 +45,8 @@
 		inline int size() const { return lhs.size() ; }
 		
 	private:
-		const LHS_TYPE& lhs ;
-		const RHS_TYPE& rhs ;
+		const LHS_T& lhs ;
+		const RHS_T& rhs ;
 	} ;	
 	
 	// specialization LHS_NA = false
@@ -58,7 +58,7 @@
 		typedef typename Rcpp::VectorBase<RTYPE,RHS_NA,RHS_T> RHS_TYPE ;
 		
 		Times_Vector_Vector( const LHS_TYPE& lhs_, const RHS_TYPE& rhs_ ) : 
-			lhs(lhs_), rhs(rhs_){}
+			lhs(lhs_.get_ref()), rhs(rhs_.get_ref()){}
 		
 		inline STORAGE operator[]( int i ) const {
 			STORAGE rhs_ = rhs[i] ;
@@ -69,8 +69,8 @@
 		inline int size() const { return lhs.size() ; }
 		
 	private:
-		const LHS_TYPE& lhs ;
-		const RHS_TYPE& rhs ;
+		const LHS_T& lhs ;
+		const RHS_T& rhs ;
 	} ;	
 
 	// specialization for RHS_NA = false 
@@ -82,7 +82,7 @@
 		typedef typename Rcpp::VectorBase<RTYPE,false,RHS_T> RHS_TYPE ;
 		
 		Times_Vector_Vector( const LHS_TYPE& lhs_, const RHS_TYPE& rhs_ ) : 
-			lhs(lhs_), rhs(rhs_){}
+			lhs(lhs_.get_ref()), rhs(rhs_.get_ref()){}
 		
 		inline STORAGE operator[]( int i ) const {
 			STORAGE lhs_ = lhs[i] ;
@@ -93,8 +93,8 @@
 		inline int size() const { return lhs.size() ; }
 		
 	private:
-		const LHS_TYPE& lhs ;
-		const RHS_TYPE& rhs ;
+		const LHS_T& lhs ;
+		const RHS_T& rhs ;
 	} ;	
 
 
@@ -107,7 +107,7 @@
 		typedef typename Rcpp::VectorBase<RTYPE,false,RHS_T> RHS_TYPE ;
 		
 		Times_Vector_Vector( const LHS_TYPE& lhs_, const RHS_TYPE& rhs_ ) : 
-			lhs(lhs_), rhs(rhs_){}
+			lhs(lhs_.get_ref()), rhs(rhs_.get_ref()){}
 		
 		inline STORAGE operator[]( int i ) const {
 			return lhs[i] * rhs[i]  ;
@@ -116,8 +116,8 @@
 		inline int size() const { return lhs.size() ; }
 		
 	private:
-		const LHS_TYPE& lhs ;
-		const RHS_TYPE& rhs ;
+		const LHS_T& lhs ;
+		const RHS_T& rhs ;
 	} ;	
 	
 	
@@ -127,33 +127,24 @@
 	public:
 		typedef typename Rcpp::VectorBase<RTYPE,NA,T> VEC_TYPE ;
 		typedef typename traits::storage_type<RTYPE>::type STORAGE ;
-		typedef STORAGE (Times_Vector_Primitive::*METHOD)(int) const ;
 		
 		Times_Vector_Primitive( const VEC_TYPE& lhs_, STORAGE rhs_ ) : 
-			lhs(lhs_), rhs(rhs_), m() {
-			
-			m = Rcpp::traits::is_na<RTYPE>(rhs) ? 
-				&Times_Vector_Primitive::rhs_is_na :
-				&Times_Vector_Primitive::rhs_is_not_na ;
-		}
+			lhs(lhs_.get_ref()), rhs(rhs_), rhs_na( Rcpp::traits::is_na<RTYPE>(rhs_) )
+			{}
 		
 		inline STORAGE operator[]( int i ) const {
-			return (this->*m)(i) ;
+			if( rhs_na ) return rhs ;
+			STORAGE x = lhs[i] ;
+			return Rcpp::traits::is_na<RTYPE>(x) ? x : (x * rhs) ;
 		}
 		
 		inline int size() const { return lhs.size() ; }
 		
 	private:
-		const VEC_TYPE& lhs ;
+		const T& lhs ;
 		STORAGE rhs ;
-		METHOD m ;
+		bool rhs_na ;
 		
-		inline STORAGE rhs_is_na(int i) const { return rhs ; }
-		inline STORAGE rhs_is_not_na(int i) const { 
-			STORAGE x = lhs[i] ;
-			return Rcpp::traits::is_na<RTYPE>(x) ? x : (x * rhs) ;
-		}
-		
 	} ;
 	
 
@@ -162,31 +153,21 @@
 	public:
 		typedef typename Rcpp::VectorBase<RTYPE,false,T> VEC_TYPE ;
 		typedef typename traits::storage_type<RTYPE>::type STORAGE ;
-		typedef STORAGE (Times_Vector_Primitive::*METHOD)(int) const ;
 		
 		Times_Vector_Primitive( const VEC_TYPE& lhs_, STORAGE rhs_ ) : 
-			lhs(lhs_), rhs(rhs_), m() {
-				m = Rcpp::traits::is_na<RTYPE>(rhs) ? 
-				&Times_Vector_Primitive::rhs_is_na :
-				&Times_Vector_Primitive::rhs_is_not_na ;
-		}
+			lhs(lhs_.get_ref()), rhs(rhs_), rhs_na( Rcpp::traits::is_na<RTYPE>(rhs_) ) {}
 		
 		inline STORAGE operator[]( int i ) const {
-			return (this->*m)(i) ;
+			return rhs_na ? rhs : (rhs * lhs[i] ) ;
 		}
 		
 		inline int size() const { return lhs.size() ; }
 		
 	private:
-		const VEC_TYPE& lhs ;
+		const T& lhs ;
 		STORAGE rhs ;
-		METHOD m ;
+		bool rhs_na ;
 		
-		inline STORAGE rhs_is_na(int i) const { return rhs ; }
-		inline STORAGE rhs_is_not_na(int i) const { 
-			return rhs * lhs[i] ;
-		}
-		
 	} ;
 	
 	

Modified: pkg/Rcpp/inst/include/Rcpp/vector/RangeIndexer.h
===================================================================
--- pkg/Rcpp/inst/include/Rcpp/vector/RangeIndexer.h	2010-09-24 09:52:34 UTC (rev 2159)
+++ pkg/Rcpp/inst/include/Rcpp/vector/RangeIndexer.h	2010-09-24 11:06:12 UTC (rev 2160)
@@ -32,66 +32,62 @@
 	
 	// TODO: check if the indexer is valid
 	RangeIndexer( VECTOR& vec_, const Rcpp::Range& range_) : 
-		vec(vec_), range(range_), start(vec_.begin()) {}
+		start(vec_.begin() + range_.get_start() ), size_( range_.size() ) {}
 	
 		// TODO: size exceptions
 	template <bool NA, typename T>	
 	RangeIndexer& operator=( const Rcpp::VectorBase<RTYPE,NA,T>& x){
-		int n = size() ;
-		for( int i=0; i<n; i++){
-			start[ range[i] ] = x[i] ;
+	    const T& input = x.get_ref() ;
+	    for( int i=0; i<size_; i++){
+			start[i] = input[i] ;
 		}
 		return *this ;
 	}
 	
 	template <bool NA, typename T>	
 	RangeIndexer& operator+=( const Rcpp::VectorBase<RTYPE,NA,T>& x){
-		int n = size() ;
-		for( int i=0; i<n; i++){
-			start[ range[i] ] += x[i] ;
+		const T& input = x.get_ref() ;
+	    for( int i=0; i<size_; i++){
+			start[i] += input[i] ;
 		}
 		return *this ;
 	}
 	
 	template <bool NA, typename T>	
 	RangeIndexer& operator*=( const Rcpp::VectorBase<RTYPE,NA,T>& x){
-		int n = size() ;
-		for( int i=0; i<n; i++){
-			start[ range[i] ] *= x[i] ;
+		for( int i=0; i<size_; i++){
+			start[i] *= x[i] ;
 		}
 		return *this ;
 	}
 	
 	template <bool NA, typename T>	
 	RangeIndexer& operator-=( const Rcpp::VectorBase<RTYPE,NA,T>& x){
-		int n = size() ;
-		for( int i=0; i<n; i++){
-			start[ range[i] ] -= x[i] ;
+		for( int i=0; i<size_; i++){
+			start[i] -= x[i] ;
 		}
 		return *this ;
 	}
 	
 	template <bool NA, typename T>	
 	RangeIndexer& operator/=( const Rcpp::VectorBase<RTYPE,NA,T>& x){
-		int n = size() ;
-		for( int i=0; i<n; i++){
-			start[ range[i] ] /= x[i] ;
+		for( int i=0; i<size_; i++){
+			start[i] /= x[i] ;
 		}
 		return *this ;
 	}
 	
 	inline Proxy operator[]( int i ){
-		return start[ range[i] ] ;
+	    return start[i] ;
 	}
 	
 	inline int size(){
-		return range.size() ;
+		return size_ ;
 	}
 	
 private:
-	VECTOR& vec ;
-	const Rcpp::Range& range ;
 	iterator start ;
+	int size_ ;
 } ;
 	
 }

Modified: pkg/Rcpp/inst/include/Rcpp/vector/VectorBase.h
===================================================================
--- pkg/Rcpp/inst/include/Rcpp/vector/VectorBase.h	2010-09-24 09:52:34 UTC (rev 2159)
+++ pkg/Rcpp/inst/include/Rcpp/vector/VectorBase.h	2010-09-24 11:06:12 UTC (rev 2160)
@@ -41,8 +41,8 @@
 		return static_cast<const VECTOR&>(*this) ;
 	}
 
-	inline stored_type operator[]( int i) const { 
-		return static_cast<const VECTOR*>(this)->operator[](i) ;
+	inline stored_type operator[]( int i) const {
+	    return static_cast<const VECTOR*>(this)->operator[](i) ;
 	}
 	
 	inline int size() const { return static_cast<const VECTOR*>(this)->size() ; }
@@ -55,7 +55,7 @@
 		typedef stored_type value_type;
 		typedef std::random_access_iterator_tag iterator_category ;
 
-		iterator( const VectorBase& object_, int index_ ) : object(object_), index(index_){} 
+		iterator( const VectorBase& object_, int index_ ) : object(object_.get_ref()), index(index_){} 
 		iterator( const iterator& other) : object(other.object), index(other.index){};
 		
 		inline iterator& operator++(){
@@ -124,7 +124,7 @@
 	
 			
 	private:
-		const VectorBase& object ;
+		const VECTOR& object ;
 		int index; 
 	} ;
 	



More information about the Rcpp-commits mailing list