Skip to content

Commit 3b31850

Browse files
committed
Use growable_* instead of SETLENGTH et al in fread
1 parent 421170e commit 3b31850

File tree

2 files changed

+7
-10
lines changed

2 files changed

+7
-10
lines changed

src/dogroups.c

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -523,7 +523,7 @@ SEXP growVector(SEXP x, const R_len_t newlen)
523523
SEXP newx;
524524
R_len_t len = length(x);
525525
if (isNull(x)) error(_("growVector passed NULL"));
526-
PROTECT(newx = allocVector(TYPEOF(x), newlen)); // TO DO: R_realloc(?) here?
526+
PROTECT(newx = growable_allocate(TYPEOF(x), newlen, newlen)); // may be shrunk later by fread
527527
if (newlen < len) len=newlen; // i.e. shrink
528528
switch (TYPEOF(x)) {
529529
case RAWSXP: memcpy(RAW(newx), RAW(x), len*SIZEOF(x)); break;

src/freadR.c

Lines changed: 6 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -257,7 +257,7 @@ bool userOverride(int8_t *type, lenOff *colNames, const char *anchor, const int
257257
if (typeSize[CT_BOOL8_N]!=1) internal_error(__func__, "typeSize[CT_BOOL8_N] != 1"); // # nocov
258258
if (typeSize[CT_STRING]!=8) internal_error(__func__, "typeSize[CT_STRING] != 1"); // # nocov
259259
colNamesSxp = R_NilValue;
260-
SET_VECTOR_ELT(RCHK, 1, colNamesSxp=allocVector(STRSXP, ncol));
260+
SET_VECTOR_ELT(RCHK, 1, colNamesSxp=growable_allocate(STRSXP, ncol, ncol));
261261
for (int i=0; i<ncol; i++) {
262262
SEXP elem;
263263
if (colNames==NULL || colNames[i].len<=0) {
@@ -445,7 +445,7 @@ size_t allocateDT(int8_t *typeArg, int8_t *sizeArg, int ncolArg, int ndrop, size
445445
if (newDT) {
446446
ncol = ncolArg;
447447
dtnrows = allocNrow;
448-
SET_VECTOR_ELT(RCHK, 0, DT=allocVector(VECSXP, ncol-ndrop));
448+
SET_VECTOR_ELT(RCHK, 0, DT=growable_allocate(VECSXP, ncol-ndrop, ncol-ndrop));
449449
if (ndrop==0) {
450450
setAttrib(DT, R_NamesSymbol, colNamesSxp); // colNames mkChar'd in userOverride step
451451
if (colClassesAs) setAttrib(DT, sym_colClassesAs, colClassesAs);
@@ -497,7 +497,7 @@ size_t allocateDT(int8_t *typeArg, int8_t *sizeArg, int ncolArg, int ndrop, size
497497
int typeChanged = (type[i] > 0) && (newDT || TYPEOF(col) != typeSxp[type[i]] || oldIsInt64 != newIsInt64);
498498
int nrowChanged = (allocNrow != dtnrows);
499499
if (typeChanged || nrowChanged) {
500-
SEXP thiscol = typeChanged ? allocVector(typeSxp[type[i]], allocNrow) // no need to PROTECT, passed immediately to SET_VECTOR_ELT, see R-exts 5.9.1
500+
SEXP thiscol = typeChanged ? growable_allocate(typeSxp[type[i]], allocNrow, allocNrow) // no need to PROTECT, passed immediately to SET_VECTOR_ELT, see R-exts 5.9.1
501501
: growVector(col, allocNrow);
502502
SET_VECTOR_ELT(DT,resi,thiscol);
503503
if (type[i]==CT_INT64) {
@@ -519,7 +519,6 @@ size_t allocateDT(int8_t *typeArg, int8_t *sizeArg, int ncolArg, int ndrop, size
519519

520520
setAttrib(thiscol, sym_tzone, ScalarString(char_UTC)); // see news for v1.13.0
521521
}
522-
SET_TRUELENGTH(thiscol, allocNrow);
523522
DTbytes += SIZEOF(thiscol)*allocNrow;
524523
}
525524
resi++;
@@ -536,9 +535,7 @@ void setFinalNrow(size_t nrow) {
536535
return;
537536
const int ncol=LENGTH(DT);
538537
for (int i=0; i<ncol; i++) {
539-
SETLENGTH(VECTOR_ELT(DT,i), nrow);
540-
SET_TRUELENGTH(VECTOR_ELT(DT,i), dtnrows);
541-
SET_GROWABLE_BIT(VECTOR_ELT(DT,i)); // #3292
538+
growable_resize(VECTOR_ELT(DT,i), nrow);
542539
}
543540
}
544541
R_FlushConsole(); // # 2481. Just a convenient place; nothing per se to do with setFinalNrow()
@@ -551,8 +548,8 @@ void dropFilledCols(int* dropArg, int ndelete) {
551548
SET_VECTOR_ELT(DT, dropFill[i], R_NilValue);
552549
SET_STRING_ELT(colNamesSxp, dropFill[i], NA_STRING);
553550
}
554-
SETLENGTH(DT, ndt-ndelete);
555-
SETLENGTH(colNamesSxp, ndt-ndelete);
551+
growable_resize(DT, ndt-ndelete);
552+
growable_resize(colNamesSxp, ndt-ndelete);
556553
}
557554

558555
void pushBuffer(ThreadLocalFreadParsingContext *ctx)

0 commit comments

Comments
 (0)