Skip to content

Commit 3460a55

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

3 files changed

Lines changed: 23 additions & 7 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: 17 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -246,9 +246,11 @@ 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_ **pure** 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`. **IMPORTANT**: the result of
252+
#' this function should be solely determined by it's input value (i.e., it
253+
#' must be a pure function).
252254
#' @export
253255
#'
254256
#' @examples
@@ -308,7 +310,16 @@ bs_dependency <- function(input = list(), theme, name, version,
308310
#'
309311
#' @rdname bs_dependency
310312
bs_dependency_defer <- function(func) {
311-
force(func)
313+
# func() most likely calls stuff like sass_file() and bs_dependency() ->
314+
# sass_partial() -> sass() (e.g., see example section above). Even though
315+
# sass() calls can be cached, we still have two performance issues:
316+
# * Computing the cache key itself is not necessarily cheap since it
317+
# involves file-level operations like getting the mtimes of file attachments
318+
# * It doesn't generally make much sense to cache sass::sass_file(),
319+
# which also does some file-level operations.
320+
# So, to help with performance (in the case where we repeatedly call the same
321+
# func with the same theme), memoise func with a short-lived cache
322+
mfunc <- memoise::memoise(func, cache = .dependency_cache)
312323

313324
tagFunction(function() {
314325
if (is_shiny_app()) {
@@ -320,11 +331,11 @@ bs_dependency_defer <- function(func) {
320331
# resulting htmlDependency so that it can be embedded in the static page.
321332
shiny::registerThemeDependency(func)
322333

323-
return(func(get_current_theme()))
334+
return(mfunc(get_current_theme()))
324335
}
325336

326337
# Outside of a Shiny context, we'll just get the global theme.
327-
func(bs_global_get())
338+
mfunc(bs_global_get())
328339
})
329340
}
330341

‎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

0 commit comments

Comments
 (0)