Skip to content

Commit

Permalink
More benchmark tweaks
Browse files Browse the repository at this point in the history
  • Loading branch information
DavisVaughan committed Aug 7, 2024
1 parent f69f0f5 commit 3d37b44
Show file tree
Hide file tree
Showing 5 changed files with 35 additions and 12 deletions.
4 changes: 4 additions & 0 deletions cpp11test/R/cpp11.R
Original file line number Diff line number Diff line change
Expand Up @@ -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`))
}
Expand Down
13 changes: 8 additions & 5 deletions cpp11test/bench/truncate.R
Original file line number Diff line number Diff line change
Expand Up @@ -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")]
8 changes: 8 additions & 0 deletions cpp11test/src/cpp11.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -422,6 +422,13 @@ extern "C" SEXP _cpp11test_rcpp_grow_(SEXP n_sxp) {
return cpp11::as_sexp(rcpp_grow_(cpp11::as_cpp<cpp11::decay_t<SEXP>>(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<cpp11::decay_t<SEXP>>(size_sxp)));
END_CPP11
}
// test-protect-nested.cpp
void test_destruction_inner();
extern "C" SEXP _cpp11test_test_destruction_inner() {
Expand Down Expand Up @@ -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},
Expand Down
12 changes: 12 additions & 0 deletions cpp11test/src/sum_rcpp.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -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;
}
10 changes: 3 additions & 7 deletions cpp11test/src/truncate.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -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.
Expand Down

0 comments on commit 3d37b44

Please sign in to comment.