tidyverse / purrr

A functional programming toolkit for R
https://purrr.tidyverse.org/
Other
1.28k stars 272 forks source link

Use minimal defer() instead of local_bindings() #1093

Closed hadley closed 1 year ago

hadley commented 1 year ago

Fixes #1089

This seems to eliminate most of the performance problem:

library(purrr)
x <- as.list(letters)

bench::mark(
  map_chr = map_chr(x, function(x) paste0(x, "a")),
  map = map(x, function(x) paste0(x, "a")) |> list_c(ptype = character()),
  map_vec = map_vec(x, function(x) paste0(x, "a"), .ptype = character())
)
#> # A tibble: 3 × 6
#>   expression      min   median `itr/sec` mem_alloc `gc/sec`
#>   <bch:expr> <bch:tm> <bch:tm>     <dbl> <bch:byt>    <dbl>
#> 1 map_chr      39.6µs   41.4µs    23769.    3.88MB     35.7
#> 2 map            46µs   47.7µs    20567.   51.27KB     36.2
#> 3 map_vec      52.2µs   53.9µs    18327.      71KB     35.9

Created on 2023-07-27 with reprex v2.0.2