[Rcpp-commits] r2284 - in pkg/Rcpp: inst/include inst/include/Rcpp src

noreply at r-forge.r-project.org noreply at r-forge.r-project.org
Thu Oct 7 13:41:56 CEST 2010


Author: romain
Date: 2010-10-07 13:41:56 +0200 (Thu, 07 Oct 2010)
New Revision: 2284

Added:
   pkg/Rcpp/inst/include/Rcpp/cache.h
   pkg/Rcpp/src/cache.cpp
Modified:
   pkg/Rcpp/inst/include/RcppCommon.h
   pkg/Rcpp/src/Environment.cpp
   pkg/Rcpp/src/Module.cpp
Log:
cache the Rcpp namespace instead of calling getNamespace over and over again

Added: pkg/Rcpp/inst/include/Rcpp/cache.h
===================================================================
--- pkg/Rcpp/inst/include/Rcpp/cache.h	                        (rev 0)
+++ pkg/Rcpp/inst/include/Rcpp/cache.h	2010-10-07 11:41:56 UTC (rev 2284)
@@ -0,0 +1,37 @@
+// -*- mode: C++; c-indent-level: 4; c-basic-offset: 4; tab-width: 4 -*-
+//
+// cache.h: Rcpp R/C++ interface class library -- 
+//
+// Copyright (C) 2009 - 2010 Dirk Eddelbuettel and Romain Francois
+//
+// This file is part of Rcpp.
+//
+// Rcpp is free software: you can redistribute it and/or modify it
+// under the terms of the GNU General Public License as published by
+// the Free Software Foundation, either version 2 of the License, or
+// (at your option) any later version.
+//
+// Rcpp is distributed in the hope that it will be useful, but
+// WITHOUT ANY WARRANTY; without even the implied warranty of
+// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
+// GNU General Public License for more details.
+//
+// You should have received a copy of the GNU General Public License
+// along with Rcpp.  If not, see <http://www.gnu.org/licenses/>.
+
+#ifndef RCPP_CACHE_H
+#define RCPP_CACHE_H
+
+namespace Rcpp {
+namespace internal{
+        
+    SEXP get_Rcpp_namespace() ;
+    
+}    
+}
+
+extern "C" SEXP get_rcpp_cache() ;
+extern "C" SEXP init_Rcpp_cache() ; 
+extern "C" SEXP maybe_init() ;
+
+#endif

Modified: pkg/Rcpp/inst/include/RcppCommon.h
===================================================================
--- pkg/Rcpp/inst/include/RcppCommon.h	2010-10-07 09:20:51 UTC (rev 2283)
+++ pkg/Rcpp/inst/include/RcppCommon.h	2010-10-07 11:41:56 UTC (rev 2284)
@@ -123,7 +123,7 @@
 RcppExport void init_Rcpp_routines(DllInfo*) ;
 
 namespace Rcpp{
-	namespace internal{
+    namespace internal{
 		template <typename T> int rcpp_call_test(T t){
 			return T::r_type::value ;
 		}
@@ -279,5 +279,6 @@
 #include <Rcpp/sugar/sugar_forward.h>
 
 #include <Rcpp/routines.h>
+#include <Rcpp/cache.h>
 
 #endif

Modified: pkg/Rcpp/src/Environment.cpp
===================================================================
--- pkg/Rcpp/src/Environment.cpp	2010-10-07 09:20:51 UTC (rev 2283)
+++ pkg/Rcpp/src/Environment.cpp	2010-10-07 11:41:56 UTC (rev 2284)
@@ -259,7 +259,8 @@
     }
     
     Environment Environment::Rcpp_namespace() throw() {
-    	    return Environment( Rf_eval( Rf_lcons( Rf_install("getNamespace"), Rf_cons( Rf_mkString("Rcpp") , R_NilValue) ), R_GlobalEnv ) ) ;
+    	    // return Environment( Rf_eval( Rf_lcons( Rf_install("getNamespace"), Rf_cons( Rf_mkString("Rcpp") , R_NilValue) ), R_GlobalEnv ) ) ;
+    	    return Rcpp::internal::get_Rcpp_namespace() ;
     }
     
     Environment Environment::new_child(bool hashed) {

Modified: pkg/Rcpp/src/Module.cpp
===================================================================
--- pkg/Rcpp/src/Module.cpp	2010-10-07 09:20:51 UTC (rev 2283)
+++ pkg/Rcpp/src/Module.cpp	2010-10-07 11:41:56 UTC (rev 2284)
@@ -176,16 +176,19 @@
    	return clazz->invoke( met, obj, cargs, nargs ) ;
 }
 
-
-
 namespace Rcpp{
 	static Module* current_scope  ;
 }
 
 Rcpp::Module* getCurrentScope(){ return Rcpp::current_scope ; }
 void setCurrentScope( Rcpp::Module* scope ){ Rcpp::current_scope = scope ; }
-void R_init_Rcpp( DllInfo* info){
+extern "C" void R_init_Rcpp( DllInfo* info){
 	Rcpp::current_scope = 0 ;
+	
+	// init the cache
+	init_Rcpp_cache() ;
+	
+	// init routines
 	init_Rcpp_routines(info) ;
 }
 

Added: pkg/Rcpp/src/cache.cpp
===================================================================
--- pkg/Rcpp/src/cache.cpp	                        (rev 0)
+++ pkg/Rcpp/src/cache.cpp	2010-10-07 11:41:56 UTC (rev 2284)
@@ -0,0 +1,54 @@
+// -*- mode: C++; c-indent-level: 4; c-basic-offset: 4; tab-width: 4 -*-
+//
+// cache.cpp: Rcpp R/C++ interface class library -- Rcpp cache
+//
+// Copyright (C) 2010	Dirk Eddelbuettel and Romain Francois
+//
+// This file is part of Rcpp.
+//
+// Rcpp is free software: you can redistribute it and/or modify it
+// under the terms of the GNU General Public License as published by
+// the Free Software Foundation, either version 2 of the License, or
+// (at your option) any later version.
+//
+// Rcpp is distributed in the hope that it will be useful, but
+// WITHOUT ANY WARRANTY; without even the implied warranty of
+// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
+// GNU General Public License for more details.
+//
+// You should have received a copy of the GNU General Public License
+// along with Rcpp.  If not, see <http://www.gnu.org/licenses/>.
+
+#include <Rcpp.h>
+
+static SEXP Rcpp_cache = R_NilValue ;
+static bool Rcpp_cache_ready = false ;
+                
+namespace Rcpp{
+    namespace internal{   
+    SEXP get_Rcpp_namespace(){ 
+        maybe_init() ;
+        return VECTOR_ELT( Rcpp_cache , 0 ) ;
+    }                         
+    
+}
+}
+
+// only used for debugging
+SEXP get_rcpp_cache() { return Rcpp_cache ; }
+
+SEXP maybe_init() { 
+    if( ! Rcpp_cache_ready )  init_Rcpp_cache() ;
+}
+
+SEXP init_Rcpp_cache(){   
+    Rcpp_cache = PROTECT( Rf_allocVector( VECSXP, 10 ) );
+	
+    // the Rcpp namespace
+    SET_VECTOR_ELT( Rcpp_cache, 0,  Rf_eval( Rf_lcons( Rf_install("getNamespace"), Rf_cons( Rf_mkString("Rcpp") , R_NilValue) ), R_GlobalEnv ) ) ;
+	R_PreserveObject( Rcpp_cache ) ;
+	UNPROTECT(1) ;  
+	Rcpp_cache_ready = true ;
+	return Rcpp_cache ;
+}
+



More information about the Rcpp-commits mailing list