Skip to content

Commit 5f17fab

Browse files
committed
dogroups: replace remaining uses of SETLENGTH
1 parent 64482a8 commit 5f17fab

File tree

2 files changed

+8
-17
lines changed

2 files changed

+8
-17
lines changed

R/data.table.R

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1591,7 +1591,7 @@ replace_dot_alias = function(e) {
15911591
if (length(xcols)) {
15921592
# TODO add: if (max(len__)==nrow) stopf("There is no need to deep copy x in this case")
15931593
# TODO move down to dogroup.c, too.
1594-
SDenv$.SDall = .Call(CsubsetDT, x, if (length(len__)) seq_len(max(len__)) else 0L, xcols) # must be deep copy when largest group is a subset
1594+
SDenv$.SDall = .Call(CcopyAsGrowable, .Call(CsubsetDT, x, if (length(len__)) seq_len(max(len__)) else 0L, xcols), FALSE) # must be deep copy when largest group is a subset
15951595
if (!is.data.table(SDenv$.SDall)) setattr(SDenv$.SDall, "class", c("data.table","data.frame")) # DF |> DT(,.SD[...],by=grp) needs .SD to be data.table, test 2022.012
15961596
if (xdotcols) setattr(SDenv$.SDall, 'names', ansvars[xcolsAns]) # now that we allow 'x.' prefix in 'j', #2313 bug fix - [xcolsAns]
15971597
SDenv$.SD = if (length(non_sdvars)) shallow(SDenv$.SDall, sdvars) else SDenv$.SDall
@@ -1864,7 +1864,7 @@ replace_dot_alias = function(e) {
18641864
grpcols = leftcols # 'leftcols' are the columns in i involved in the join (either head of key(i) or head along i)
18651865
jiscols = chmatch(jisvars, names_i) # integer() if there are no jisvars (usually there aren't, advanced feature)
18661866
xjiscols = chmatch(xjisvars, names_x)
1867-
SDenv$.xSD = x[min(nrow(i), 1L), xjisvars, with=FALSE]
1867+
SDenv$.xSD = .Call(CcopyAsGrowable, x[min(nrow(i), 1L), xjisvars, with=FALSE], FALSE)
18681868
if (!missing(on)) o__ = xo else o__ = integer(0L)
18691869
} else {
18701870
groups = byval

src/dogroups.c

Lines changed: 6 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -124,7 +124,7 @@ SEXP dogroups(SEXP dt, SEXP dtcols, SEXP groups, SEXP grpcols, SEXP jiscols, SEX
124124
for (R_len_t i=0; i<n; ++i) {
125125
if (ilens[i] > maxGrpSize) maxGrpSize = ilens[i];
126126
}
127-
defineVar(install(".I"), I = PROTECT(allocVector(INTSXP, maxGrpSize)), env); nprotect++;
127+
defineVar(install(".I"), I = PROTECT(R_allocResizableVector(INTSXP, maxGrpSize)), env); nprotect++;
128128
hash_set(specials, I, -maxGrpSize); // marker for anySpecialStatic(); see its comments
129129
R_LockBinding(install(".I"), env);
130130

@@ -197,7 +197,7 @@ SEXP dogroups(SEXP dt, SEXP dtcols, SEXP groups, SEXP grpcols, SEXP jiscols, SEX
197197
INTEGER(GRP)[0] = i+1; // group counter exposed as .GRP
198198
INTEGER(rownames)[1] = -grpn; // the .set_row_names() of .SD. Not .N when nomatch=NA and this is a nomatch
199199
for (int j=0; j<length(SDall); ++j) {
200-
SETLENGTH(VECTOR_ELT(SDall,j), grpn); // before copying data in otherwise assigning after the end could error R API checks
200+
R_resizeVector(VECTOR_ELT(SDall,j), grpn); // before copying data in otherwise assigning after the end could error R API checks
201201
defineVar(nameSyms[j], VECTOR_ELT(SDall, j), env);
202202
// Redo this defineVar for each group in case user's j assigned to the column names (env is static) (tests 387 and 388)
203203
// nameSyms pre-stored to save repeated install() for efficiency, though.
@@ -219,7 +219,7 @@ SEXP dogroups(SEXP dt, SEXP dtcols, SEXP groups, SEXP grpcols, SEXP jiscols, SEX
219219
}
220220
if (istarts[i] == NA_INTEGER || (LENGTH(order) && iorder[ istarts[i]-1 ]==NA_INTEGER)) {
221221
for (int j=0; j<length(SDall); ++j) {
222-
SETLENGTH(VECTOR_ELT(SDall, j), 1);
222+
R_resizeVector(VECTOR_ELT(SDall, j), 1);
223223
writeNA(VECTOR_ELT(SDall, j), 0, 1, false);
224224
// writeNA uses SET_ for STR and VEC, and we always use SET_ to assign to SDall always too. Otherwise,
225225
// this writeNA could decrement the reference for the old value which wasn't incremented in the first place.
@@ -231,14 +231,14 @@ SEXP dogroups(SEXP dt, SEXP dtcols, SEXP groups, SEXP grpcols, SEXP jiscols, SEX
231231
// and we hope that reference counting on by default from R 4.0 will avoid costly gc()s.
232232
}
233233
grpn = 1; // it may not be 1 e.g. test 722. TODO: revisit.
234-
SETLENGTH(I, grpn);
234+
R_resizeVector(I, grpn);
235235
INTEGER(I)[0] = 0;
236236
for (int j=0; j<length(xSD); ++j) {
237237
writeNA(VECTOR_ELT(xSD, j), 0, 1, false);
238238
}
239239
} else {
240240
if (verbose) tstart = wallclock();
241-
SETLENGTH(I, grpn);
241+
R_resizeVector(I, grpn);
242242
int *iI = INTEGER(I);
243243
if (LENGTH(order)==0) {
244244
const int rownum = grpn ? istarts[i]-1 : -1;
@@ -319,7 +319,7 @@ SEXP dogroups(SEXP dt, SEXP dtcols, SEXP groups, SEXP grpcols, SEXP jiscols, SEX
319319
RHS = VECTOR_ELT(jval,j%LENGTH(jval));
320320
if (isNull(target)) {
321321
// first time adding to new column
322-
if (TRUELENGTH(dt) < colj+1) internal_error(__func__, "Trying to add new column by reference but tl is full; setalloccol should have run first at R level before getting to this point"); // # nocov
322+
if (R_maxLength(dt) < colj+1) internal_error(__func__, "Trying to add new column by reference but tl is full; setalloccol should have run first at R level before getting to this point"); // # nocov
323323
target = PROTECT(allocNAVectorLike(RHS, n));
324324
// Even if we could know reliably to switch from allocNAVectorLike to allocVector for slight speedup, user code could still
325325
// contain a switched halt, and in that case we'd want the groups not yet done to have NA rather than 0 or uninitialized.
@@ -486,15 +486,6 @@ SEXP dogroups(SEXP dt, SEXP dtcols, SEXP groups, SEXP grpcols, SEXP jiscols, SEX
486486
// ... TO DO: set truelength to LENGTH(VECTOR_ELT(ans,0)), length to ansloc and enhance finalizer to handle over-allocated rows.
487487
}
488488
} else ans = R_NilValue;
489-
// Now reset length of .SD columns and .I to length of largest group, otherwise leak if the last group is smaller (often is).
490-
// Also reset truelength on specials; see comments in anySpecialStatic().
491-
for (int j=0; j<length(SDall); ++j) {
492-
SEXP this = VECTOR_ELT(SDall,j);
493-
SETLENGTH(this, maxGrpSize);
494-
SET_TRUELENGTH(this, maxGrpSize);
495-
}
496-
SETLENGTH(I, maxGrpSize);
497-
SET_TRUELENGTH(I, maxGrpSize);
498489
if (verbose) {
499490
if (nblock[0] && nblock[1]) internal_error(__func__, "block 0 [%d] and block 1 [%d] have both run", nblock[0], nblock[1]); // # nocov
500491
int w = nblock[1]>0;

0 commit comments

Comments
 (0)