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 2 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 @@ -28,6 +28,8 @@

- Fixed broken vignettes, improved CSS for HTML vignettes, and reduced the file sizes.

- Faster processing of cache dependencies (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(chunk_label=options$label)
yihui marked this conversation as resolved.
Show resolved Hide resolved
}
if (options$cache < 3) {
if (options$cache.rebuild || !cache.exists) block_cache(options, res.orig, objs)
Expand Down
13 changes: 10 additions & 3 deletions R/cache.R
Original file line number Diff line number Diff line change
Expand Up @@ -142,6 +142,7 @@ 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 chunk_label The chunk label of the current code chunk.
atusy marked this conversation as resolved.
Show resolved Hide resolved
#' @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 +153,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'), chunk_label=NULL) {
yihui marked this conversation as resolved.
Show resolved Hide resolved
# 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 @@ -164,8 +165,14 @@ dep_auto = function(path = opts_chunk$get('cache.path')) {
}
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 (is.null(chunk_label)) {
if (length(nms) < 2) return(invisible(NULL))
chunk_ids <- 2:length(nms)
yihui marked this conversation as resolved.
Show resolved Hide resolved
} else {
chunk_ids <- match(chunk_label, nms)
yihui marked this conversation as resolved.
Show resolved Hide resolved
if (is.na(chunk_ids) || chunk_ids < 2) return(invisible(NULL))
}
for (i in chunk_ids) {
if (length(g <- globals[[nms[i]]]) == 0) next
for (j in 1:(i - 1L)) {
# check if current globals are in old locals
Expand Down
4 changes: 3 additions & 1 deletion man/dep_auto.Rd

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

Loading