[Rprotobuf-commits] r477 - pkg/R

noreply at r-forge.r-project.org noreply at r-forge.r-project.org
Tue Aug 21 20:09:09 CEST 2012


Author: murray
Date: 2012-08-21 20:09:08 +0200 (Tue, 21 Aug 2012)
New Revision: 477

Modified:
   pkg/R/wrapper_FieldDescriptor.R
Log:
Improve the efficiency of the type/cpptype/label string lookup
functions when the as.string parameter is set by only computing this
list once at package load time instead of during each method
invocation.  Suggested by Karl during review.



Modified: pkg/R/wrapper_FieldDescriptor.R
===================================================================
--- pkg/R/wrapper_FieldDescriptor.R	2012-08-18 21:41:41 UTC (rev 476)
+++ pkg/R/wrapper_FieldDescriptor.R	2012-08-21 18:09:08 UTC (rev 477)
@@ -32,13 +32,14 @@
 TYPE_SINT32   <- 17L
 TYPE_SINT64   <- 18L
 
+.TYPES <- sapply(ls( pattern="^TYPE_" ), function(x) get(x))
+
 setGeneric( "type", function(object, as.string = FALSE){
 	standardGeneric( "type" )
 } )
 setMethod( "type", "FieldDescriptor", function(object, as.string = FALSE){
 	type <- .Call( "FieldDescriptor__type", object at pointer, PACKAGE = "RProtoBuf" )
 	if( as.string ) {
-                .TYPES <- sapply(ls( "package:RProtoBuf", pattern="^TYPE_" ), get)
                 names(which(.TYPES == type))
         } else {
                 type
@@ -57,13 +58,13 @@
 CPPTYPE_STRING <- 9L
 CPPTYPE_MESSAGE <- 10L
 
+.CPPTYPES <- sapply(ls( pattern="^CPPTYPE_" ), function(x) get(x))
 setGeneric( "cpp_type", function(object, as.string = FALSE ){
 	standardGeneric( "cpp_type" )
 } )
 setMethod( "cpp_type", "FieldDescriptor", function(object, as.string = FALSE){
 	cpptype <- .Call( "FieldDescriptor__cpp_type", object at pointer, PACKAGE = "RProtoBuf" )
 	if( as.string ) {
-                .CPPTYPES <- sapply(ls( "package:RProtoBuf", pattern="^CPPTYPE_" ), get)
                 names(which(.CPPTYPES == cpptype))
         } else {
                 cpptype
@@ -73,6 +74,7 @@
 LABEL_OPTIONAL <- 1L
 LABEL_REQUIRED <- 2L
 LABEL_REPEATED <- 3L
+.LABELS <- sapply(ls( pattern="^LABEL_" ), function(x) get(x))
 
 setGeneric( "label", function(object, as.string = FALSE ){
 	standardGeneric( "label" )
@@ -80,7 +82,6 @@
 setMethod( "label", "FieldDescriptor", function(object, as.string = FALSE){
 	lab <- .Call( "FieldDescriptor__label", object at pointer, PACKAGE = "RProtoBuf" )
 	if( as.string ) {
-                .LABELS <- sapply(ls( "package:RProtoBuf", pattern="^LABEL_" ), get)
                 names(which(.LABELS == lab))
         } else {
                 lab



More information about the Rprotobuf-commits mailing list