Skip to content

Commit

Permalink
fix #2318: faster processing of dependencies in dep_auto() (#2321)
Browse files Browse the repository at this point in the history
Co-authored-by: knokknok <>
Co-authored-by: Yihui Xie <xie@yihui.name>
Co-authored-by: atusy <30277794+atusy@users.noreply.github.com>
  • Loading branch information
3 people committed Apr 1, 2024
1 parent 74bcff8 commit f36e52c
Show file tree
Hide file tree
Showing 4 changed files with 14 additions and 8 deletions.
2 changes: 2 additions & 0 deletions NEWS.md
Original file line number Diff line number Diff line change
Expand Up @@ -42,6 +42,8 @@

- `spin()` dropped support for `#-` as the chunk delimiter token. Please use `#+` or `# %%` or `#|` instead.

- Faster processing of cache dependencies in `dep_auto()` (thanks, @knokknok, #2318).

# CHANGES IN knitr VERSION 1.45

## NEW FEATURES
Expand Down
2 changes: 1 addition & 1 deletion R/block.R
Original file line number Diff line number Diff line change
Expand Up @@ -345,7 +345,7 @@ eng_r = function(options) {
objs, cache_globals(options$cache.globals, code), options$label,
options$cache.path
)
dep_auto()
dep_auto(labels = options$label)
}
if (options$cache < 3) {
if (options$cache.rebuild || !cache.exists) block_cache(options, res.orig, objs)
Expand Down
13 changes: 7 additions & 6 deletions R/cache.R
Original file line number Diff line number Diff line change
Expand Up @@ -142,6 +142,8 @@ cache_rx = '_[abcdef0123456789]{32}[.](rdb|rdx|RData)$'
#' is similar to the effect of the \code{dependson} option. It is supposed to be
#' used in the first chunk of a document and this chunk must not be cached.
#' @param path Path to the dependency file.
#' @param labels A vector of labels of chunks for which the dependencies will be
#' built. By default, dependencies for all chunks will be built.
#' @return \code{NULL}. The dependencies are built as a side effect.
#' @note Be cautious about \code{path}: because this function is used in a
#' chunk, the working directory when the chunk is evaluated is the directory
Expand All @@ -152,7 +154,7 @@ cache_rx = '_[abcdef0123456789]{32}[.](rdb|rdx|RData)$'
#' @export
#' @seealso \code{\link{dep_prev}}
#' @references \url{https://yihui.org/knitr/demo/cache/}
dep_auto = function(path = opts_chunk$get('cache.path')) {
dep_auto = function(path = opts_chunk$get('cache.path'), labels = all_labels()) {
# this function should be evaluated in the original working directory
owd = setwd(opts_knit$get('output.dir')); on.exit(setwd(owd))
paths = valid_path(path, c('__objects', '__globals'))
Expand All @@ -162,11 +164,10 @@ dep_auto = function(path = opts_chunk$get('cache.path')) {
warning('corrupt dependency files? \ntry remove ', paste(paths, collapse = '; '))
return(invisible(NULL))
}
nms = intersect(names(knit_code$get()), names(locals)) # guarantee correct order
# locals may contain old chunk names; the intersection can be of length < 2
if (length(nms) < 2) return(invisible(NULL))
for (i in 2:length(nms)) {
if (length(g <- globals[[nms[i]]]) == 0) next
nms = intersect(all_labels(), names(locals)) # guarantee correct order
for (i in match(labels, nms)) {
# ignore first chunk (i < 2); locals may contain old chunk names (i will be NA)
if (is.na(i) || i < 2 || length(g <- globals[[nms[i]]]) == 0) next
for (j in 1:(i - 1L)) {
# check if current globals are in old locals
if (any(g %in% locals[[nms[j]]]))
Expand Down
5 changes: 4 additions & 1 deletion man/dep_auto.Rd

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

0 comments on commit f36e52c

Please sign in to comment.