From eef1c40ceda737d0c2505dd1c652a4985823bf48 Mon Sep 17 00:00:00 2001 From: Dirk Eddelbuettel Date: Sat, 25 Apr 2020 00:09:39 -0500 Subject: [PATCH 1/2] allocate two temp objects as simple vectors (closes #1074) --- ChangeLog | 5 +++++ inst/NEWS.Rd | 5 +++++ inst/include/Rcpp/sugar/functions/sample.h | 4 ++-- 3 files changed, 12 insertions(+), 2 deletions(-) diff --git a/ChangeLog b/ChangeLog index e373a96a4..571e17073 100644 --- a/ChangeLog +++ b/ChangeLog @@ -1,3 +1,8 @@ +2020-04-25 Dirk Eddelbuettel + + * inst/include/Rcpp/sugar/functions/sample.h: Replace R_alloc() with + a standard vector allocation to reduce memory consumption in sample + 2020-04-16 Dirk Eddelbuettel * DESCRIPTION (Version, Date): Roll minor version diff --git a/inst/NEWS.Rd b/inst/NEWS.Rd index 3c99425c5..9604643cf 100644 --- a/inst/NEWS.Rd +++ b/inst/NEWS.Rd @@ -38,6 +38,11 @@ \item The exceptions test is now partially skipped on Solaris as it already is on Windows (Dirk in \ghpr{1065}). } + \item Changes in Rcpp Sugar: + \itemize{ + \item Two \code{sample()} objects are now standard vectors and not + \code{R_alloc} created (Dirk in \ghpr{1075} fixing \ghpr{1074}). + } } } diff --git a/inst/include/Rcpp/sugar/functions/sample.h b/inst/include/Rcpp/sugar/functions/sample.h index 9f4817b1c..9a66254d4 100644 --- a/inst/include/Rcpp/sugar/functions/sample.h +++ b/inst/include/Rcpp/sugar/functions/sample.h @@ -348,7 +348,7 @@ inline Vector EmpiricalSample(int n, int size, bool replace, bool one_ba return ans; } - int* x = reinterpret_cast(R_alloc(n, sizeof(int))); + IntegerVector x(n); for (int i = 0; i < n; i++) { x[i] = i; } @@ -378,7 +378,7 @@ inline Vector EmpiricalSample(int size, bool replace, const Vector return ans; } - int* x = reinterpret_cast(R_alloc(n, sizeof(int))); + IntegerVector x(n); for (int i = 0; i < n; i++) { x[i] = i; } From 24f8d0575777bf15fa770db8c4608af98e6755d1 Mon Sep 17 00:00:00 2001 From: Dirk Eddelbuettel Date: Sat, 25 Apr 2020 00:14:15 -0500 Subject: [PATCH 2/2] initialize via no_init(n) --- inst/include/Rcpp/sugar/functions/sample.h | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/inst/include/Rcpp/sugar/functions/sample.h b/inst/include/Rcpp/sugar/functions/sample.h index 9a66254d4..50bd63813 100644 --- a/inst/include/Rcpp/sugar/functions/sample.h +++ b/inst/include/Rcpp/sugar/functions/sample.h @@ -348,7 +348,7 @@ inline Vector EmpiricalSample(int n, int size, bool replace, bool one_ba return ans; } - IntegerVector x(n); + IntegerVector x = no_init(n); for (int i = 0; i < n; i++) { x[i] = i; } @@ -378,7 +378,7 @@ inline Vector EmpiricalSample(int size, bool replace, const Vector return ans; } - IntegerVector x(n); + IntegerVector x = no_init(n); for (int i = 0; i < n; i++) { x[i] = i; }