Skip to content

Commit a8b2a4f

Browse files
committed
More benchmark tweaks
1 parent 6ac3528 commit a8b2a4f

File tree

5 files changed

+35
-12
lines changed

5 files changed

+35
-12
lines changed

cpp11test/R/cpp11.R

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -224,6 +224,10 @@ rcpp_grow_ <- function(n_sxp) {
224224
.Call(`_cpp11test_rcpp_grow_`, n_sxp)
225225
}
226226

227+
rcpp_push_and_truncate_ <- function(size_sxp) {
228+
.Call(`_cpp11test_rcpp_push_and_truncate_`, size_sxp)
229+
}
230+
227231
test_destruction_inner <- function() {
228232
invisible(.Call(`_cpp11test_test_destruction_inner`))
229233
}

cpp11test/bench/truncate.R

Lines changed: 8 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -3,15 +3,18 @@ pkgload::load_all("cpp11test")
33
bench::press(len = as.integer(10 ^ (0:6)),
44
{
55
bench::mark(
6-
cpp11_push_and_truncate_(len),
7-
min_iterations = 100
6+
cpp11 = cpp11_push_and_truncate_(len),
7+
rcpp = rcpp_push_and_truncate_(len),
8+
check = FALSE,
9+
min_iterations = 1000
810
)
911
}
10-
)
12+
)[c("expression", "len", "min", "mem_alloc", "n_itr", "n_gc")]
1113

1214
# Longer benchmark, lots of gc
1315
len <- as.integer(10 ^ 7)
1416
bench::mark(
15-
cpp11_push_and_truncate_(len),
17+
cpp11 = cpp11_push_and_truncate_(len),
18+
rcpp = rcpp_push_and_truncate_(len),
1619
min_iterations = 200
17-
)
20+
)[c("expression", "min", "mem_alloc", "n_itr", "n_gc")]

cpp11test/src/cpp11.cpp

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -422,6 +422,13 @@ extern "C" SEXP _cpp11test_rcpp_grow_(SEXP n_sxp) {
422422
return cpp11::as_sexp(rcpp_grow_(cpp11::as_cpp<cpp11::decay_t<SEXP>>(n_sxp)));
423423
END_CPP11
424424
}
425+
// sum_rcpp.cpp
426+
SEXP rcpp_push_and_truncate_(SEXP size_sxp);
427+
extern "C" SEXP _cpp11test_rcpp_push_and_truncate_(SEXP size_sxp) {
428+
BEGIN_CPP11
429+
return cpp11::as_sexp(rcpp_push_and_truncate_(cpp11::as_cpp<cpp11::decay_t<SEXP>>(size_sxp)));
430+
END_CPP11
431+
}
425432
// test-protect-nested.cpp
426433
void test_destruction_inner();
427434
extern "C" SEXP _cpp11test_test_destruction_inner() {
@@ -489,6 +496,7 @@ static const R_CallMethodDef CallEntries[] = {
489496
{"_cpp11test_protect_one_preserve_", (DL_FUNC) &_cpp11test_protect_one_preserve_, 2},
490497
{"_cpp11test_protect_one_sexp_", (DL_FUNC) &_cpp11test_protect_one_sexp_, 2},
491498
{"_cpp11test_rcpp_grow_", (DL_FUNC) &_cpp11test_rcpp_grow_, 1},
499+
{"_cpp11test_rcpp_push_and_truncate_", (DL_FUNC) &_cpp11test_rcpp_push_and_truncate_, 1},
492500
{"_cpp11test_rcpp_release_", (DL_FUNC) &_cpp11test_rcpp_release_, 1},
493501
{"_cpp11test_rcpp_sum_dbl_accumulate_", (DL_FUNC) &_cpp11test_rcpp_sum_dbl_accumulate_, 1},
494502
{"_cpp11test_rcpp_sum_dbl_for_", (DL_FUNC) &_cpp11test_rcpp_sum_dbl_for_, 1},

cpp11test/src/sum_rcpp.cpp

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -56,3 +56,15 @@
5656

5757
return x;
5858
}
59+
60+
[[cpp11::register]] SEXP rcpp_push_and_truncate_(SEXP size_sxp) {
61+
R_xlen_t size = INTEGER(size_sxp)[0];
62+
63+
// Allocate `size` worth of doubles (filled with garbage data)
64+
Rcpp::NumericVector out(size);
65+
66+
// Push 1 more past the existing capacity
67+
out.push_back(0);
68+
69+
return out;
70+
}

cpp11test/src/truncate.cpp

Lines changed: 3 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -3,15 +3,11 @@
33
[[cpp11::register]] SEXP cpp11_push_and_truncate_(SEXP size_sexp) {
44
R_xlen_t size = INTEGER(size_sexp)[0];
55

6+
// Allocate `size` worth of doubles (filled with garbage data)
67
cpp11::writable::doubles out(size);
78

8-
// Fill it
9-
for (R_xlen_t i = 0; i < size; ++i) {
10-
out.push_back(0);
11-
}
12-
13-
// Push 1 more past the existing capacity,
14-
// doubling the capacity
9+
// Push 1 more past the existing length/capacity,
10+
// doubling the capacity for cpp11 vectors
1511
out.push_back(0);
1612

1713
// Truncate back to `size + 1` size and return result.

0 commit comments

Comments
 (0)