Skip to content

Commit 7cd87a4

Browse files
committed
Memoise calls to func() made via bs_dependency_defer() (more directly addresses #369)
1 parent 5dea5e9 commit 7cd87a4

4 files changed

Lines changed: 45 additions & 14 deletions

File tree

‎DESCRIPTION‎

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -24,7 +24,9 @@ Imports:
2424
jsonlite,
2525
sass (>= 0.4.0),
2626
jquerylib (>= 0.1.3),
27-
rlang
27+
rlang,
28+
cachem,
29+
memoise
2830
Suggests:
2931
shiny (>= 1.6.0),
3032
rmarkdown (>= 2.7),

‎R/bs-dependencies.R‎

Lines changed: 28 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -246,9 +246,15 @@ bs_dependency <- function(input = list(), theme, name, version,
246246
do.call(htmlDependency, c(dep_args, .dep_args))
247247
}
248248

249-
#' @param func a _non-anonymous_ function, with a _single_ argument. This function
250-
#' should accept a [bs_theme()] object and return a single [htmlDependency()],
251-
#' a list of them, or `NULL`.
249+
#' @param func a _non-anonymous_ function, with a _single_ argument.
250+
#' This function should accept a [bs_theme()] object and return a single
251+
#' [htmlDependency()], a list of them, or `NULL`.
252+
#' @param memoise whether or not to memoise (i.e., cache) `func` results for a
253+
#' short period of time. The default, `TRUE`, can have large performance
254+
#' benefits when many instances of the same themable widget are rendered. Note
255+
#' that you may want to avoid memoisation if `func` relies on side-effects
256+
#' (e.g., files on-disk) that need to change for each themable widget
257+
#' instance.
252258
#' @export
253259
#'
254260
#' @examples
@@ -307,24 +313,37 @@ bs_dependency <- function(input = list(), theme, name, version,
307313
#' }
308314
#'
309315
#' @rdname bs_dependency
310-
bs_dependency_defer <- function(func) {
311-
force(func)
316+
bs_dependency_defer <- function(func, memoise = TRUE) {
317+
# func() most likely calls stuff like sass_file() and bs_dependency() ->
318+
# sass_partial() -> sass() (e.g., see example section above) Even though
319+
# sass() calls can be cached, there is still considerable overhead involved
320+
# in computing the cache key itself because it involves file-level operations
321+
# (e.g. getting the mtimes of file attachments).
322+
# So, to help with performance (in the case where we repeatedly call the same
323+
# func with the same theme), memoise func with a short-lived cache
324+
if (memoise) {
325+
mfunc <- memoise::memoise(func, cache = .dependency_cache)
326+
} else {
327+
mfunc <- func
328+
}
312329

313330
tagFunction(function() {
314331
if (is_shiny_app()) {
315332
# If we're in a Shiny app, do two things:
316333
# (1) Register this function as a dependency so that Shiny will know to
317-
# update it later if the theme dynamically changes. Repeated registrations
318-
# are harmless because Shiny will de-duplicate them.
334+
# update it later if the theme dynamically changes (i.e.,
335+
# session$setCurrentTheme() is called). Note that we *don't* provide
336+
# the memoised version since Shiny will de-duplicate identical()
337+
# functions anyway.
319338
# (2) Call the user's `func()` with the current theme, and return the
320339
# resulting htmlDependency so that it can be embedded in the static page.
321340
shiny::registerThemeDependency(func)
322341

323-
return(func(get_current_theme()))
342+
return(mfunc(get_current_theme()))
324343
}
325344

326345
# Outside of a Shiny context, we'll just get the global theme.
327-
func(bs_global_get())
346+
mfunc(bs_global_get())
328347
})
329348
}
330349

‎R/onLoad.R‎

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,7 @@
11
.onLoad <- function(libname, pkgname) {
22
register_upgrade_message("shiny", "1.6.0")
33
register_upgrade_message("rmarkdown", "2.7")
4+
.dependency_cache <<- cachem::cache_mem(max_age = 1)
45
}
6+
7+
.dependency_cache <- NULL

‎man/bs_dependency.Rd‎

Lines changed: 11 additions & 4 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

0 commit comments

Comments
 (0)