Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add ALTREP view support #1725

Draft
wants to merge 5 commits into
base: main
Choose a base branch
from
Draft
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 3 additions & 0 deletions R/altrep.R
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
is_altrep <- function(x) {
.Call(ffi_is_altrep, x)
}
17 changes: 17 additions & 0 deletions R/view.R
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
vec_view <- function(x, start, size) {
check_number_whole(start)
check_number_whole(size)
.Call(ffi_vec_view, x, start, size)
}

view_inspect <- function(x) {
invisible(.Call(ffi_view_inspect, x))
}

view_is_materialized <- function(x) {
.Call(ffi_view_is_materialized, x)
}

view_materialize <- function(x) {
.Call(ffi_view_materialize, x)
}
1 change: 1 addition & 0 deletions src/Makevars
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,7 @@ lib-files = \
rlang/vec.c \
rlang/vec-chr.c \
rlang/vec-lgl.c \
rlang/view.c \
rlang/vendor.c \
rlang/walk.c

Expand Down
34 changes: 34 additions & 0 deletions src/internal/exported.c
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,9 @@
#include "../internal/utils.h"
#include "../internal/vec.h"

// From rlang/rlang.c
void r_init_library_with_dll(DllInfo* dll, const char* package);

// From rlang/vec.c
void r_vec_poke_n(r_obj* x, r_ssize offset,
r_obj* y, r_ssize from, r_ssize n);
Expand All @@ -18,6 +21,13 @@
}


// altrep.h

r_obj* ffi_is_altrep(r_obj* x) {
return r_lgl(r_is_altrep(x));
}


// cnd.c

r_obj* ffi_cnd_signal(r_obj* cnd) {
Expand Down Expand Up @@ -1053,6 +1063,30 @@
}


// view.c

r_obj* ffi_vec_view(r_obj* x, r_obj* ffi_start, r_obj* ffi_size) {
const r_ssize start = r_arg_as_ssize(ffi_start, "start") - 1;
const r_ssize size = r_arg_as_ssize(ffi_size, "size");
return r_vec_view(x, start, size);
}

r_obj* ffi_view_inspect(r_obj* x) {
r_check_view(x);
return r_lgl(r_view_inspect(x));
}

r_obj* ffi_view_is_materialized(r_obj* x) {
r_check_view(x);
return r_lgl(r_view_is_materialized(x));
}

r_obj* ffi_view_materialize(r_obj* x) {

Check warning on line 1084 in src/internal/exported.c

View check run for this annotation

Codecov / codecov/patch

src/internal/exported.c#L1084

Added line #L1084 was not covered by tests
r_check_view(x);
return r_view_materialize(x);
}


// walk.c

