r-lib / rlang

Low-level API for programming with R
https://rlang.r-lib.org
Other
498 stars 136 forks source link

Cache result of `rlang:::is_on_disk()` #1608

Open DavisVaughan opened 1 year ago

DavisVaughan commented 1 year ago

Due to it calling system.file(), which is typically around half the time of is_installed() or check_installed().

Would also be good to look into what the rest of the time is taken up by, but this is a good first step.

library(rlang)

bench::mark(check_installed("dplyr"))
#> # A tibble: 1 × 6
#>   expression                    min   median `itr/sec` mem_alloc `gc/sec`
#>   <bch:expr>               <bch:tm> <bch:tm>     <dbl> <bch:byt>    <dbl>
#> 1 check_installed("dplyr")    760µs    877µs     1119.    8.11MB     10.4

bench::mark(is_installed("dplyr"))
#> # A tibble: 1 × 6
#>   expression                 min   median `itr/sec` mem_alloc `gc/sec`
#>   <bch:expr>            <bch:tm> <bch:tm>     <dbl> <bch:byt>    <dbl>
#> 1 is_installed("dplyr")    755µs    854µs     1139.    1.95KB     12.6

bench::mark(requireNamespace("dplyr", quietly = TRUE))
#> # A tibble: 1 × 6
#>   expression                                  min median itr/s…¹ mem_a…² gc/se…³
#>   <bch:expr>                                <bch> <bch:>   <dbl> <bch:b>   <dbl>
#> 1 requireNamespace("dplyr", quietly = TRUE) 956ns 1.22µs 791422.      0B       0
#> # … with abbreviated variable names ¹​`itr/sec`, ²​mem_alloc, ³​`gc/sec`

# Often around half the time
bench::mark(system.file(package = "dplyr"))
#> # A tibble: 1 × 6
#>   expression                          min   median `itr/sec` mem_alloc `gc/sec`
#>   <bch:expr>                     <bch:tm> <bch:tm>     <dbl> <bch:byt>    <dbl>
#> 1 system.file(package = "dplyr")    334µs    352µs     2434.    1.95KB     4.06

Created on 2023-03-28 with reprex v2.0.2.9000