rstudio / rstudioapi

Safely access RStudio's API (when available)
http://rstudio.github.io/rstudioapi
Other
165 stars 35 forks source link

Feature request: `getMode()` as a fast shortcut for `versionInfo()$mode` #280

Open DavisVaughan opened 11 months ago

DavisVaughan commented 11 months ago

Similar to #279

We discovered in https://github.com/r-lib/lifecycle/issues/167 that cli::num_ansi_colors() is slow when called interactively in RStudio due to rstudioapi::versionInfo() being fairly slow. versionInfo() is slow due to having to call readCitationFile(), and in cli the citation bit isn't required at all. The only things it uses are the $version and the $mode.

getVersion() gives us fast (in theory, see #279) access to $version. It would be nice to also have getMode() that cli could use to have fast access to the $mode.

library(rstudioapi)

bench::mark(
  .Call("rs_rstudioProgramMode", PACKAGE = "(embedding)"),
  versionInfo()$mode
)
# A tibble: 2 × 13
  expression      min median `itr/sec` mem_alloc `gc/sec` n_itr  n_gc total_time result memory    
  <bch:expr>    <bch> <bch:>     <dbl> <bch:byt>    <dbl> <int> <dbl>   <bch:tm> <list> <list>    
1 ".Call(\"rs_… 328ns  410ns  2298553.        0B     0    10000     0     4.35ms <chr>  <Rprofmem>
2 "versionInfo… 375µs  386µs     2515.    20.6KB     8.27  1216     4   483.42ms <chr>  <Rprofmem>
# ℹ 2 more variables: time <list>, gc <list>