r-lib / rlang

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

speed up package version check in `.rlang_cli_compat()` #1657

Closed simonpcouch closed 9 months ago

simonpcouch commented 9 months ago

I recently came across a slowdown when profiling some tidymodels code that I thought might be worth raising. The utils::packageVersion("rlang") >= "0.4.2" check in standalone-cli.R adds up for us downstream.

With rlang 1.1.1:

bench::mark(
  signal = rlang::signal(class = "boop"),
  group_by = dplyr::group_by(mtcars, cyl),
  check = FALSE
)
#> # A tibble: 2 × 6
#>   expression      min   median `itr/sec` mem_alloc `gc/sec`
#>   <bch:expr> <bch:tm> <bch:tm>     <dbl> <bch:byt>    <dbl>
#> 1 signal        276µs    288µs     3393.  296.68KB     16.8
#> 2 group_by      860µs    900µs     1080.    3.49MB     16.8

With this PR:

#> # A tibble: 2 × 6
#>   expression      min   median `itr/sec` mem_alloc `gc/sec`
#>   <bch:expr> <bch:tm> <bch:tm>     <dbl> <bch:byt>    <dbl>
#> 1 signal       46.1µs   47.8µs    20399.  294.26KB     32.0
#> 2 group_by    572.5µs  604.7µs     1603.    3.48MB     23.2

I believe this should be compatible with the "standalone" approach but feels a bit more hacky than I'd usually be comfortable submitting. Very much open to iterate. :)

lionel- commented 9 months ago

Thanks! The other thing you could do is cache the information in a top-level object namespaced with the compat prefix.