Skip to content

Commit

Permalink
cache on expression level, part 1
Browse files Browse the repository at this point in the history
Not really active on expression level, just some infrastructure and code running. Must now process each block separately, i.e. parse_transform_serialize and parse_transform_serialize_block from branch caching@48a9a6
  • Loading branch information
lorenzwalthert committed Jan 17, 2020
1 parent 4140c6d commit 584df24
Show file tree
Hide file tree
Showing 14 changed files with 115 additions and 23 deletions.
7 changes: 5 additions & 2 deletions R/nest.R
Original file line number Diff line number Diff line change
Expand Up @@ -5,10 +5,13 @@
#' @param text A character vector to parse.
#' @return A nested parse table. See [tokenize()] for details on the columns
#' of the parse table.
#' @inheritParams tokenize
#' @importFrom purrr when
#' @keywords internal
compute_parse_data_nested <- function(text) {
parse_data <- tokenize(text) %>%
compute_parse_data_nested <- function(text,
transformers = NULL,
cache_dir = NULL) {
parse_data <- tokenize(text, transformers, cache_dir) %>%
add_terminal_token_before() %>%
add_terminal_token_after() %>%
add_stylerignore()
Expand Down
33 changes: 29 additions & 4 deletions R/parse.R
Original file line number Diff line number Diff line change
Expand Up @@ -71,31 +71,56 @@ has_crlf_as_first_line_sep <- function(message, initial_text) {
#' * A column "child" that contains *nest*s.
#'
#' @param text A character vector.
#' @inheritParams get_parse_data
#' @return A flat parse table
#' @importFrom rlang seq2
#' @keywords internal
tokenize <- function(text) {
get_parse_data(text, include_text = NA) %>%
tokenize <- function(text,
transformers = NULL,
cache_dir = NULL) {
get_parse_data(text,
include_text = NA,
transformers = transformers,
cache_dir = cache_dir
) %>%
ensure_correct_str_txt(text) %>%
enhance_mapping_special()
}

#' Obtain robust parse data
#'
#' Wrapper around `utils::getParseData(parse(text = text))` that returns a flat
#' parse table.
#' parse table. When caching information should be added, make sure that
#' the cache is activated with `cache_activate()` and both `transformers` and
#' `cache_dir` are non-`NULL`.
#' @param text The text to parse.
#' @param include_text Passed to [utils::getParseData()] as `includeText`.
#' @param ... Other arguments passed to [utils::getParseData()].
#' @param transformers A list of transformer functions. Used only to determine
#' if the code to style was previously cached.
#' @param cache_dir The directory to look for the cache.
#' @keywords internal
get_parse_data <- function(text, include_text = TRUE, ...) {
get_parse_data <- function(text,
include_text = TRUE,
transformers = NULL,
cache_dir = NULL,
...) {
# avoid https://bugs.r-project.org/bugzilla3/show_bug.cgi?id=16041
parse_safely(text, keep.source = TRUE)
parsed <- parse_safely(text, keep.source = TRUE)
pd <- as_tibble(
utils::getParseData(parsed, includeText = include_text),
.name_repair = "minimal") %>%
add_id_and_short()

pd$block <- seq_len(nrow(pd))
pd$is_cached <- NA
top_level_exprs <- which(pd$parent <= 0)
if (cache_is_activated() && !is.null(cache_dir) && !is.null(transformers)) {
for (idx in top_level_exprs) {
pd$is_cached[idx] <- is_cached(pd$text[idx], transformers, cache_dir)
}
}
parser_version_set(parser_version_find(pd))
pd
}
Expand Down
8 changes: 6 additions & 2 deletions R/token-create.R
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,9 @@ create_tokens <- function(tokens,
indents = 0,
terminal = TRUE,
child = NULL,
stylerignore = FALSE) {
stylerignore = FALSE,
block = NA,
is_cached = NA) {
len_text <- length(texts)
new_tibble(
list(
Expand All @@ -50,7 +52,9 @@ create_tokens <- function(tokens,
indention_ref_pos_id = indention_ref_pos_ids,
indent = indents,
child = rep(list(child), len_text),
stylerignore = stylerignore
stylerignore = stylerignore,
block = block,
is_cached = is_cached
),
nrow = len_text
)
Expand Down
8 changes: 2 additions & 6 deletions R/transform-files.R
Original file line number Diff line number Diff line change
Expand Up @@ -87,11 +87,7 @@ make_transformer <- function(transformers,
should_use_cache <- is_R.cache_installed && cache_is_activated()

if (should_use_cache) {
use_cache <- R.cache::generateCache(
key = cache_make_key(text, transformers),
dirs = cache_dir
) %>%
file.exists()
use_cache <- is_cached(text, transformers, cache_dir)
} else {
use_cache <- FALSE
}
Expand Down Expand Up @@ -197,7 +193,7 @@ split_roxygen_segments <- function(text, roxygen_examples) {
#' @keywords internal
parse_transform_serialize_r <- function(text, transformers, warn_empty = TRUE) {
text <- assert_text(text)
pd_nested <- compute_parse_data_nested(text)
pd_nested <- compute_parse_data_nested(text, transformers)
start_line <- find_start_line(pd_nested)
if (nrow(pd_nested) == 0) {
if (warn_empty) {
Expand Down
17 changes: 17 additions & 0 deletions R/utils-cache.R
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,23 @@ hash_standardize <- function(text) {
list()
}

#' Check if text is cached
#'
#' This boilds down to check if the hash exists at the caching dir as a file.
#' @param text,transformers Passed to [cache_make_key()] to generate a key.
#' @param cache_dir The caching directory relative to the `.Rcache` root to
#' look for a cached value.
#' @keywords internal
is_cached <- function(text, transformers, cache_dir) {
R.cache::generateCache(
key = cache_make_key(text, transformers),
dirs = cache_dir
) %>%
file.exists()

}


#' Make a key for `R.cache`
#'
#' This is used to determine if caching already corresponds to a style guide.
Expand Down
7 changes: 6 additions & 1 deletion man/compute_parse_data_nested.Rd

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

4 changes: 3 additions & 1 deletion man/create_tokens.Rd

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

17 changes: 15 additions & 2 deletions man/get_parse_data.Rd

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

18 changes: 18 additions & 0 deletions man/is_cached.Rd

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

3 changes: 2 additions & 1 deletion man/parse_transform_serialize_r.Rd

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

3 changes: 2 additions & 1 deletion man/style_roxygen_code_example.Rd

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

3 changes: 2 additions & 1 deletion man/style_roxygen_code_example_segment.Rd

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

3 changes: 2 additions & 1 deletion man/style_roxygen_example_snippet.Rd

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

7 changes: 6 additions & 1 deletion man/tokenize.Rd

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

0 comments on commit 584df24

Please sign in to comment.