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

Parallel styling #682

Draft
wants to merge 4 commits into
base: main
Choose a base branch
from
Draft
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
1 change: 1 addition & 0 deletions .github/workflows/R-CMD-check.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,7 @@ jobs:

env:
R_REMOTES_NO_ERRORS_FROM_WARNINGS: true
R_STYLER_FUTURE_NO_OVERRIDE: true
RSPM: ${{ matrix.config.rspm }}
GITHUB_PAT: ${{ secrets.GITHUB_TOKEN }}
_R_CHECK_FORCE_SUGGESTS_: false
Expand Down
1 change: 1 addition & 0 deletions .github/workflows/benchmarking.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@ jobs:

env:
R_REMOTES_NO_ERRORS_FROM_WARNINGS: true
R_STYLER_FUTURE_NO_OVERRIDE: true
RSPM: ${{ matrix.config.rspm }}
GITHUB_PAT: ${{ secrets.GITHUB_TOKEN }}
steps:
Expand Down
1 change: 1 addition & 0 deletions .github/workflows/test-coverage.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@ jobs:
runs-on: macOS-latest
env:
GITHUB_PAT: ${{ secrets.GITHUB_TOKEN }}
R_STYLER_FUTURE_NO_OVERRIDE: true
steps:
- uses: actions/checkout@v2

