rstudio / bslib

Tools for theming Shiny and R Markdown via Bootstrap 3, 4, or 5.
https://rstudio.github.io/bslib/
Other
466 stars 57 forks source link

Performance issue with selectize #369

Closed psimm closed 2 years ago

psimm commented 3 years ago

Hi, when building a large app with many inputs, I noticed a performance issue.

library(shiny)
library(bslib)

ui <- fluidPage(
  theme = bs_theme(bootswatch = "slate"),
  do.call("tagList", lapply(1:200, function(x) {
    selectInput(paste0("id", x), "Another input", choices = LETTERS, selectize = TRUE)
  }))
)
server <- function(input, output, session) {}
shinyApp(ui, server)

Profiling the app with

profvis::profvis(runApp('app.R'))

shows that bslib::bs_dependency() is called many times and causes the slowdown. Total time: 6580ms on my computer.

The time goes down to 1320ms if selectize = FALSE or if bslib is not used.

Related question: https://stackoverflow.com/q/67726350 (I didn't ask that question but I took its reprex)

sessionInfo()

R version 4.1.0 (2021-05-18)
Platform: x86_64-apple-darwin17.0 (64-bit)
Running under: macOS Big Sur 11.5.2

Matrix products: default
LAPACK: /Library/Frameworks/R.framework/Versions/4.1/Resources/lib/libRlapack.dylib

locale:
[1] en_US.UTF-8/en_US.UTF-8/en_US.UTF-8/C/en_US.UTF-8/en_US.UTF-8

attached base packages:
[1] stats     graphics  grDevices datasets  utils     methods   base     

other attached packages:
[1] bslib_0.3.0 shiny_1.6.0

loaded via a namespace (and not attached):
 [1] Rcpp_1.0.7        magrittr_2.0.1    xtable_1.8-4      R6_2.5.0         
 [5] rlang_0.4.11      fastmap_1.1.0     fansi_0.5.0       stringr_1.4.0    
 [9] tools_4.1.0       sessioninfo_1.1.1 utf8_1.2.2        cli_3.0.1        
[13] withr_2.4.2       jquerylib_0.1.4   htmltools_0.5.2   ellipsis_0.3.2   
[17] yaml_2.2.1        digest_0.6.27     tibble_3.1.3      lifecycle_1.0.0  
[21] crayon_1.4.1      later_1.2.0       vctrs_0.3.8       profvis_0.3.7    
[25] sass_0.4.0        htmlwidgets_1.5.3 promises_1.2.0.1  fs_1.5.0         
[29] cachem_1.0.5      mime_0.11         stringi_1.7.3     pillar_1.6.2     
[33] compiler_4.1.0    jsonlite_1.7.2    httpuv_1.6.1      renv_0.14.0      
[37] pkgconfig_2.0.3  
wch commented 3 years ago

This appears to be related to the presence of an html dependency. The same slowdown happens with sliderInput, which attaches an htmlDependency object to the HTML.

For example, this shows about 6500 milliseconds of CPU time on my computer

library(bslib)
library(shiny)
library(profvis)

ui <- fluidPage(
  theme = bs_theme(bootswatch = "slate"),
  do.call("tagList", lapply(1:200, function(x) {
    sliderInput(paste0("id", x), "Another input", 1, 100, 50)
  }))
)
app <- shinyApp(ui, server = function(input, output, session) {})
profvis(runApp(app))

If the sliderInput is replaced with something that doesn't have an htmlDependency, like textInput, then it is fast (250ms):

ui <- fluidPage(
  theme = bs_theme(bootswatch = "slate"),
  do.call("tagList", lapply(1:200, function(x) {
    textInput(paste0("id", x), "Another input")
  }))
)
app <- shinyApp(ui, server = function(input, output, session) {})
profvis(runApp(app))

Similarly, if we keep the sliderInput, but just remove the theme=bs_theme(), then it is still fast (650ms):

ui <- fluidPage(
  do.call("tagList", lapply(1:200, function(x) {
    sliderInput(paste0("id", x), "Another input", 1, 100, 50)
  }))
)
app <- shinyApp(ui, server = function(input, output, session) {})
profvis(runApp(app))

So the problem appears to be an interaction between bslib and having many html dependency objects.

github-actions[bot] commented 1 year ago

This issue has been automatically locked. If you believe you have found a related problem, please open a new issue (with a reproducible example or feature request) and link to this issue.