Commit 593a08d
ARROW-9140: [R] Zero-copy Arrow to R where possible
This makes altrep R vectors of type `INTSXP` or `REALSXP` from `arrow::Array` of type `Int32Type` / `DoubleType` that don't have any nulls:
the altrep vector holds an external pointer so that the `Array` stays around, and its payload is shared. The R vector is marked as not mutable.
``` r
library(arrow, warn.conflicts = FALSE)
#> See arrow_info() for available features
```
create a “big” arrow Array with no nulls (just for testing purposes)
``` r
a <- arrow:::Test_array_nonull_dbl_vector(1e7)
```
turn into R vector, using altrep, sharing the payload
``` r
v <- a$as_vector()
```
verify it’s an altrep with the inspect method
``` r
.Internal(inspect(v))
#> @7f9abf8ba470 14 REALSXP g0c0 [REF(65535)] std::shared_ptr<arrow::Array, double, NONULL> (len=10000000, ptr=0x7f9ab5c8cd18)
```
it’s marked as not mutable so check that modify -\> duplicate
``` r
v[1] <- 0
#> Duplicate
.Internal(inspect(v))
#> @7f9ac0000000 14 REALSXP g1c7 [MARK,REF(1)] (len=10000000, tl=0) 0,42,42,42,42,...
```
timings for double vector
``` r
bench::workout({
a <- arrow:::Test_array_nonull_dbl_vector(1e7)
v <- a$as_vector()
.Internal(inspect(v))
v[1] <- 0
.Internal(inspect(v))
})
#> @7f9abc122190 14 REALSXP g0c0 [REF(65535)] std::shared_ptr<arrow::Array, double, NONULL> (len=10000000, ptr=0x7f9aba2109c8)
#> Duplicate
#> @7f9aa5c00000 14 REALSXP g1c7 [MARK,REF(1)] (len=10000000, tl=0) 0,42,42,42,42,...
#> # A tibble: 5 x 3
#> exprs process real
#> <bch:expr> <bch:tm> <bch:tm>
#> 1 a <- arrow:::Test_array_nonull_dbl_vector(1e+07) 70.3ms 70.6ms
#> 2 v <- a$as_vector() 13µs 14.3µs
#> 3 .Internal(inspect(v)) 12µs 11.9µs
```
when a copy is needed, the data is copied entirely:
```r
#> 4 v[1] <- 0 53.1ms 53.2ms
#> 5 .Internal(inspect(v)) 20µs 22.6µs
```
timings for integer vector
``` r
bench::workout({
a <- arrow:::Test_array_nonull_int_vector(1e7)
v <- a$as_vector()
.Internal(inspect(v))
v[1] <- 0
.Internal(inspect(v))
})
#> @7f9abc5bd780 13 INTSXP g0c0 [REF(65535)] std::shared_ptr<arrow::Array, int32, NONULL> (len=10000000, ptr=0x7f9ab8997378)
#> @7f9ac0000000 14 REALSXP g1c7 [MARK,REF(1)] (len=10000000, tl=0) 0,42,42,42,42,...
#> # A tibble: 5 x 3
#> exprs process real
#> <bch:expr> <bch:tm> <bch:tm>
#> 1 a <- arrow:::Test_array_nonull_int_vector(1e+07) 54.5ms 54.7ms
#> 2 v <- a$as_vector() 12µs 13.2µs
#> 3 .Internal(inspect(v)) 11µs 11.3µs
#> 4 v[1] <- 0 851.4ms 854.6ms
#> 5 .Internal(inspect(v)) 17µs 18.8µs
```
<sup>Created on 2021-06-08 by the [reprex package](https://reprex.tidyverse.org) (v2.0.0)</sup>
Closes apache#10445 from romainfrancois/ARROW_9140_zero_copy
Lead-authored-by: Romain Francois <romain@rstudio.com>
Co-authored-by: Romain François <romain@rstudio.com>
Co-authored-by: Benjamin Kietzman <bengilgit@gmail.com>
Signed-off-by: Neal Richardson <neal.p.richardson@gmail.com>1 parent 8827978 commit 593a08d
7 files changed
Lines changed: 341 additions & 0 deletions
File tree
- r
- R
- data-raw
- src
- tests/testthat
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
| |||
214 | 214 | | |
215 | 215 | | |
216 | 216 | | |
| 217 | + | |
| 218 | + | |
| 219 | + | |
| 220 | + | |
| 221 | + | |
217 | 222 | | |
218 | 223 | | |
219 | 224 | | |
| |||
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
| |||
| 1 | + | |
| 2 | + | |
| 3 | + | |
| 4 | + | |
| 5 | + | |
| 6 | + | |
| 7 | + | |
| 8 | + | |
| 9 | + | |
| 10 | + | |
| 11 | + | |
| 12 | + | |
| 13 | + | |
| 14 | + | |
| 15 | + | |
| 16 | + | |
| 17 | + | |
| 18 | + | |
| 19 | + | |
| 20 | + | |
| 21 | + | |
| 22 | + | |
| 23 | + | |
| 24 | + | |
| 25 | + | |
| 26 | + | |
| 27 | + | |
| 28 | + | |
| 29 | + | |
| 30 | + | |
| 31 | + | |
| 32 | + | |
| 33 | + | |
| 34 | + | |
| 35 | + | |
| 36 | + | |
| 37 | + | |
| 38 | + | |
| 39 | + | |
| 40 | + | |
| 41 | + | |
| 42 | + | |
| 43 | + | |
| 44 | + | |
| 45 | + | |
| 46 | + | |
| 47 | + | |
| 48 | + | |
| 49 | + | |
| 50 | + | |
| 51 | + | |
| 52 | + | |
| 53 | + | |
| 54 | + | |
| 55 | + | |
| 56 | + | |
| 57 | + | |
| 58 | + | |
| 59 | + | |
| 60 | + | |
| 61 | + | |
| 62 | + | |
| 63 | + | |
| 64 | + | |
| 65 | + | |
| 66 | + | |
| 67 | + | |
| 68 | + | |
| 69 | + | |
| 70 | + | |
| 71 | + | |
| 72 | + | |
| 73 | + | |
| 74 | + | |
| 75 | + | |
| 76 | + | |
| 77 | + | |
| 78 | + | |
| 79 | + | |
| 80 | + | |
| 81 | + | |
| 82 | + | |
| 83 | + | |
| 84 | + | |
| 85 | + | |
| 86 | + | |
| 87 | + | |
| 88 | + | |
| 89 | + | |
| 90 | + | |
| 91 | + | |
| 92 | + | |
| 93 | + | |
| 94 | + | |
| 95 | + | |
| 96 | + | |
| 97 | + | |
| 98 | + | |
| 99 | + | |
| 100 | + | |
| 101 | + | |
| 102 | + | |
| 103 | + | |
| 104 | + | |
| 105 | + | |
| 106 | + | |
| 107 | + | |
| 108 | + | |
| 109 | + | |
| 110 | + | |
| 111 | + | |
| 112 | + | |
| 113 | + | |
| 114 | + | |
| 115 | + | |
| 116 | + | |
| 117 | + | |
| 118 | + | |
| 119 | + | |
| 120 | + | |
| 121 | + | |
| 122 | + | |
| 123 | + | |
| 124 | + | |
| 125 | + | |
| 126 | + | |
| 127 | + | |
| 128 | + | |
| 129 | + | |
| 130 | + | |
| 131 | + | |
| 132 | + | |
| 133 | + | |
| 134 | + | |
| 135 | + | |
| 136 | + | |
| 137 | + | |
| 138 | + | |
| 139 | + | |
| 140 | + | |
| 141 | + | |
| 142 | + | |
| 143 | + | |
| 144 | + | |
| 145 | + | |
| 146 | + | |
| 147 | + | |
| 148 | + | |
| 149 | + | |
| 150 | + | |
| 151 | + | |
| 152 | + | |
| 153 | + | |
| 154 | + | |
| 155 | + | |
| 156 | + | |
| 157 | + | |
| 158 | + | |
| 159 | + | |
| 160 | + | |
| 161 | + | |
| 162 | + | |
| 163 | + | |
| 164 | + | |
| 165 | + | |
| 166 | + | |
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
| |||
28 | 28 | | |
29 | 29 | | |
30 | 30 | | |
| 31 | + | |
31 | 32 | | |
32 | 33 | | |
33 | 34 | | |
| |||
143 | 144 | | |
144 | 145 | | |
145 | 146 | | |
| 147 | + | |
| 148 | + | |
| 149 | + | |
| 150 | + | |
| 151 | + | |
| 152 | + | |
| 153 | + | |
| 154 | + | |
| 155 | + | |
| 156 | + | |
| 157 | + | |
| 158 | + | |
| 159 | + | |
| 160 | + | |
| 161 | + | |
| 162 | + | |
| 163 | + | |
| 164 | + | |
146 | 165 | | |
147 | 166 | | |
148 | 167 | | |
| |||
1280 | 1299 | | |
1281 | 1300 | | |
1282 | 1301 | | |
| 1302 | + | |
| 1303 | + | |
| 1304 | + | |
| 1305 | + | |
1283 | 1306 | | |
1284 | 1307 | | |
1285 | 1308 | | |
| |||
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
| |||
165 | 165 | | |
166 | 166 | | |
167 | 167 | | |
| 168 | + | |
| 169 | + | |
| 170 | + | |
| 171 | + | |
| 172 | + | |
| 173 | + | |
168 | 174 | | |
169 | 175 | | |
170 | 176 | | |
| |||
0 commit comments