You are viewing a plain text version of this content. The canonical link for it is here.
Posted to github@arrow.apache.org by GitBox <gi...@apache.org> on 2021/12/21 12:52:01 UTC

[GitHub] [arrow] paleolimbot commented on a change in pull request #11894: ARROW-14029: [R] Repair map_batches()

paleolimbot commented on a change in pull request #11894:
URL: https://github.com/apache/arrow/pull/11894#discussion_r773109692



##########
File path: r/R/dataset-scan.R
##########
@@ -185,17 +183,29 @@ ScanTask <- R6Class("ScanTask",
 #' `data.frame`? Default `TRUE`
 #' @export
 map_batches <- function(X, FUN, ..., .data.frame = TRUE) {
-  if (.data.frame) {
-    lapply <- map_dfr
-  }
-  scanner <- Scanner$create(ensure_group_vars(X))
+  # TODO: possibly refactor do_exec_plan to return a RecordBatchReader
+  plan <- ExecPlan$create()
+  final_node <- plan$Build(X)
+  reader <- plan$Run(final_node)
   FUN <- as_mapper(FUN)
-  lapply(scanner$ScanBatches(), function(batch) {
-    # TODO: wrap batch in arrow_dplyr_query with X$selected_columns,
-    # X$temp_columns, and X$group_by_vars
-    # if X is arrow_dplyr_query, if some other arg (.dplyr?) == TRUE
-    FUN(batch, ...)
-  })
+
+  # TODO: wrap batch in arrow_dplyr_query with X$selected_columns,
+  # X$temp_columns, and X$group_by_vars
+  # if X is arrow_dplyr_query, if some other arg (.dplyr?) == TRUE
+  batch <- reader$read_next_batch()
+  res <- list()

Review comment:
       I'm not sure exactly how many batches are going to get processed here, but if it's over about 1000 the `append()` approach starts to get slow. The non-append approach isn't too verbose if you're interested:
   
   ``` r
   append_with_initial_alloc <- function(n) {
     res <- vector("list", 1024)
     i <- 0L
     while (i < n) {
       i <- i + 1L
       # this will work even after i > n in R and
       # is relatively fast because list()s are overallocated
       # by about 5% for this express use-case
       res[[i]] <- sprintf("result %d", i)
     }
     
     if (i < length(res)) {
       res <- res[seq_len(i)]
     }
     invisible(res)
   }
   
   append_without_initial_alloc <- function(n) {
     res <- list()
     i <- 0L
     while (i < n) {
       i <- i + 1L
       res <- append(res, list(sprintf("result %d", i)))
     }
     invisible(res)
   }
   
   bench::mark(
     append_with_initial_alloc(1e4),
     append_without_initial_alloc(1e4)
   )
   #> Warning: Some expressions had a GC in every iteration; so filtering is disabled.
   #> # A tibble: 2 × 6
   #>   expression                               min   median `itr/sec` mem_alloc
   #>   <bch:expr>                          <bch:tm> <bch:tm>     <dbl> <bch:byt>
   #> 1 append_with_initial_alloc(10000)      6.64ms   6.78ms    134.      1.99MB
   #> 2 append_without_initial_alloc(10000)  291.2ms 292.58ms      3.42  381.99MB
   #> # … with 1 more variable: gc/sec <dbl>
   ```




-- 
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: github-unsubscribe@arrow.apache.org

For queries about this service, please contact Infrastructure at:
users@infra.apache.org