Skip to content

Commit

Permalink
- faster processing of dependencies
Browse files Browse the repository at this point in the history
  • Loading branch information
knokknok committed Jan 31, 2024
1 parent a14074c commit 065bac0
Show file tree
Hide file tree
Showing 3 changed files with 13 additions and 4 deletions.
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)
}
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.
#' @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) {
# 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)
} else {
chunk_ids <- match(chunk_label, nms)
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

0 comments on commit 065bac0

Please sign in to comment.