Commit d4929aa9 authored by Radford Neal's avatar Radford Neal

First version released on github

parent 9ea54f65
...@@ -151,7 +151,11 @@ users, such as cleanups of source code. ...@@ -151,7 +151,11 @@ users, such as cleanups of source code.
21) 21)
22) o PROTECT, UNPROTECT, etc. were made mostly macros in most of the files
in src/main. This applies only to files that include Defn.h
after defining the symbol USE_FAST_PROTECT_MACROS.
With these macros, code of the form v = PROTECT(...) must be replaced
by PROTECT(v = ...). This change speeds up numerous operations.
23) 23)
......
PROTECT, UNPROTECT, etc. have been made mostly macros in most of the
files in src/main. This applies only to files that include Defn.h
after defining the symbol USE_FAST_PROTECT_MACROS. If this is
defined, macros PROTECT2 and PROTECT3 for protecting two or three
objects at once are also defined.
This change speeds up numerous operations.
...@@ -1304,6 +1304,52 @@ extern void *alloca(size_t); ...@@ -1304,6 +1304,52 @@ extern void *alloca(size_t);
# endif # endif
#endif #endif
/* Enable this by defining USE_FAST_PROTECT_MACROS before including Defn.h.
Redefines PROTECT, UNPROTECT, PROTECT_WITH_INDEX, and REPROTECT for speed,
as is possible because the required variables are defined above. The
macros below call procedure in memory.c for error handling. PROTECT_PTR is
not redefined, since it contains a significant amount of code.
Defining USE_FAST_PROTECT_MACROS in source files outside src/main may
cause problems at link time. */
#ifdef USE_FAST_PROTECT_MACROS
extern SEXP Rf_protect_error (void); /* SEXP only so it will work with "?" */
extern void Rf_unprotect_error (void);
#undef PROTECT
#define PROTECT(s) \
( R_PPStackTop < R_PPStackSize ? R_PPStack[R_PPStackTop++] = (s) \
: Rf_protect_error() )
#undef PROTECT2
#define PROTECT2(s1,s2) \
( R_PPStackTop+1 < R_PPStackSize ? (R_PPStack[R_PPStackTop++] = (s1), \
R_PPStack[R_PPStackTop++] = (s2)) \
: Rf_protect_error() )
#undef PROTECT3
#define PROTECT3(s1,s2,s3) \
( R_PPStackTop+2 < R_PPStackSize ? (R_PPStack[R_PPStackTop++] = (s1), \
R_PPStack[R_PPStackTop++] = (s2), \
R_PPStack[R_PPStackTop++] = (s3)) \
: Rf_protect_error() )
#undef UNPROTECT
#define UNPROTECT(n) \
( R_PPStackTop >= (n) ? (void) (R_PPStackTop -= (n)) \
: Rf_unprotect_error() )
#undef PROTECT_WITH_INDEX
#define PROTECT_WITH_INDEX(x,i) \
( (*(i) = R_PPStackTop), PROTECT(x) )
#undef REPROTECT
#define REPROTECT(x,i) \
( (void) (R_PPStack[i] = x) )
#endif
#endif /* DEFN_H_ */ #endif /* DEFN_H_ */
/* /*
*- Local Variables: *- Local Variables:
......
...@@ -506,8 +506,10 @@ void (SET_HASHVALUE)(SEXP x, int v); ...@@ -506,8 +506,10 @@ void (SET_HASHVALUE)(SEXP x, int v);
#define isByteCode(x) (TYPEOF(x)==BCODESXP) #define isByteCode(x) (TYPEOF(x)==BCODESXP)
/* Pointer Protection and Unprotection */ /* Pointer Protection and Unprotection */
#define PROTECT(s) Rf_protect(s) #define PROTECT(s) Rf_protect(s)
#define UNPROTECT(n) Rf_unprotect(n) #define PROTECT2(s1,s2) Rf_protect2(s) /* BEWARE! All args evaluated */
#define PROTECT3(s1,s2,s3) Rf_protect3(s) /* before any are protected */
#define UNPROTECT(n) Rf_unprotect(n)
#define UNPROTECT_PTR(s) Rf_unprotect_ptr(s) #define UNPROTECT_PTR(s) Rf_unprotect_ptr(s)
/* We sometimes need to coerce a protected value and place the new /* We sometimes need to coerce a protected value and place the new
...@@ -661,6 +663,8 @@ Rboolean Rf_pmatch(SEXP, SEXP, Rboolean); ...@@ -661,6 +663,8 @@ Rboolean Rf_pmatch(SEXP, SEXP, Rboolean);
Rboolean Rf_psmatch(const char *, const char *, Rboolean); Rboolean Rf_psmatch(const char *, const char *, Rboolean);
void Rf_PrintValue(SEXP); void Rf_PrintValue(SEXP);
SEXP Rf_protect(SEXP); SEXP Rf_protect(SEXP);
void Rf_protect2(SEXP, SEXP);
void Rf_protect3(SEXP, SEXP, SEXP);
SEXP Rf_setAttrib(SEXP, SEXP, SEXP); SEXP Rf_setAttrib(SEXP, SEXP, SEXP);
void Rf_setSVector(SEXP*, int, SEXP); void Rf_setSVector(SEXP*, int, SEXP);
void Rf_set_elements_to_NA_or_NULL(SEXP, int, int); void Rf_set_elements_to_NA_or_NULL(SEXP, int, int);
...@@ -676,7 +680,7 @@ SEXP Rf_type2str(SEXPTYPE); ...@@ -676,7 +680,7 @@ SEXP Rf_type2str(SEXPTYPE);
void Rf_unprotect(int); void Rf_unprotect(int);
void Rf_unprotect_ptr(SEXP); void Rf_unprotect_ptr(SEXP);
void R_ProtectWithIndex(SEXP, PROTECT_INDEX *); SEXP R_ProtectWithIndex(SEXP, PROTECT_INDEX *);
void R_Reprotect(SEXP, PROTECT_INDEX); void R_Reprotect(SEXP, PROTECT_INDEX);
SEXP R_tryEval(SEXP, SEXP, int *); SEXP R_tryEval(SEXP, SEXP, int *);
SEXP R_tryEvalSilent(SEXP, SEXP, int *); SEXP R_tryEvalSilent(SEXP, SEXP, int *);
......
...@@ -29,6 +29,7 @@ ...@@ -29,6 +29,7 @@
#include <config.h> #include <config.h>
#endif #endif
#define USE_FAST_PROTECT_MACROS
#include <Defn.h> #include <Defn.h>
#include <R_ext/RConverters.h> #include <R_ext/RConverters.h>
......
...@@ -29,6 +29,7 @@ ...@@ -29,6 +29,7 @@
#include <string.h> #include <string.h>
#define USE_FAST_PROTECT_MACROS
#include <Defn.h> #include <Defn.h>
#include <R_ext/RStartup.h> #include <R_ext/RStartup.h>
...@@ -80,7 +81,7 @@ do_commandArgs(SEXP call, SEXP op, SEXP args, SEXP env) ...@@ -80,7 +81,7 @@ do_commandArgs(SEXP call, SEXP op, SEXP args, SEXP env)
SEXP vals; SEXP vals;
/* need protection as mkChar allocates */ /* need protection as mkChar allocates */
vals = PROTECT(allocVector(STRSXP, NumCommandLineArgs)); PROTECT(vals = allocVector(STRSXP, NumCommandLineArgs));
for(i = 0; i < NumCommandLineArgs; i++) for(i = 0; i < NumCommandLineArgs; i++)
SET_STRING_ELT(vals, i, mkChar(CommandLineArgs[i])); SET_STRING_ELT(vals, i, mkChar(CommandLineArgs[i]));
UNPROTECT(1); UNPROTECT(1);
......
...@@ -29,6 +29,7 @@ ...@@ -29,6 +29,7 @@
#include <config.h> #include <config.h>
#endif #endif
#define USE_FAST_PROTECT_MACROS
#include "Defn.h" #include "Defn.h"
#include <R_ext/Random.h> #include <R_ext/Random.h>
......
...@@ -90,6 +90,7 @@ ...@@ -90,6 +90,7 @@
#include <config.h> #include <config.h>
#endif #endif
#define USE_FAST_PROTECT_MACROS
#include <Defn.h> #include <Defn.h>
#include <string.h> #include <string.h>
......
...@@ -34,6 +34,7 @@ ...@@ -34,6 +34,7 @@
#endif #endif
#include <stdlib.h> /* for setenv or putenv */ #include <stdlib.h> /* for setenv or putenv */
#define USE_FAST_PROTECT_MACROS
#include <Defn.h> /* for PATH_MAX */ #include <Defn.h> /* for PATH_MAX */
#include <Rinterface.h> #include <Rinterface.h>
#include <Fileio.h> #include <Fileio.h>
......
...@@ -32,6 +32,7 @@ ...@@ -32,6 +32,7 @@
# include <config.h> # include <config.h>
#endif #endif
#define USE_FAST_PROTECT_MACROS
#include <Defn.h> #include <Defn.h>
/* This is remapped */ /* This is remapped */
#undef pmatch #undef pmatch
......
...@@ -27,6 +27,7 @@ ...@@ -27,6 +27,7 @@
#include <config.h> #include <config.h>
#endif #endif
#define USE_FAST_PROTECT_MACROS
#include <Defn.h> #include <Defn.h>
/* .Internal(lapply(X, FUN)) */ /* .Internal(lapply(X, FUN)) */
......
...@@ -33,6 +33,7 @@ ...@@ -33,6 +33,7 @@
/* for definition of "struct exception" in math.h */ /* for definition of "struct exception" in math.h */
# define __LIBM_PRIVATE # define __LIBM_PRIVATE
#endif #endif
#define USE_FAST_PROTECT_MACROS
#include "Defn.h" /*-> Arith.h -> math.h */ #include "Defn.h" /*-> Arith.h -> math.h */
#ifdef __OpenBSD__ #ifdef __OpenBSD__
# undef __LIBM_PRIVATE # undef __LIBM_PRIVATE
......
...@@ -29,6 +29,7 @@ ...@@ -29,6 +29,7 @@
#include <config.h> #include <config.h>
#endif #endif
#define USE_FAST_PROTECT_MACROS
#include <Defn.h> #include <Defn.h>
#include <Rmath.h> #include <Rmath.h>
#include <R_ext/RS.h> /* for Calloc/Free */ #include <R_ext/RS.h> /* for Calloc/Free */
......
...@@ -28,6 +28,7 @@ ...@@ -28,6 +28,7 @@
#include <config.h> #include <config.h>
#endif #endif
#define USE_FAST_PROTECT_MACROS
#include <Defn.h> #include <Defn.h>
#include <Rmath.h> #include <Rmath.h>
...@@ -1532,7 +1533,7 @@ SEXP R_do_slot(SEXP obj, SEXP name) { ...@@ -1532,7 +1533,7 @@ SEXP R_do_slot(SEXP obj, SEXP name) {
TYPEOF(obj) == VECSXP) /* needed for namedList class */ TYPEOF(obj) == VECSXP) /* needed for namedList class */
return value; return value;
if(isSymbol(name) ) { if(isSymbol(name) ) {
input = PROTECT(ScalarString(PRINTNAME(name))); PROTECT(input = ScalarString(PRINTNAME(name)));
classString = getAttrib(obj, R_ClassSymbol); classString = getAttrib(obj, R_ClassSymbol);
if(isNull(classString)) { if(isNull(classString)) {
UNPROTECT(1); UNPROTECT(1);
......
...@@ -31,6 +31,7 @@ ...@@ -31,6 +31,7 @@
#include <config.h> #include <config.h>
#endif #endif
#define USE_FAST_PROTECT_MACROS
#include <Defn.h> #include <Defn.h>
#include <Graphics.h> #include <Graphics.h>
#include <Colors.h> #include <Colors.h>
......
...@@ -31,6 +31,7 @@ ...@@ -31,6 +31,7 @@
# include <config.h> # include <config.h>
#endif #endif
#define USE_FAST_PROTECT_MACROS
#include <Defn.h> #include <Defn.h>
#define imax2(x, y) ((x < y) ? y : x) #define imax2(x, y) ((x < y) ? y : x)
......
...@@ -29,6 +29,7 @@ ...@@ -29,6 +29,7 @@
#include <config.h> #include <config.h>
#endif #endif
#define USE_FAST_PROTECT_MACROS
#define R_USE_SIGNALS 1 #define R_USE_SIGNALS 1
#include <Defn.h> #include <Defn.h>
#include <Print.h> #include <Print.h>
......
...@@ -74,6 +74,7 @@ abbreviate chartr make.names strtrim tolower toupper give error. ...@@ -74,6 +74,7 @@ abbreviate chartr make.names strtrim tolower toupper give error.
# include <config.h> # include <config.h>
#endif #endif
#define USE_FAST_PROTECT_MACROS
#include <Defn.h> #include <Defn.h>
#include <errno.h> #include <errno.h>
......
...@@ -29,6 +29,7 @@ ...@@ -29,6 +29,7 @@
#include <config.h> #include <config.h>
#endif #endif
#define USE_FAST_PROTECT_MACROS
#include <Defn.h> /*-- Maybe modularize into own Coerce.h ..*/ #include <Defn.h> /*-- Maybe modularize into own Coerce.h ..*/
#include <float.h> /* for DBL_DIG */ #include <float.h> /* for DBL_DIG */
#define R_MSG_mode _("invalid 'mode' argument") #define R_MSG_mode _("invalid 'mode' argument")
......
...@@ -30,6 +30,7 @@ ...@@ -30,6 +30,7 @@
#include <config.h> #include <config.h>
#endif #endif
#define USE_FAST_PROTECT_MACROS
#include <Defn.h> #include <Defn.h>
#include <Graphics.h> /* for dpptr */ #include <Graphics.h> /* for dpptr */
#include <Colors.h> #include <Colors.h>
......
...@@ -63,6 +63,7 @@ ...@@ -63,6 +63,7 @@
#undef HAVE_CPOW #undef HAVE_CPOW
#endif #endif
#define USE_FAST_PROTECT_MACROS
#include <Defn.h> /* -> ../include/R_ext/Complex.h */ #include <Defn.h> /* -> ../include/R_ext/Complex.h */
#include <Rmath.h> #include <Rmath.h>
#include <R_ext/Applic.h> /* R_cpoly */ #include <R_ext/Applic.h> /* R_cpoly */
......
...@@ -79,6 +79,9 @@ ...@@ -79,6 +79,9 @@
# include <config.h> # include <config.h>
#endif #endif
/* Don't enable this, since: (1) many instances, but not time critical,
and (2) PROTECT is used as a function here */
/* #define USE_FAST_PROTECT_MACROS */
#define R_USE_SIGNALS 1 #define R_USE_SIGNALS 1
#include <Defn.h> #include <Defn.h>
#include <Fileio.h> #include <Fileio.h>
......
...@@ -113,6 +113,7 @@ ...@@ -113,6 +113,7 @@
#include <config.h> #include <config.h>
#endif #endif
#define USE_FAST_PROTECT_MACROS
#define R_USE_SIGNALS 1 #define R_USE_SIGNALS 1
#include <Defn.h> #include <Defn.h>
......
...@@ -28,6 +28,7 @@ ...@@ -28,6 +28,7 @@
#include <config.h> #include <config.h>
#endif #endif
#define USE_FAST_PROTECT_MACROS
#include <Defn.h> #include <Defn.h>
#include <Rmath.h> #include <Rmath.h>
......
...@@ -28,6 +28,7 @@ ...@@ -28,6 +28,7 @@
#include <config.h> #include <config.h>
#endif #endif
#define USE_FAST_PROTECT_MACROS
#include "Defn.h" #include "Defn.h"
static SEXP cumsum(SEXP x, SEXP s) static SEXP cumsum(SEXP x, SEXP s)
......
...@@ -116,6 +116,7 @@ extern time_t mktime (struct tm*); ...@@ -116,6 +116,7 @@ extern time_t mktime (struct tm*);
#endif #endif
#include <stdlib.h> /* for setenv or putenv */ #include <stdlib.h> /* for setenv or putenv */
#define USE_FAST_PROTECT_MACROS
#include <Defn.h> #include <Defn.h>
/* The glibc in RH8.0 was broken and assumed that dates before /* The glibc in RH8.0 was broken and assumed that dates before
......
...@@ -27,6 +27,7 @@ ...@@ -27,6 +27,7 @@
# include <config.h> # include <config.h>
#endif #endif
#define USE_FAST_PROTECT_MACROS
#define R_USE_SIGNALS 1 #define R_USE_SIGNALS 1
#include <Defn.h> #include <Defn.h>
#include <Rconnections.h> #include <Rconnections.h>
......
...@@ -28,6 +28,7 @@ ...@@ -28,6 +28,7 @@
#include <config.h> #include <config.h>
#endif #endif
#define USE_FAST_PROTECT_MACROS
#define R_USE_SIGNALS 1 #define R_USE_SIGNALS 1
#include <Defn.h> #include <Defn.h>
......
...@@ -95,6 +95,7 @@ ...@@ -95,6 +95,7 @@
#include <config.h> #include <config.h>
#endif #endif
#define USE_FAST_PROTECT_MACROS
#define R_USE_SIGNALS 1 #define R_USE_SIGNALS 1
#include <Defn.h> #include <Defn.h>
#include <float.h> /* for DBL_DIG */ #include <float.h> /* for DBL_DIG */
......
...@@ -32,6 +32,8 @@ ...@@ -32,6 +32,8 @@
#include <config.h> #include <config.h>
#endif #endif
/* Don't enable this, since many instances, but probably not time critical */
/* #define USE_FAST_PROTECT_MACROS */
#include "Defn.h" #include "Defn.h"
static SEXP ParenSymbol; static SEXP ParenSymbol;
......
...@@ -37,6 +37,7 @@ ...@@ -37,6 +37,7 @@
#include <config.h> #include <config.h>
#endif #endif
#define USE_FAST_PROTECT_MACROS
#include <Defn.h> #include <Defn.h>
#include <Graphics.h> #include <Graphics.h>
#include <GraphicsBase.h> /* registerBase */ #include <GraphicsBase.h> /* registerBase */
......
...@@ -29,6 +29,7 @@ ...@@ -29,6 +29,7 @@
# include <config.h> # include <config.h>
#endif #endif
#define USE_FAST_PROTECT_MACROS
#define R_USE_SIGNALS 1 #define R_USE_SIGNALS 1
#include <Defn.h> #include <Defn.h>
#include <ctype.h> /* for tolower */ #include <ctype.h> /* for tolower */
......
...@@ -31,6 +31,7 @@ ...@@ -31,6 +31,7 @@
#define HAVE_BZIP2 #define HAVE_BZIP2
#define USE_FAST_PROTECT_MACROS
#include <Defn.h> #include <Defn.h>
#include <Fileio.h> /* for R_fopen */ #include <Fileio.h> /* for R_fopen */
#include "unzip.h" #include "unzip.h"
......
...@@ -29,6 +29,7 @@ ...@@ -29,6 +29,7 @@
#include <config.h> #include <config.h>
#endif #endif
#define USE_FAST_PROTECT_MACROS
#include "Defn.h" #include "Defn.h"
......
...@@ -29,6 +29,7 @@ ...@@ -29,6 +29,7 @@
#include <config.h> #include <config.h>
#endif #endif
#define USE_FAST_PROTECT_MACROS
#include "Defn.h" #include "Defn.h"
#include <R_ext/RS.h> /* S4 bit */ #include <R_ext/RS.h> /* S4 bit */
......
...@@ -27,6 +27,7 @@ ...@@ -27,6 +27,7 @@
#include <config.h> #include <config.h>
#endif #endif
#define USE_FAST_PROTECT_MACROS
#include <Defn.h> #include <Defn.h>
#include <R_ext/GraphicsEngine.h> #include <R_ext/GraphicsEngine.h>
#include <R_ext/Applic.h> /* pretty0() */ #include <R_ext/Applic.h> /* pretty0() */
......
...@@ -92,6 +92,7 @@ ...@@ -92,6 +92,7 @@
# include <config.h> # include <config.h>
#endif #endif
#define USE_FAST_PROTECT_MACROS
#define R_USE_SIGNALS 1 #define R_USE_SIGNALS 1
#include "Defn.h" #include "Defn.h"
#include <R_ext/Callbacks.h> #include <R_ext/Callbacks.h>
......
...@@ -27,6 +27,7 @@ ...@@ -27,6 +27,7 @@
#include <config.h> #include <config.h>
#endif #endif
#define USE_FAST_PROTECT_MACROS
#define R_USE_SIGNALS 1 #define R_USE_SIGNALS 1
#include <Defn.h> #include <Defn.h>
/* -> Errormsg.h */ /* -> Errormsg.h */
......
...@@ -31,6 +31,7 @@ ...@@ -31,6 +31,7 @@
# include <config.h> # include <config.h>
#endif #endif
#define USE_FAST_PROTECT_MACROS
#define R_USE_SIGNALS 1 #define R_USE_SIGNALS 1
#include <Defn.h> #include <Defn.h>
#include <Rinterface.h> #include <Rinterface.h>
......
...@@ -45,6 +45,7 @@ ...@@ -45,6 +45,7 @@
#include <config.h> #include <config.h>
#endif #endif
#define USE_FAST_PROTECT_MACROS
#include <Defn.h> #include <Defn.h>
#include <float.h> /* for DBL_EPSILON */ #include <float.h> /* for DBL_EPSILON */
#include <Rmath.h> #include <Rmath.h>
......
...@@ -31,6 +31,7 @@ ...@@ -31,6 +31,7 @@
#include <config.h> #include <config.h>
#endif #endif
#define USE_FAST_PROTECT_MACROS
#include "Defn.h" #include "Defn.h"
#include <R_ext/Applic.h> #include <R_ext/Applic.h>
......
...@@ -31,6 +31,7 @@ ...@@ -31,6 +31,7 @@
#include <config.h> #include <config.h>
#endif #endif
#define USE_FAST_PROTECT_MACROS
#include <Defn.h> #include <Defn.h>
#include <Rmath.h> #include <Rmath.h>
#include <R_ext/GraphicsEngine.h> #include <R_ext/GraphicsEngine.h>
......
...@@ -35,6 +35,7 @@ ...@@ -35,6 +35,7 @@
#include <config.h> #include <config.h>
#endif #endif
#define USE_FAST_PROTECT_MACROS
#include "Defn.h" #include "Defn.h"
int R_fgetc(FILE *fp) int R_fgetc(FILE *fp)
......
...@@ -102,6 +102,8 @@ ...@@ -102,6 +102,8 @@
#include <config.h> #include <config.h>
#endif #endif
/* Don't enable this, since many instances, but not time critical */
/* #define USE_FAST_PROTECT_MACROS */
#include "IOStuff.h" /*-> Defn.h */ #include "IOStuff.h" /*-> Defn.h */
#include "Fileio.h" #include "Fileio.h"
#include "Parse.h" #include "Parse.h"
......
...@@ -23,6 +23,8 @@ ...@@ -23,6 +23,8 @@
#include <config.h> #include <config.h>
#endif #endif
/* Don't enable this, since many instances, but not time critical */
/* #define USE_FAST_PROTECT_MACROS */
#include "IOStuff.h" /*-> Defn.h */ #include "IOStuff.h" /*-> Defn.h */
#include "Fileio.h" #include "Fileio.h"
#include "Parse.h" #include "Parse.h"
......
...@@ -99,6 +99,8 @@ ...@@ -99,6 +99,8 @@
#include <config.h> #include <config.h>
#endif #endif
/* Don't enable this, since many instances, but not time critical */
/* #define USE_FAST_PROTECT_MACROS */
#define R_USE_SIGNALS 1 #define R_USE_SIGNALS 1
#include <Defn.h> #include <Defn.h>
#include <Parse.h> #include <Parse.h>
......
...@@ -23,6 +23,8 @@ ...@@ -23,6 +23,8 @@
#include <config.h> #include <config.h>
#endif #endif