Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Faster processing of dependencies #2321

Merged
merged 6 commits into from
Apr 1, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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.