[rkward-cvs] SF.net SVN: rkward:[3476] trunk/rkward

tfry at users.sourceforge.net tfry at users.sourceforge.net
Sun Mar 13 16:38:51 UTC 2011


Revision: 3476
          http://rkward.svn.sourceforge.net/rkward/?rev=3476&view=rev
Author:   tfry
Date:     2011-03-13 16:38:51 +0000 (Sun, 13 Mar 2011)

Log Message:
-----------
Make retrieval of function arguments more efficient (around 40% faster analysis of package:base). Also support formals or primitives.

Modified Paths:
--------------
    trunk/rkward/ChangeLog
    trunk/rkward/rkward/rbackend/rkstructuregetter.cpp
    trunk/rkward/rkward/rbackend/rkstructuregetter.h
    trunk/rkward/rkward/rbackend/rpackages/rkward/R/internal.R

Modified: trunk/rkward/ChangeLog
===================================================================
--- trunk/rkward/ChangeLog	2011-03-12 18:30:49 UTC (rev 3475)
+++ trunk/rkward/ChangeLog	2011-03-13 16:38:51 UTC (rev 3476)
@@ -1,3 +1,4 @@
+- Support function argument hinting for R primitives
 - Support S4 slots in object name completion and workspace browser		TODO: more testing
 - More correct handling of quotes in object name completion
 - Support plot history for ggplot2 plots

Modified: trunk/rkward/rkward/rbackend/rkstructuregetter.cpp
===================================================================
--- trunk/rkward/rkward/rbackend/rkstructuregetter.cpp	2011-03-12 18:30:49 UTC (rev 3475)
+++ trunk/rkward/rkward/rbackend/rkstructuregetter.cpp	2011-03-13 16:38:51 UTC (rev 3476)
@@ -19,6 +19,7 @@
 
 #include "rdata.h"
 #include "rkrsupport.h"
+#include "rkrbackendprotocol_shared.h"
 #include "../core/robject.h"
 
 #include "../debug.h"
@@ -51,9 +52,8 @@
 	dims_fun = prefetch_fun ("dim");
 	names_fun = prefetch_fun ("names");
 	length_fun = prefetch_fun ("length");
+	args_fun = prefetch_fun ("args");
 	rk_get_slots_fun = prefetch_fun (".rk.get.slots", false);
-
-	get_formals_fun = prefetch_fun (".rk.get.formals", false);
 }
 
 RKStructureGetter::~RKStructureGetter () {
@@ -400,12 +400,23 @@
 		childdata->setData (children);
 		res[RObject::StoragePositionChildren] = childdata;
 	} else if (is_function) {
-// TODO: get_formals_fun is still the major bottleneck, but no idea, how to improve on this
-		SEXP formals_s = RKRSupport::callSimpleFun (get_formals_fun, value, R_GlobalEnv);
+// TODO: getting the formals is still a bit of a bottleneck, but no idea, how to improve on this, any further
+		SEXP formals_s;
+		if (Rf_isPrimitive (value)) formals_s = FORMALS (RKRSupport::callSimpleFun (args_fun, value, R_BaseEnv));	// primitives don't have formals, internally
+		else formals_s = FORMALS (value);
 		PROTECT (formals_s);
-		// the default values
+
+		// get the default values
+		QStringList formals = RKRSupport::SEXPToStringList (formals_s);
+		// for the most part, the implicit as.character in SEXPToStringList does a good on the formals (and it's the fastest of many options that I have tried).
+		// Only for naked strings (as in 'function (a="something")'), we're missing the quotes. So we add quotes, after conversion, as needed:
+		SEXP dummy = formals_s;
+		for (int i = 0; i < Rf_length (dummy); ++i) {
+			if (TYPEOF (CAR (dummy)) == STRSXP) formals[i] = RKRSharedFunctionality::quote (formals[i]);
+			dummy = CDR (dummy);
+		}
 		RData *funargvaluesdata = new RData;
-		funargvaluesdata->setData (RKRSupport::SEXPToStringList (formals_s));
+		funargvaluesdata->setData (formals);
 
 		// the argument names
 		SEXP names_s = Rf_getAttrib (formals_s, R_NamesSymbol);

Modified: trunk/rkward/rkward/rbackend/rkstructuregetter.h
===================================================================
--- trunk/rkward/rkward/rbackend/rkstructuregetter.h	2011-03-12 18:30:49 UTC (rev 3475)
+++ trunk/rkward/rkward/rbackend/rkstructuregetter.h	2011-03-13 16:38:51 UTC (rev 3476)
@@ -67,7 +67,7 @@
 	SEXP is_character_fun;
 	SEXP is_logical_fun;
 	SEXP names_fun;
-	SEXP get_formals_fun;
+	SEXP args_fun;
 	SEXP double_brackets_fun;
 	SEXP length_fun;
 	SEXP rk_get_slots_fun;

Modified: trunk/rkward/rkward/rbackend/rpackages/rkward/R/internal.R
===================================================================
--- trunk/rkward/rkward/rbackend/rpackages/rkward/R/internal.R	2011-03-12 18:30:49 UTC (rev 3475)
+++ trunk/rkward/rkward/rbackend/rpackages/rkward/R/internal.R	2011-03-13 16:38:51 UTC (rev 3476)
@@ -261,16 +261,6 @@
 	.Call ("rk.get.structure.global", as.character (name), as.integer (envlevel), namespacename)
 }
 
-".rk.get.formals" <- function (x) 
-{
-    f <- formals (x)
-    r <- as.character(lapply(f, function(v) {
-        if (is.character(v)) return(encodeString(v, quote = "\"")) else return(v)
-    }))
-    names (r) <- names (f)
-    r
-}
-
 ".rk.get.slots" <- function (x) {
 	slotnames <- methods::slotNames (class (x))
 	ret <- lapply (slotnames, function (slotname) slot (x, slotname))


This was sent by the SourceForge.net collaborative development platform, the world's largest Open Source development site.




More information about the rkward-tracker mailing list