Skip to content

Commit 4075099

Browse files
Merge pull request #979 from r-lib/f-558-speed
Optimize visiting code
2 parents 88ee57c + 2fde44d commit 4075099

9 files changed

+83
-16
lines changed

R/initialize.R

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,7 @@
1010
#' {
1111
#' string_to_format <- "call( 3)"
1212
#' pd <- styler:::compute_parse_data_nested(string_to_format)
13-
#' styler:::pre_visit(pd, c(default_style_guide_attributes))
13+
#' styler:::pre_visit_one(pd, default_style_guide_attributes)
1414
#' }
1515
#' )
1616
#' @export

R/nested-to-tree.R

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,7 @@
88
#' @keywords internal
99
create_tree <- function(text, structure_only = FALSE) {
1010
compute_parse_data_nested(text, transformers = NULL) %>%
11-
pre_visit(c(default_style_guide_attributes)) %>%
11+
pre_visit_one(default_style_guide_attributes) %>%
1212
create_tree_from_pd_with_default_style_attributes(structure_only)
1313
}
1414

@@ -36,8 +36,8 @@ create_tree_from_pd_with_default_style_attributes <- function(pd,
3636
#' {
3737
#' code <- "a <- function(x) { if(x > 1) { 1+1 } else {x} }"
3838
#' nested_pd <- styler:::compute_parse_data_nested(code)
39-
#' initialized <- styler:::pre_visit(
40-
#' nested_pd, c(default_style_guide_attributes)
39+
#' initialized <- styler:::pre_visit_one(
40+
#' nested_pd, default_style_guide_attributes
4141
#' )
4242
#' styler:::create_node_from_nested_root(initialized,
4343
#' structure_only = FALSE

R/relevel.R

Lines changed: 2 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -12,8 +12,7 @@
1212
#' @param pd_nested A nested parse table to partially flatten.
1313
#' @keywords internal
1414
flatten_operators <- function(pd_nested) {
15-
pd_nested %>%
16-
post_visit(c(flatten_operators_one))
15+
post_visit_one(pd_nested, flatten_operators_one)
1716
}
1817

1918
#' Flatten one level of nesting with its child
@@ -144,8 +143,7 @@ wrap_expr_in_expr <- function(pd) {
144143
#' @keywords internal
145144
relocate_eq_assign <- function(pd) {
146145
if (parser_version_get() < 2) {
147-
pd %>%
148-
post_visit(c(relocate_eq_assign_nest))
146+
post_visit_one(pd, relocate_eq_assign_nest)
149147
} else {
150148
pd
151149
}

R/transform-block.R

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -28,7 +28,7 @@ parse_transform_serialize_r_block <- function(pd_nested,
2828
base_indention) {
2929
if (!all(pd_nested$is_cached, na.rm = TRUE) || !cache_is_activated()) {
3030
transformed_pd <- apply_transformers(pd_nested, transformers)
31-
flattened_pd <- post_visit(transformed_pd, list(extract_terminals)) %>%
31+
flattened_pd <- post_visit_one(transformed_pd, extract_terminals) %>%
3232
enrich_terminals(transformers$use_raw_indention) %>%
3333
apply_ref_indention() %>%
3434
set_regex_indention(

R/visit.R

Lines changed: 66 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -20,9 +20,38 @@ pre_visit <- function(pd_nested, funs) {
2020
if (is.null(pd_nested)) {
2121
return()
2222
}
23+
if (length(funs) == 0) {
24+
return(pd_nested)
25+
}
2326
pd_nested <- visit_one(pd_nested, funs)
2427

25-
pd_nested$child <- map(pd_nested$child, pre_visit, funs = funs)
28+
children <- pd_nested$child
29+
for (i in seq_along(children)) {
30+
child <- children[[i]]
31+
if (!is.null(child)) {
32+
children[[i]] <- pre_visit(child, funs)
33+
}
34+
}
35+
pd_nested$child <- children
36+
pd_nested
37+
}
38+
39+
#' @rdname visit
40+
#' @keywords internal
41+
pre_visit_one <- function(pd_nested, fun) {
42+
if (is.null(pd_nested)) {
43+
return()
44+
}
45+
pd_nested <- fun(pd_nested)
46+
47+
children <- pd_nested$child
48+
for (i in seq_along(children)) {
49+
child <- children[[i]]
50+
if (!is.null(child)) {
51+
children[[i]] <- pre_visit_one(child, fun)
52+
}
53+
}
54+
pd_nested$child <- children
2655
pd_nested
2756
}
2857

@@ -32,11 +61,42 @@ post_visit <- function(pd_nested, funs) {
3261
if (is.null(pd_nested)) {
3362
return()
3463
}
64+
if (length(funs) == 0) {
65+
return(pd_nested)
66+
}
67+
68+
children <- pd_nested$child
69+
for (i in seq_along(children)) {
70+
child <- children[[i]]
71+
if (!is.null(child)) {
72+
children[[i]] <- post_visit(child, funs)
73+
}
74+
}
75+
pd_nested$child <- children
3576

36-
pd_nested$child <- map(pd_nested$child, post_visit, funs = funs)
3777
visit_one(pd_nested, funs)
3878
}
3979

80+
#' @rdname visit
81+
#' @keywords internal
82+
post_visit_one <- function(pd_nested, fun) {
83+
if (is.null(pd_nested)) {
84+
return()
85+
}
86+
force(fun)
87+
88+
children <- pd_nested$child
89+
for (i in seq_along(children)) {
90+
child <- children[[i]]
91+
if (!is.null(child)) {
92+
children[[i]] <- post_visit_one(child, fun)
93+
}
94+
}
95+
pd_nested$child <- children
96+
97+
fun(pd_nested)
98+
}
99+
40100
#' Transform a flat parse table with a list of transformers
41101
#'
42102
#' Uses [Reduce()] to apply each function of `funs` sequentially to
@@ -46,7 +106,10 @@ post_visit <- function(pd_nested, funs) {
46106
#' @family visitors
47107
#' @keywords internal
48108
visit_one <- function(pd_flat, funs) {
49-
Reduce(function(x, fun) fun(x), funs, init = pd_flat)
109+
for (f in funs) {
110+
pd_flat <- f(pd_flat)
111+
}
112+
pd_flat
50113
}
51114

52115
#' Propagate context to terminals

man/create_node_from_nested_root.Rd

Lines changed: 2 additions & 2 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

man/default_style_guide_attributes.Rd

Lines changed: 1 addition & 1 deletion
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

man/visit.Rd

Lines changed: 6 additions & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

vignettes/customizing_styler.Rmd

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -40,7 +40,7 @@ As the name says, this function removes spaces after the opening parenthesis. Bu
4040
```{r}
4141
string_to_format <- "call( 3)"
4242
pd <- styler:::compute_parse_data_nested(string_to_format) %>%
43-
styler:::pre_visit(c(default_style_guide_attributes))
43+
styler:::pre_visit_one(default_style_guide_attributes)
4444
pd$child[[1]] %>%
4545
select(token, terminal, text, newlines, spaces)
4646
```

0 commit comments

Comments
 (0)