static inline
Expand Down
7 changes: 7 additions & 0 deletions src/internal/init.c
Original file line number Diff line number Diff line change
Expand Up @@ -111,6 +111,7 @@ static const R_CallMethodDef r_callables[] = {
{"ffi_init_rlang", (DL_FUNC) &ffi_init_rlang, 1},
{"ffi_interp", (DL_FUNC) &ffi_interp, 2},
{"ffi_interrupt", (DL_FUNC) &ffi_interrupt, 0},
{"ffi_is_altrep", (DL_FUNC) &ffi_is_altrep, 1},
{"ffi_is_atomic", (DL_FUNC) &ffi_is_atomic, 2},
{"ffi_is_call", (DL_FUNC) &ffi_is_call, 4},
{"ffi_is_character", (DL_FUNC) &ffi_is_character, 4},
Expand Down Expand Up @@ -238,6 +239,10 @@ static const R_CallMethodDef r_callables[] = {
{"ffi_vec_poke_n", (DL_FUNC) &ffi_vec_poke_n, 5},
{"ffi_vec_poke_range", (DL_FUNC) &ffi_vec_poke_range, 5},
{"ffi_vec_resize", (DL_FUNC) &ffi_vec_resize, 2},
{"ffi_vec_view", (DL_FUNC) &ffi_vec_view, 3},
{"ffi_view_inspect", (DL_FUNC) &ffi_view_inspect, 1},
{"ffi_view_is_materialized", (DL_FUNC) &ffi_view_is_materialized, 1},
{"ffi_view_materialize", (DL_FUNC) &ffi_view_materialize, 1},
{"ffi_which_operator", (DL_FUNC) &ffi_which_operator, 1},
{"ffi_wref_key", (DL_FUNC) &ffi_wref_key, 1},
{"ffi_wref_value", (DL_FUNC) &ffi_wref_value, 1},
Expand Down Expand Up @@ -321,6 +326,8 @@ void R_init_rlang(DllInfo* dll) {

R_registerRoutines(dll, NULL, r_callables, NULL, externals);
R_useDynamicSymbols(dll, FALSE);

r_init_library_with_dll(dll, "rlang");
}


Expand Down
21 changes: 21 additions & 0 deletions src/rlang/altrep.h
Original file line number Diff line number Diff line change
Expand Up @@ -13,5 +13,26 @@
# define ALTREP(x) false
#endif

#if R_VERSION >= R_Version(4, 3, 0)
#define RLANG_R_HAS_ALTLIST 1
#else
#define RLANG_R_HAS_ALTLIST 0
#endif

static inline
bool r_is_altrep(r_obj* x) {
return ALTREP(x);
}

static inline
r_obj* r_altrep_data1(r_obj* x) {
return R_altrep_data1(x);
}

static inline
r_obj* r_altrep_data2(r_obj* x) {
return R_altrep_data2(x);
}


#endif
1 change: 1 addition & 0 deletions src/rlang/rlang-types.h
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,7 @@

typedef struct SEXPREC r_obj;
typedef Rcomplex r_complex;
typedef Rbyte r_byte;

typedef R_xlen_t r_ssize;
#define R_SSIZE_MAX R_XLEN_T_MAX
Expand Down
8 changes: 8 additions & 0 deletions src/rlang/rlang.c
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,7 @@
#include "vec-chr.c"
#include "vec-lgl.c"
#include "vendor.c"
#include "view.c"
#include "walk.c"


Expand Down Expand Up @@ -120,4 +121,11 @@ r_obj* r_init_library(r_obj* ns) {
return r_null;
}

// This *must* be called before making any calls to the functions
// provided in the library. Call this function from your `R_init_<package>()`
// function, passing along the `dll` and your package's name.
void r_init_library_with_dll(DllInfo* dll, const char* package) {
r_init_library_view(dll, package);
}

bool _r_use_local_precious_list = false;
1 change: 1 addition & 0 deletions src/rlang/rlang.h
Original file line number Diff line number Diff line change
Expand Up @@ -72,6 +72,7 @@ bool _r_use_local_precious_list;
#include "vec-chr.h"
#include "vec-lgl.h"
#include "vendor.h"
#include "view.h"
#include "walk.h"


Expand Down
12 changes: 10 additions & 2 deletions src/rlang/vec.h
Original file line number Diff line number Diff line change
Expand Up @@ -21,9 +21,13 @@ r_complex* r_cpl_begin(r_obj* x) {
return COMPLEX(x);
}
static inline
void* r_raw_begin(r_obj* x) {
r_byte* r_raw_begin0(r_obj* x) {
return RAW(x);
}
static inline
void* r_raw_begin(r_obj* x) {
return r_raw_begin0(x);
}

static inline
const int* r_int_cbegin(r_obj* x) {
Expand All @@ -42,8 +46,12 @@ const r_complex* r_cpl_cbegin(r_obj* x) {
return (const r_complex*) COMPLEX(x);
}
static inline
const r_byte* r_raw_cbegin0(r_obj* x) {
return (const r_byte*) RAW(x);
}
static inline
const void* r_raw_cbegin(r_obj* x) {
return (const void*) RAW(x);
return (const void*) r_raw_cbegin0(x);
}
static inline
r_obj* const * r_chr_cbegin(r_obj* x) {
Expand Down
Loading
Loading