[Rcpp-commits] r1865 - in pkg/Rcpp: . inst inst/include/Rcpp/vector

noreply at r-forge.r-project.org noreply at r-forge.r-project.org
Sat Jul 17 10:02:49 CEST 2010


Author: romain
Date: 2010-07-17 10:02:48 +0200 (Sat, 17 Jul 2010)
New Revision: 1865

Modified:
   pkg/Rcpp/DESCRIPTION
   pkg/Rcpp/NEWS
   pkg/Rcpp/inst/ChangeLog
   pkg/Rcpp/inst/include/Rcpp/vector/Vector.h
Log:
faster Vector::names

Modified: pkg/Rcpp/DESCRIPTION
===================================================================
--- pkg/Rcpp/DESCRIPTION	2010-07-17 01:00:43 UTC (rev 1864)
+++ pkg/Rcpp/DESCRIPTION	2010-07-17 08:02:48 UTC (rev 1865)
@@ -1,6 +1,6 @@
 Package: Rcpp
 Title: Seamless R and C++ Integration
-Version: 0.8.4.1
+Version: 0.8.4.2
 Date: $Date$
 Author: Dirk Eddelbuettel and Romain Francois, with contributions 
  by Simon Urbanek, David Reiss and Douglas Bates; based on code written during 

Modified: pkg/Rcpp/NEWS
===================================================================
--- pkg/Rcpp/NEWS	2010-07-17 01:00:43 UTC (rev 1864)
+++ pkg/Rcpp/NEWS	2010-07-17 08:02:48 UTC (rev 1865)
@@ -1,3 +1,8 @@
+0.8.5   (future)
+
+	o	speed improvements. assigning vector names is faster as it avoids
+		making a callback to R if not necessary. 
+
 0.8.4   2010-07-09
 
     o   new sugar vector functions: rep, rep_len, rep_each, rev, head, tail,

Modified: pkg/Rcpp/inst/ChangeLog
===================================================================
--- pkg/Rcpp/inst/ChangeLog	2010-07-17 01:00:43 UTC (rev 1864)
+++ pkg/Rcpp/inst/ChangeLog	2010-07-17 08:02:48 UTC (rev 1865)
@@ -1,3 +1,8 @@
+2010-07-17  Romain Francois <romain at r-enthusiasts.com>
+
+	* inst/include/vector/Vector.h: faster lhs use of names, only using callback
+	to R if necessary. (discovered while profiling RProtoBuf)
+	
 2010-07-16  Romain Francois <romain at r-enthusiasts.com>
 
 	* man/Rcpp-package.Rd : removed old content and point to the Rcpp-package

Modified: pkg/Rcpp/inst/include/Rcpp/vector/Vector.h
===================================================================
--- pkg/Rcpp/inst/include/Rcpp/vector/Vector.h	2010-07-17 01:00:43 UTC (rev 1864)
+++ pkg/Rcpp/inst/include/Rcpp/vector/Vector.h	2010-07-17 08:02:48 UTC (rev 1865)
@@ -244,17 +244,27 @@
 		}
 		
 		void set(SEXP x) const {
-			SEXP new_vec = PROTECT( internal::try_catch( 
-			Rf_lcons( Rf_install("names<-"), 
-					Rf_cons( parent, Rf_cons( x , R_NilValue) )))) ;
-			/* names<- makes a new vector, so we have to change 
-			   the SEXP of the parent of this proxy, it might be 
-			   worth to work directly with the names attribute instead
-			   of using the names<- R function, but then we need to 
-			   take care of coercion, recycling, etc ... we cannot just 
-			   brutally assign the names attribute */
-			const_cast<Vector&>(parent).setSEXP( new_vec ) ;
-			UNPROTECT(1) ; /* new_vec */
+			
+			/* check if we can use a fast version */
+			if( TYPEOF(x) == STRSXP && parent.size() == Rf_length(x) ){
+				SEXP y = const_cast<Vector&>(parent).asSexp() ; 
+				Rf_setAttrib( y, R_NamesSymbol, x ) ;
+			} else {
+				/* use the slower and more flexible version (callback to R) */
+			
+				SEXP new_vec = PROTECT( internal::try_catch( 
+				Rf_lcons( Rf_install("names<-"), 
+						Rf_cons( parent, Rf_cons( x , R_NilValue) )))) ;
+				/* names<- makes a new vector, so we have to change 
+				   the SEXP of the parent of this proxy, it might be 
+				   worth to work directly with the names attribute instead
+				   of using the names<- R function, but then we need to 
+				   take care of coercion, recycling, etc ... we cannot just 
+				   brutally assign the names attribute */
+				const_cast<Vector&>(parent).setSEXP( new_vec ) ;
+				UNPROTECT(1) ; /* new_vec */
+    			}
+    		
     		}
     		
 	} ;



More information about the Rcpp-commits mailing list