Slide 80
Slide 80 text
ALTREP aware
SEXP sum_vec_c3(SEXP x) {
int sum = 0;
int buf[1024];
R_xlen_t i = 0;
R_xlen_t n = Rf_xlength(x);
while(i < n - 1024) {
INTEGER_GET_REGION(x, i, 1024, buf);
for (R_xlen_t j = 0; j < 1024; ++j) {
sum = sum + buf[j];
}
i += 1024;
}
R_xlen_t extra = n - i;
INTEGER_GET_REGION(x, i, extra, buf);
for (R_xlen_t j = 0; j < extra; ++j) {
sum = sum + buf[j];
}
return Rf_ScalarInteger(sum);
}
}
x <- 1:10000
sum_vec_c3(x)
#> [1] 50005000
.Internal(inspect(x))
#> @1342a4be0 13 INTSXP g0c0
#> [REF(65535)] 1 : 10000 (compact)
lobstr::obj_sizes(x)
#> 680 B