Skip to content

Commit 332bfba

Browse files
committed
frollapply(adaptive=TRUE): resizable temporaries
Since adaptive application of rolling functions requires us to resize the argument to match the window size, make sure to allocate it as such.
1 parent 2fa7728 commit 332bfba

File tree

4 files changed

+18
-26
lines changed

4 files changed

+18
-26
lines changed

R/frollapply.R

Lines changed: 6 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -297,38 +297,22 @@ frollapply = function(X, N, FUN, ..., by.column=TRUE, fill=NA, align=c("right","
297297
tight = function(i, dest, src, n) FUN(.Call(CmemcpyDT, dest, src, i, n), ...)
298298
}
299299
} else {
300-
#has.growable = base::getRversion() >= "3.4.0"
301-
## this is now always TRUE
302-
## we keep this branch, it may be useful when getting rid of SET_GROWABLE_BIT and SETLENGTH #6180
303-
has.growable = TRUE
304-
cpy = if (has.growable) function(x) .Call(Csetgrowable, copy(x)) else copy
300+
cpy = function(x) .Call(CcopyAsGrowable, x, by.column)
305301
ansMask = function(len, n) {
306302
mask = seq_len(len) >= n
307303
mask[is.na(mask)] = FALSE ## test 6010.206
308304
mask
309305
}
310306
if (by.column) {
311-
allocWindow = function(x, n) x[seq_len(max(n, na.rm=TRUE))]
312-
if (has.growable) {
313-
tight = function(i, dest, src, n) FUN(.Call(CmemcpyVectoradaptive, dest, src, i, n), ...) # CmemcpyVectoradaptive handles k[i]==0
314-
} else {
315-
tight = function(i, dest, src, n) {stopf("internal error: has.growable should be TRUE, implement support for n==0"); FUN(src[(i-n[i]+1L):i], ...)} # nocov
316-
}
307+
allocWindow = function(x, n) cpy(x[seq_len(max(n, na.rm=TRUE))])
308+
tight = function(i, dest, src, n) FUN(.Call(CmemcpyVectoradaptive, dest, src, i, n), ...) # CmemcpyVectoradaptive handles k[i]==0
317309
} else {
318310
if (!list.df) {
319-
allocWindow = function(x, n) x[seq_len(max(n, na.rm=TRUE)), , drop=FALSE]
320-
} else {
321-
allocWindow = function(x, n) lapply(x, `[`, seq_len(max(n)))
322-
}
323-
if (has.growable) {
324-
tight = function(i, dest, src, n) FUN(.Call(CmemcpyDTadaptive, dest, src, i, n), ...) # CmemcpyDTadaptive handles k[i]==0
311+
allocWindow = function(x, n) cpy(x[seq_len(max(n, na.rm=TRUE)), , drop=FALSE])
325312
} else {
326-
if (!list.df) { # nocov
327-
tight = function(i, dest, src, n) {stopf("internal error: has.growable should be TRUE, implement support for n==0"); FUN(src[(i-n[i]+1L):i, , drop=FALSE], ...)} # nocov
328-
} else {
329-
tight = function(i, dest, src, n) {stopf("internal error: has.growable should be TRUE, implement support for n==0"); FUN(lapply(src, `[`, (i-n[i]+1L):i), ...)} # nocov
330-
}
313+
allocWindow = function(x, n) cpy(lapply(x, `[`, seq_len(max(n))))
331314
}
315+
tight = function(i, dest, src, n) FUN(.Call(CmemcpyDTadaptive, dest, src, i, n), ...) # CmemcpyDTadaptive handles k[i]==0
332316
}
333317
}
334318
## prepare templates for errors and warnings

src/data.table.h

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -301,7 +301,7 @@ SEXP memcpyVector(SEXP dest, SEXP src, SEXP offset, SEXP size);
301301
SEXP memcpyDT(SEXP dest, SEXP src, SEXP offset, SEXP size);
302302
SEXP memcpyVectoradaptive(SEXP dest, SEXP src, SEXP offset, SEXP size);
303303
SEXP memcpyDTadaptive(SEXP dest, SEXP src, SEXP offset, SEXP size);
304-
SEXP setgrowable(SEXP x);
304+
SEXP copyAsGrowable(SEXP x, SEXP by_column);
305305

306306
// nafill.c
307307
void nafillDouble(double *x, uint_fast64_t nx, unsigned int type, double fill, bool nan_is_na, ans_t *ans, bool verbose);

src/frollapply.c

Lines changed: 10 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -75,6 +75,14 @@ SEXP memcpyDTadaptive(SEXP dest, SEXP src, SEXP offset, SEXP size) {
7575
// # nocov end
7676

7777
// needed in adaptive=TRUE
78-
SEXP setgrowable(SEXP x) {
79-
return x;
78+
SEXP copyAsGrowable(SEXP x, SEXP by_column) {
79+
if (LOGICAL_RO(by_column)[0])
80+
return R_duplicateAsResizable(x);
81+
82+
SEXP ret = PROTECT(shallow_duplicate(x));
83+
R_xlen_t n = xlength(ret);
84+
for (R_xlen_t i = 0; i < n; ++i)
85+
SET_VECTOR_ELT(ret, i, R_duplicateAsResizable(VECTOR_ELT(ret, i)));
86+
UNPROTECT(1);
87+
return ret;
8088
}

src/init.c

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -158,7 +158,7 @@ R_CallMethodDef callMethods[] = {
158158
{"CmemcpyDT", (DL_FUNC)&memcpyDT, -1},
159159
{"CmemcpyVectoradaptive", (DL_FUNC)&memcpyVectoradaptive, -1},
160160
{"CmemcpyDTadaptive", (DL_FUNC)&memcpyDTadaptive, -1},
161-
{"Csetgrowable", (DL_FUNC)&setgrowable, -1},
161+
{"CcopyAsGrowable", (DL_FUNC)&copyAsGrowable, -1},
162162
{"Cfrolladapt", (DL_FUNC)&frolladapt, -1},
163163
{NULL, NULL, 0}
164164
};

0 commit comments

Comments
 (0)