From 3d37b44881ecae5f486cd8c575ac80836125b7d2 Mon Sep 17 00:00:00 2001 From: Davis Vaughan Date: Fri, 26 Jul 2024 10:56:26 -0400 Subject: [PATCH] More benchmark tweaks --- cpp11test/R/cpp11.R | 4 ++++ cpp11test/bench/truncate.R | 13 ++++++++----- cpp11test/src/cpp11.cpp | 8 ++++++++ cpp11test/src/sum_rcpp.cpp | 12 ++++++++++++ cpp11test/src/truncate.cpp | 10 +++------- 5 files changed, 35 insertions(+), 12 deletions(-) diff --git a/cpp11test/R/cpp11.R b/cpp11test/R/cpp11.R index ce43aae4..bb3153e3 100644 --- a/cpp11test/R/cpp11.R +++ b/cpp11test/R/cpp11.R @@ -224,6 +224,10 @@ rcpp_grow_ <- function(n_sxp) { .Call(`_cpp11test_rcpp_grow_`, n_sxp) } +rcpp_push_and_truncate_ <- function(size_sxp) { + .Call(`_cpp11test_rcpp_push_and_truncate_`, size_sxp) +} + test_destruction_inner <- function() { invisible(.Call(`_cpp11test_test_destruction_inner`)) } diff --git a/cpp11test/bench/truncate.R b/cpp11test/bench/truncate.R index f4a3f77c..a074e430 100644 --- a/cpp11test/bench/truncate.R +++ b/cpp11test/bench/truncate.R @@ -3,15 +3,18 @@ pkgload::load_all("cpp11test") bench::press(len = as.integer(10 ^ (0:6)), { bench::mark( - cpp11_push_and_truncate_(len), - min_iterations = 100 + cpp11 = cpp11_push_and_truncate_(len), + rcpp = rcpp_push_and_truncate_(len), + check = FALSE, + min_iterations = 1000 ) } -) +)[c("expression", "len", "min", "mem_alloc", "n_itr", "n_gc")] # Longer benchmark, lots of gc len <- as.integer(10 ^ 7) bench::mark( - cpp11_push_and_truncate_(len), + cpp11 = cpp11_push_and_truncate_(len), + rcpp = rcpp_push_and_truncate_(len), min_iterations = 200 -) +)[c("expression", "min", "mem_alloc", "n_itr", "n_gc")] diff --git a/cpp11test/src/cpp11.cpp b/cpp11test/src/cpp11.cpp index 5e22f827..7cfa36c3 100644 --- a/cpp11test/src/cpp11.cpp +++ b/cpp11test/src/cpp11.cpp @@ -422,6 +422,13 @@ extern "C" SEXP _cpp11test_rcpp_grow_(SEXP n_sxp) { return cpp11::as_sexp(rcpp_grow_(cpp11::as_cpp>(n_sxp))); END_CPP11 } +// sum_rcpp.cpp +SEXP rcpp_push_and_truncate_(SEXP size_sxp); +extern "C" SEXP _cpp11test_rcpp_push_and_truncate_(SEXP size_sxp) { + BEGIN_CPP11 + return cpp11::as_sexp(rcpp_push_and_truncate_(cpp11::as_cpp>(size_sxp))); + END_CPP11 +} // test-protect-nested.cpp void test_destruction_inner(); extern "C" SEXP _cpp11test_test_destruction_inner() { @@ -489,6 +496,7 @@ static const R_CallMethodDef CallEntries[] = { {"_cpp11test_protect_one_preserve_", (DL_FUNC) &_cpp11test_protect_one_preserve_, 2}, {"_cpp11test_protect_one_sexp_", (DL_FUNC) &_cpp11test_protect_one_sexp_, 2}, {"_cpp11test_rcpp_grow_", (DL_FUNC) &_cpp11test_rcpp_grow_, 1}, + {"_cpp11test_rcpp_push_and_truncate_", (DL_FUNC) &_cpp11test_rcpp_push_and_truncate_, 1}, {"_cpp11test_rcpp_release_", (DL_FUNC) &_cpp11test_rcpp_release_, 1}, {"_cpp11test_rcpp_sum_dbl_accumulate_", (DL_FUNC) &_cpp11test_rcpp_sum_dbl_accumulate_, 1}, {"_cpp11test_rcpp_sum_dbl_for_", (DL_FUNC) &_cpp11test_rcpp_sum_dbl_for_, 1}, diff --git a/cpp11test/src/sum_rcpp.cpp b/cpp11test/src/sum_rcpp.cpp index 47a5c0e9..035edef7 100644 --- a/cpp11test/src/sum_rcpp.cpp +++ b/cpp11test/src/sum_rcpp.cpp @@ -56,3 +56,15 @@ return x; } + +[[cpp11::register]] SEXP rcpp_push_and_truncate_(SEXP size_sxp) { + R_xlen_t size = INTEGER(size_sxp)[0]; + + // Allocate `size` worth of doubles (filled with garbage data) + Rcpp::NumericVector out(size); + + // Push 1 more past the existing capacity + out.push_back(0); + + return out; +} diff --git a/cpp11test/src/truncate.cpp b/cpp11test/src/truncate.cpp index fca095ce..0e065f1c 100644 --- a/cpp11test/src/truncate.cpp +++ b/cpp11test/src/truncate.cpp @@ -3,15 +3,11 @@ [[cpp11::register]] SEXP cpp11_push_and_truncate_(SEXP size_sexp) { R_xlen_t size = INTEGER(size_sexp)[0]; + // Allocate `size` worth of doubles (filled with garbage data) cpp11::writable::doubles out(size); - // Fill it - for (R_xlen_t i = 0; i < size; ++i) { - out.push_back(0); - } - - // Push 1 more past the existing capacity, - // doubling the capacity + // Push 1 more past the existing length/capacity, + // doubling the capacity for cpp11 vectors out.push_back(0); // Truncate back to `size + 1` size and return result.