Expand Down
3 changes: 3 additions & 0 deletions DESCRIPTION
Original file line number Diff line number Diff line change
Expand Up @@ -33,6 +33,8 @@ Suggests:
data.tree (>= 0.1.6),
digest,
dplyr,
furrr,
future,
here,
knitr,
prettycode,
Expand All @@ -56,6 +58,7 @@ Collate:
'detect-alignment.R'
'environments.R'
'expr-is.R'
'future.R'
'indent.R'
'initialize.R'
'io.R'
Expand Down
1 change: 1 addition & 0 deletions NAMESPACE
Original file line number Diff line number Diff line change
Expand Up @@ -40,6 +40,7 @@ importFrom(purrr,when)
importFrom(rlang,abort)
importFrom(rlang,is_empty)
importFrom(rlang,is_installed)
importFrom(rlang,local_options)
importFrom(rlang,seq2)
importFrom(rlang,warn)
importFrom(rlang,with_handlers)
Expand Down
20 changes: 20 additions & 0 deletions R/future.R
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
#' Parallelization of styling
#'
#' [style_file()], [style_pkg()] and [style_dir()] leverage the `{future}`
#' framework for parallelization. To make use of parallel processing, you need
#' to have the package `{furrr}` installed. In that case, the strategy
#' [future::multiprocess()] is set temporarily during styling (only if there is
#' more than two files to style, because of start-up costs of parallelization).
#' You can prevent this temporary change in the future strategy by setting the
#' environment variable `R_STYLER_FUTURE_NO_OVERRIDE` to `TRUE` (case ignored). In
#' that case, the future strategy set before calling styler will be used, if
#' you have not set any, the future `{framework}` falls back to
#' [future::sequential()].
#'
#' @section Life-cycle:
#' The parallelization feature is experimental, in particular how its governed
#' with `R_STYLER_FUTURE_NO_OVERRIDE` and how progress bars are displayed with
#' `{furrr}`. In the future, most likely the `{progressr}` package will be used
#' to handle progress bars.
#' @name styler_future
NULL
35 changes: 31 additions & 4 deletions R/transform-files.R
Original file line number Diff line number Diff line change
Expand Up @@ -11,17 +11,44 @@
#' styling whether or not it was actually changed (or would be changed when
#' `dry` is not "off").
#' @keywords internal
#' @importFrom rlang local_options
transform_files <- function(files, transformers, include_roxygen_examples, base_indention, dry) {
transformer <- make_transformer(transformers, include_roxygen_examples, base_indention)
max_char <- min(max(nchar(files), 0), getOption("width"))
len_files <- length(files)
if (len_files > 0L && !getOption("styler.quiet", FALSE)) {
cat("Styling ", len_files, " files:\n")
cat("Styling ", len_files, " files:\n") # TODO should only have one space between words
}

future_strategy_no_override <- tolower(Sys.getenv("R_STYLER_FUTURE_NO_OVERRIDE", "FALSE")) == "true"
if (rlang::is_installed("furrr")) {
# only parallelise when furrr is installed
if (!future_strategy_no_override && length(files) > 2) {
# only override when env not set and more than two files.
local_options(future.supportsMulticore.unstable = "quiet")
oplan <- future::plan("multisession") # not sure we should use

# withr::defer() gives C stack overflow on macOS, on.exit not.
on.exit(future::plan(oplan), add = TRUE, after = FALSE)
}
changed <- furrr::future_map_lgl(files, transform_file,
fun = transformer, max_char_path = max_char, dry = dry,
.progress = TRUE
)
} else {
if (future_strategy_no_override) {
rlang::warn(paste(
"Environment variable `R_STYLER_FUTURE_NO_OVERRIDE` is not respected",
"because package {furrr} is not installed. Falling back to ",
"`purrr::map_lgl()` to iterate over files to style instead of",
"`furrr::future_map_lgl()`."
))
}
changed <- map_lgl(files, transform_file,
fun = transformer, max_char_path = max_char, dry = dry
)
}

changed <- map_lgl(files, transform_file,
fun = transformer, max_char_path = max_char, dry = dry
)
communicate_summary(changed, max_char)
communicate_warning(changed, transformers)
new_tibble(list(file = files, changed = changed), nrow = len_files)
Expand Down
5 changes: 5 additions & 0 deletions inst/WORDLIST
Original file line number Diff line number Diff line change
Expand Up @@ -58,6 +58,7 @@ filetype
forcond
formatter
funct
furrr
gcc
getChecksum
getOption
Expand Down Expand Up @@ -100,6 +101,7 @@ macOS
magrittr
md
Müller
multiprocess
mutli
na
navbar
Expand All @@ -111,6 +113,8 @@ ourself
packagemanager
packrat
pandoc
parallelization
Parallelization
params
paren
parsable
Expand All @@ -124,6 +128,7 @@ pre
precommit
prefill
prettycode
progressr
PRs
purrr
rcmdcheck
Expand Down
25 changes: 25 additions & 0 deletions man/styler_future.Rd

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

53 changes: 53 additions & 0 deletions tests/testthat/test-future.R
Original file line number Diff line number Diff line change
@@ -0,0 +1,53 @@
path_rel <- c(
"dirty-sample-with-scope-tokens.R",
"dirty-sample-with-scope-spaces.R",
"clean-sample-with-scope-tokens.R"
)

path_dir <- file.path(
"public-api", "xyzdir-dirty", path_rel
)

test_that("can style in parallel when no strategy is set and overriding is deactivated", {
withr::local_envvar("R_STYLER_FUTURE_NO_OVERRIDE" = "TRUE")
expect_match(
catch_style_file_output(path_dir),
'Styling +3',
all = FALSE
)
})

test_that("can style in parallel when no strategy is set and overriding is not deactivated", {
withr::local_envvar("R_STYLER_FUTURE_NO_OVERRIDE" = "FALSE")
expect_match(
catch_style_file_output(path_dir),
'Styling +3',
all = FALSE
)
})

test_that("can style in parallel when no strategy is set", {

expect_match(
catch_style_file_output(path_dir),
'Styling +3',
all = FALSE
)
})

test_that("can style in parallel when strategy is set", {
future::plan('sequential')
expect_match(
catch_style_file_output(path_dir),
'Styling +3',
all = FALSE
)
})

test_that("with fewer than two files, the plan is not modified", {
expect_match(
catch_style_file_output(path_dir[1]),
'Styling +1',
all = FALSE
)
})