krlmlr commented on code in PR #279:
URL: https://github.com/apache/arrow-nanoarrow/pull/279#discussion_r1299384346
##########
r/R/convert-array-stream.R:
##########
@@ -62,26 +68,52 @@ convert_array_stream <- function(array_stream, to = NULL,
size = NULL, n = Inf)
)
}
+ # Collect all batches into a list()
+ batches <- collect_array_stream(
+ array_stream,
+ n,
+ schema = schema,
+ validate = FALSE
+ )
+
+ # If there is exactly one batch, use convert_array()
+ if (length(batches) == 1L) {
+ return(.Call(nanoarrow_c_convert_array, batches[[1]], to))
+ }
+
+ # Otherwise, compute the final size, create another array stream,
+ # and call convert_array_stream() with a known size.
+ lengths <- vapply(
+ batches,
+ # Use custom accessor because array$length in a loop is slow
+ function(array) {
+ .Call(nanoarrow_c_array_proxy, array, NULL, FALSE)$length
+ },
+ double(1)
+ )
+ basic_stream <- .Call(nanoarrow_c_basic_array_stream, batches, schema, FALSE)
+ convert_array_stream(basic_stream, to = to, size = sum(lengths))
+}
+
+#' @rdname convert_array_stream
+#' @export
+collect_array_stream <- function(array_stream, n = Inf, schema = NULL,
+ validate = TRUE) {
+ stopifnot(
+ inherits(array_stream, "nanoarrow_array_stream")
+ )
+
+ if (is.null(schema)) {
+ schema <- .Call(nanoarrow_c_array_stream_get_schema, array_stream)
+ }
+
batches <- vector("list", 1024L)
n_batches <- 0L
get_next <- array_stream$get_next
- while (!is.null(array <- get_next(schema, validate = FALSE)) && (n_batches <
n)) {
+ while (!is.null(array <- get_next(schema, validate = validate)) &&
(n_batches < n)) {
n_batches <- n_batches + 1L
- batches[[n_batches]] <- .Call(nanoarrow_c_convert_array, array, to)
+ batches[[n_batches]] <- array
Review Comment:
Without array doubling, the run time still might be superlinear for very
many batches. I have no idea if this is of practical relevance, though.
``` r
fill_list <- function(n) {
out <- vector("list", 1024)
for (i in seq_len(n)) {
out[[i]] <- i
}
NULL
}
bench::mark(fill_list(1e6), fill_list(2e6), fill_list(4e6), fill_list(8e6))
#> Warning: Some expressions had a GC in every iteration; so filtering is
#> disabled.
#> # A tibble: 4 × 6
#> expression min median `itr/sec` mem_alloc `gc/sec`
#> <bch:expr> <bch:tm> <bch:tm> <dbl> <bch:byt> <dbl>
#> 1 fill_list(1e+06) 446.17ms 453.62ms 2.20 165.33MB 2.20
#> 2 fill_list(2e+06) 1.38s 1.38s 0.723 334.95MB 2.17
#> 3 fill_list(4e+06) 4.72s 4.72s 0.212 663.35MB 1.69
#> 4 fill_list(8e+06) 8.42s 8.42s 0.119 1.28GB 1.42
```
<sup>Created on 2023-08-20 with [reprex
v2.0.2](https://reprex.tidyverse.org)</sup>
##########
r/R/convert-array-stream.R:
##########
@@ -62,26 +68,52 @@ convert_array_stream <- function(array_stream, to = NULL,
size = NULL, n = Inf)
)
}
+ # Collect all batches into a list()
+ batches <- collect_array_stream(
+ array_stream,
+ n,
+ schema = schema,
+ validate = FALSE
+ )
+
+ # If there is exactly one batch, use convert_array()
+ if (length(batches) == 1L) {
+ return(.Call(nanoarrow_c_convert_array, batches[[1]], to))
+ }
+
+ # Otherwise, compute the final size, create another array stream,
+ # and call convert_array_stream() with a known size.
+ lengths <- vapply(
+ batches,
+ # Use custom accessor because array$length in a loop is slow
+ function(array) {
+ .Call(nanoarrow_c_array_proxy, array, NULL, FALSE)$length
+ },
+ double(1)
+ )
Review Comment:
Will this be faster and more elegant in C, or even with a single `for` loop,
since you only seem to need `sum(lengths)` ?
##########
r/R/convert-array-stream.R:
##########
@@ -62,26 +68,52 @@ convert_array_stream <- function(array_stream, to = NULL,
size = NULL, n = Inf)
)
}
+ # Collect all batches into a list()
+ batches <- collect_array_stream(
+ array_stream,
+ n,
+ schema = schema,
+ validate = FALSE
+ )
+
+ # If there is exactly one batch, use convert_array()
+ if (length(batches) == 1L) {
+ return(.Call(nanoarrow_c_convert_array, batches[[1]], to))
+ }
+
+ # Otherwise, compute the final size, create another array stream,
+ # and call convert_array_stream() with a known size.
+ lengths <- vapply(
+ batches,
+ # Use custom accessor because array$length in a loop is slow
+ function(array) {
+ .Call(nanoarrow_c_array_proxy, array, NULL, FALSE)$length
+ },
+ double(1)
+ )
+ basic_stream <- .Call(nanoarrow_c_basic_array_stream, batches, schema, FALSE)
+ convert_array_stream(basic_stream, to = to, size = sum(lengths))
+}
+
+#' @rdname convert_array_stream
+#' @export
+collect_array_stream <- function(array_stream, n = Inf, schema = NULL,
+ validate = TRUE) {
+ stopifnot(
+ inherits(array_stream, "nanoarrow_array_stream")
+ )
+
+ if (is.null(schema)) {
+ schema <- .Call(nanoarrow_c_array_stream_get_schema, array_stream)
+ }
+
batches <- vector("list", 1024L)
n_batches <- 0L
get_next <- array_stream$get_next
- while (!is.null(array <- get_next(schema, validate = FALSE)) && (n_batches <
n)) {
+ while (!is.null(array <- get_next(schema, validate = validate)) &&
(n_batches < n)) {
Review Comment:
I prefer a `break` inside the loop for the `n_batches < n` case, it also
makes it very clear when the check occurs.
##########
r/R/convert-array-stream.R:
##########
@@ -62,26 +68,52 @@ convert_array_stream <- function(array_stream, to = NULL,
size = NULL, n = Inf)
)
}
+ # Collect all batches into a list()
+ batches <- collect_array_stream(
+ array_stream,
+ n,
+ schema = schema,
+ validate = FALSE
+ )
+
+ # If there is exactly one batch, use convert_array()
+ if (length(batches) == 1L) {
+ return(.Call(nanoarrow_c_convert_array, batches[[1]], to))
+ }
+
+ # Otherwise, compute the final size, create another array stream,
+ # and call convert_array_stream() with a known size.
+ lengths <- vapply(
+ batches,
+ # Use custom accessor because array$length in a loop is slow
+ function(array) {
+ .Call(nanoarrow_c_array_proxy, array, NULL, FALSE)$length
+ },
+ double(1)
+ )
+ basic_stream <- .Call(nanoarrow_c_basic_array_stream, batches, schema, FALSE)
+ convert_array_stream(basic_stream, to = to, size = sum(lengths))
Review Comment:
Maybe something like this, to avoid the tail recursion and extra work?
```suggestion
batches <- collect_array_stream(
array_stream,
n,
schema = schema,
validate = FALSE
)
# We are guaranteed to have only one batch here:
stopifnot(length(batches) == 1)
.Call(nanoarrow_c_convert_array, batches[[1]], to)
```
##########
r/R/convert-array-stream.R:
##########
@@ -62,26 +68,52 @@ convert_array_stream <- function(array_stream, to = NULL,
size = NULL, n = Inf)
)
}
+ # Collect all batches into a list()
+ batches <- collect_array_stream(
+ array_stream,
+ n,
+ schema = schema,
+ validate = FALSE
+ )
+
+ # If there is exactly one batch, use convert_array()
+ if (length(batches) == 1L) {
+ return(.Call(nanoarrow_c_convert_array, batches[[1]], to))
+ }
+
+ # Otherwise, compute the final size, create another array stream,
+ # and call convert_array_stream() with a known size.
+ lengths <- vapply(
+ batches,
+ # Use custom accessor because array$length in a loop is slow
+ function(array) {
+ .Call(nanoarrow_c_array_proxy, array, NULL, FALSE)$length
+ },
+ double(1)
+ )
+ basic_stream <- .Call(nanoarrow_c_basic_array_stream, batches, schema, FALSE)
+ convert_array_stream(basic_stream, to = to, size = sum(lengths))
+}
+
+#' @rdname convert_array_stream
+#' @export
+collect_array_stream <- function(array_stream, n = Inf, schema = NULL,
+ validate = TRUE) {
+ stopifnot(
+ inherits(array_stream, "nanoarrow_array_stream")
+ )
+
+ if (is.null(schema)) {
+ schema <- .Call(nanoarrow_c_array_stream_get_schema, array_stream)
+ }
+
batches <- vector("list", 1024L)
n_batches <- 0L
get_next <- array_stream$get_next
- while (!is.null(array <- get_next(schema, validate = FALSE)) && (n_batches <
n)) {
+ while (!is.null(array <- get_next(schema, validate = validate)) &&
(n_batches < n)) {
n_batches <- n_batches + 1L
- batches[[n_batches]] <- .Call(nanoarrow_c_convert_array, array, to)
+ batches[[n_batches]] <- array
Review Comment:
Without array doubling, the run time still might be superlinear for very
many batches. I have no idea if this is of practical relevance, though.
``` r
fill_list <- function(n) {
out <- vector("list", 1024)
for (i in seq_len(n)) {
out[[i]] <- i
}
NULL
}
bench::mark(fill_list(1e6), fill_list(2e6), fill_list(4e6), fill_list(8e6))
#> Warning: Some expressions had a GC in every iteration; so filtering is
#> disabled.
#> # A tibble: 4 × 6
#> expression min median `itr/sec` mem_alloc `gc/sec`
#> <bch:expr> <bch:tm> <bch:tm> <dbl> <bch:byt> <dbl>
#> 1 fill_list(1e+06) 446.17ms 453.62ms 2.20 165.33MB 2.20
#> 2 fill_list(2e+06) 1.38s 1.38s 0.723 334.95MB 2.17
#> 3 fill_list(4e+06) 4.72s 4.72s 0.212 663.35MB 1.69
#> 4 fill_list(8e+06) 8.42s 8.42s 0.119 1.28GB 1.42
```
<sup>Created on 2023-08-20 with [reprex
v2.0.2](https://reprex.tidyverse.org)</sup>
--
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.
To unsubscribe, e-mail: [email protected]
For queries about this service, please contact Infrastructure at:
[email protected]