Skip to content

Commit 405393d

Browse files
authored
Get rid of STDVEC_DATAPTR() and DATAPTR() usage (#562)
* Get rid of STDVEC_DATAPTR() usage * Remove usage of DATAPTR (well, use the recommended backport solution) * Try to force a current version of ggplot2 * How about not building vignettes on really old R? * Go back to original workflow config * Try pinning the version of patchwork, instead of ggplot2 * Add NEWS bullet * Use DATAPTR_RW() in all the Dataptr() methods * Flesh out force_materialization() and use it in more places * Use read-only access when de-altrep-ing prior to write
1 parent 953cc26 commit 405393d

File tree

15 files changed

+48
-23
lines changed

15 files changed

+48
-23
lines changed

.github/workflows/R-CMD-check.yaml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -62,7 +62,7 @@ jobs:
6262

6363
- uses: r-lib/actions/setup-r-dependencies@v2
6464
with:
65-
extra-packages: any::rcmdcheck
65+
extra-packages: any::rcmdcheck, [email protected]
6666
needs: check
6767

6868
- uses: r-lib/actions/check-r-package@v2

NEWS.md

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,7 @@
11
# vroom (development version)
22

3+
* vroom no longer uses `STDVEC_DATAPTR()` and takes the recommended approach for phasing out usage of `DATAPTR()` (#561).
4+
35
# vroom 1.6.6
46

57
* Fixed a bad URL in the README at CRAN's request.

src/altrep.cc

Lines changed: 27 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -16,7 +16,26 @@
1616

1717
[[cpp11::register]] void force_materialization(SEXP x) {
1818
#ifdef HAS_ALTREP
19-
DATAPTR(x);
19+
// Note: vroom_lgl has no ALTREP implementation, so not included
20+
if (R_altrep_inherits(x, vroom_chr::class_t)) {
21+
vroom_chr::Materialize(x);
22+
} else if (R_altrep_inherits(x, vroom_date::class_t)) {
23+
vroom_date::Materialize(x);
24+
} else if (R_altrep_inherits(x, vroom_dbl::class_t)) {
25+
vroom_dbl::Materialize(x);
26+
} else if (R_altrep_inherits(x, vroom_dttm::class_t)) {
27+
vroom_dttm::Materialize(x);
28+
} else if (R_altrep_inherits(x, vroom_fct::class_t)) {
29+
vroom_fct::Materialize(x);
30+
} else if (R_altrep_inherits(x, vroom_int::class_t)) {
31+
vroom_int::Materialize(x);
32+
} else if (R_altrep_inherits(x, vroom_num::class_t)) {
33+
vroom_num::Materialize(x);
34+
} else if (R_altrep_inherits(x, vroom_time::class_t)) {
35+
vroom_time::Materialize(x);
36+
} else if (R_altrep_inherits(x, vroom_big_int::class_t)) {
37+
vroom_big_int::Materialize(x);
38+
}
2039
#endif
2140
}
2241

@@ -40,15 +59,13 @@ bool vroom_altrep(SEXP x) {
4059
[[cpp11::register]] SEXP vroom_materialize(SEXP x, bool replace) {
4160
#ifdef HAS_ALTREP
4261
for (R_xlen_t col = 0; col < Rf_xlength(x); ++col) {
43-
4462
SEXP elt = VECTOR_ELT(x, col);
45-
// First materialize all of the non-character vectors
4663
if (vroom_altrep(elt)) {
47-
DATAPTR(elt);
64+
force_materialization(elt);
4865
}
4966
}
5067

51-
// If replace replace the altrep vectors with their materialized
68+
// If replace, replace the altrep vectors with their materialized
5269
// vectors
5370
if (replace) {
5471
for (R_xlen_t col = 0; col < Rf_xlength(x); ++col) {
@@ -81,7 +98,7 @@ bool vroom_altrep(SEXP x) {
8198
case LGLSXP: {
8299
SET_VECTOR_ELT(out, col, Rf_allocVector(LGLSXP, nrow));
83100
int* out_p = LOGICAL(VECTOR_ELT(out, col));
84-
int* in_p = LOGICAL(elt);
101+
const int* in_p = LOGICAL_RO(elt);
85102
for (R_xlen_t row = 0; row < nrow; ++row) {
86103
out_p[row] = in_p[row];
87104
}
@@ -90,7 +107,7 @@ bool vroom_altrep(SEXP x) {
90107
case INTSXP: {
91108
SET_VECTOR_ELT(out, col, Rf_allocVector(INTSXP, nrow));
92109
int* out_p = INTEGER(VECTOR_ELT(out, col));
93-
int* in_p = INTEGER(elt);
110+
const int* in_p = INTEGER_RO(elt);
94111
for (R_xlen_t row = 0; row < nrow; ++row) {
95112
out_p[row] = in_p[row];
96113
}
@@ -99,7 +116,7 @@ bool vroom_altrep(SEXP x) {
99116
case REALSXP: {
100117
SET_VECTOR_ELT(out, col, Rf_allocVector(REALSXP, nrow));
101118
double* out_p = REAL(VECTOR_ELT(out, col));
102-
double* in_p = REAL(elt);
119+
const double* in_p = REAL_RO(elt);
103120
for (R_xlen_t row = 0; row < nrow; ++row) {
104121
out_p[row] = in_p[row];
105122
}
@@ -108,9 +125,9 @@ bool vroom_altrep(SEXP x) {
108125
case STRSXP: {
109126
SET_VECTOR_ELT(out, col, Rf_allocVector(STRSXP, nrow));
110127
SEXP out_elt = VECTOR_ELT(out, col);
111-
DATAPTR(elt);
128+
const SEXP* in_p = STRING_PTR_RO(elt);
112129
for (R_xlen_t row = 0; row < nrow; ++row) {
113-
SET_STRING_ELT(out_elt, row, STRING_ELT(elt, row));
130+
SET_STRING_ELT(out_elt, row, in_p[row]);
114131
}
115132
break;
116133
}

src/altrep.h

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -39,4 +39,10 @@ extern "C" {
3939
#include <R_ext/Altrep.h>
4040
}
4141
#endif
42+
43+
// Backport DATAPTR_RW for R < 4.6.0 (as recommended in Writing R Extensions)
44+
#if R_VERSION < R_Version(4, 6, 0)
45+
# define DATAPTR_RW(x) DATAPTR(x)
46+
#endif
47+
4248
#endif

src/vroom_big_int.h

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -99,7 +99,7 @@ class vroom_big_int : public vroom_vec {
9999
}
100100

101101
static void* Dataptr(SEXP vec, Rboolean) {
102-
return STDVEC_DATAPTR(Materialize(vec));
102+
return DATAPTR_RW(Materialize(vec));
103103
}
104104

105105
// -------- initialize the altrep class with the methods above

src/vroom_chr.h

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -103,7 +103,7 @@ struct vroom_chr : vroom_vec {
103103
}
104104

105105
static void* Dataptr(SEXP vec, Rboolean) {
106-
return STDVEC_DATAPTR(Materialize(vec));
106+
return DATAPTR_RW(Materialize(vec));
107107
}
108108

109109
// -------- initialize the altrep class with the methods above

src/vroom_date.h

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -102,7 +102,7 @@ class vroom_date : public vroom_dttm {
102102
}
103103

104104
static void* Dataptr(SEXP vec, Rboolean) {
105-
return STDVEC_DATAPTR(Materialize(vec));
105+
return DATAPTR_RW(Materialize(vec));
106106
}
107107

108108
// -------- initialize the altrep class with the methods above

src/vroom_dbl.h

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -84,7 +84,7 @@ class vroom_dbl : public vroom_vec {
8484
}
8585

8686
static void* Dataptr(SEXP vec, Rboolean) {
87-
return STDVEC_DATAPTR(Materialize(vec));
87+
return DATAPTR_RW(Materialize(vec));
8888
}
8989

9090
// -------- initialize the altrep class with the methods above

src/vroom_dttm.h

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -210,7 +210,7 @@ class vroom_dttm : public vroom_vec {
210210
}
211211

212212
static void* Dataptr(SEXP vec, Rboolean) {
213-
return STDVEC_DATAPTR(Materialize(vec));
213+
return DATAPTR_RW(Materialize(vec));
214214
}
215215

216216
// -------- initialize the altrep class with the methods above

src/vroom_fct.h

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -215,7 +215,7 @@ struct vroom_fct : vroom_vec {
215215
}
216216

217217
static void* Dataptr(SEXP vec, Rboolean) {
218-
return STDVEC_DATAPTR(Materialize(vec));
218+
return DATAPTR_RW(Materialize(vec));
219219
}
220220

221221
static SEXP Extract_subset(SEXP x, SEXP indx, SEXP) {

0 commit comments

Comments
 (0)