Skip to content

Commit 1f4437b

Browse files
Check for performance improvements with if() + else() instead of ifelse() (#1006)
* get started * more if + else * some more conversions * A couple more * Update token-create.R * A couple more * pre-commit * Update rules-line-breaks.R Co-authored-by: github-actions[bot] <github-actions[bot]@users.noreply.github.com>
1 parent d80d061 commit 1f4437b

11 files changed

+92
-39
lines changed

R/detect-alignment-utils.R

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -11,12 +11,12 @@ alignment_ensure_no_closing_brace <- function(pd_by_line,
1111
return(pd_by_line)
1212
}
1313
last <- last(pd_by_line)
14-
if (nrow(last) == 1) {
14+
if (nrow(last) == 1L) {
1515
# can drop last line completely
1616
pd_by_line[-length(pd_by_line)]
1717
} else {
1818
# only drop last elment of last line
19-
pd_by_line[[length(pd_by_line)]] <- last[seq2(1, nrow(last) - 1), ]
19+
pd_by_line[[length(pd_by_line)]] <- last[seq2(1L, nrow(last) - 1L), ]
2020
pd_by_line
2121
}
2222
}

R/detect-alignment.R

Lines changed: 12 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -53,16 +53,18 @@ token_is_on_aligned_line <- function(pd_flat) {
5353
if (length(pd_by_line) < 1L) {
5454
return(TRUE)
5555
}
56-
last_line_is_closing_brace_only <- nrow(last(pd_by_line)) == 1
57-
relevant_idx <- seq2(2, ifelse(last_line_is_closing_brace_only,
58-
length(pd_by_line) - 1,
56+
last_line_is_closing_brace_only <- nrow(last(pd_by_line)) == 1L
57+
last_idx <- if (last_line_is_closing_brace_only) {
58+
length(pd_by_line) - 1L
59+
} else {
5960
length(pd_by_line)
60-
))
61+
}
62+
relevant_idx <- seq2(2L, last_idx)
6163
pd_by_line <- pd_by_line[relevant_idx]
6264

6365
relevant_lag_spaces_col_1 <- map_int(pd_by_line, ~ .x$.lag_spaces[1])
6466

65-
col1_is_aligned <- length(unique(relevant_lag_spaces_col_1)) == 1
67+
col1_is_aligned <- length(unique(relevant_lag_spaces_col_1)) == 1L
6668
if (!col1_is_aligned) {
6769
return(FALSE)
6870
}
@@ -105,7 +107,11 @@ token_is_on_aligned_line <- function(pd_flat) {
105107
# over columns.
106108
n_cols <- map_int(pd_by_line, ~ sum(.x$token == "','"))
107109
previous_line <- current_col <- 0L
108-
start_eval <- ifelse(alignment_col1_all_named(pd_by_line), 1L, 2L)
110+
start_eval <- if (alignment_col1_all_named(pd_by_line)) {
111+
1L
112+
} else {
113+
2L
114+
}
109115
for (column in seq2(1L, max(n_cols))) {
110116
by_line <- alignment_serialize_column(pd_by_line, column) %>%
111117
compact() %>%

R/expr-is.R

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -75,16 +75,16 @@ is_comment <- function(pd) {
7575
#' expression (like `~column`), in the second row if it is a symmetric tilde
7676
#' expression (like `a~b`).
7777
#' @keywords internal
78-
is_tilde_expr <- function(pd, tilde_pos = c(1, 2)) {
79-
if (is.null(pd) || nrow(pd) == 1) {
78+
is_tilde_expr <- function(pd, tilde_pos = c(1L, 2L)) {
79+
if (is.null(pd) || nrow(pd) == 1L) {
8080
return(FALSE)
8181
}
8282
any(pd$token[tilde_pos] == "'~'")
8383
}
8484

8585
#' @rdname is_tilde_expr
8686
is_asymmetric_tilde_expr <- function(pd) {
87-
is_tilde_expr(pd, tilde_pos = 1)
87+
is_tilde_expr(pd, tilde_pos = 1L)
8888
}
8989

9090
#' @rdname is_tilde_expr

R/nested-to-tree.R

Lines changed: 5 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -48,10 +48,12 @@ create_tree_from_pd_with_default_style_attributes <- function(pd,
4848
#' @keywords internal
4949
create_node_from_nested_root <- function(pd_nested, structure_only) {
5050
assert_data.tree_installation()
51-
n <- data.tree::Node$new(ifelse(
52-
structure_only, "Hierarchical structure",
51+
name <- if (structure_only) {
52+
"Hierarchical structure"
53+
} else {
5354
"ROOT (token: short_text [lag_newlines/spaces] {pos_id})"
54-
))
55+
}
56+
n <- data.tree::Node$new(name)
5557
create_node_from_nested(pd_nested, n, structure_only)
5658
n
5759
}

R/relevel.R

Lines changed: 7 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -52,13 +52,16 @@ flatten_operators_one <- function(pd_nested) {
5252
#' from left or from right.
5353
#' @keywords internal
5454
flatten_pd <- function(pd_nested, token, child_token = token, left = TRUE) {
55-
token_pos_candidates <- which(pd_nested$token[-1] %in% token) + 1
55+
token_pos_candidates <- which(pd_nested$token[-1] %in% token) + 1L
5656
if (length(token_pos_candidates) == 0L) {
5757
return(pd_nested)
5858
}
59-
token_pos <- token_pos_candidates[
60-
ifelse(left, 1, length(token_pos_candidates))
61-
]
59+
token_pos_idx <- if (left) {
60+
1L
61+
} else {
62+
length(token_pos_candidates)
63+
}
64+
token_pos <- token_pos_candidates[token_pos_idx]
6265
if (left) {
6366
pos <- previous_non_comment(pd_nested, token_pos)
6467
} else {

R/rules-line-breaks.R

Lines changed: 13 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -65,11 +65,12 @@ set_line_break_before_curly_opening <- function(pd) {
6565
~ next_terminal(pd[.x, ], vars = "token_after")$token_after
6666
) != "'{'"
6767
last_expr_idx <- max(which(pd$token == "expr"))
68-
is_last_expr <- ifelse(any(c("IF", "WHILE") == pd$token[1]),
68+
is_last_expr <- if (any(c("IF", "WHILE") == pd$token[1])) {
6969
# rule not applicable for if and while
70-
TRUE, (line_break_to_set_idx + 1L) == last_expr_idx
71-
)
72-
70+
TRUE
71+
} else {
72+
(line_break_to_set_idx + 1L) == last_expr_idx
73+
}
7374
no_line_break_before_curly_idx <- any(pd$token[line_break_to_set_idx] == "EQ_SUB")
7475
linebreak_before_curly <- ifelse(is_function_call(pd),
7576
# if in function call and has pipe, it is not recognized as function call
@@ -338,7 +339,11 @@ set_line_break_after_opening_if_call_is_multi_line <- function(pd,
338339
#' @keywords internal
339340
find_line_break_position_in_multiline_call <- function(pd) {
340341
candidate <- (which(pd$token == "EQ_SUB") - 1L)[1]
341-
ifelse(is.na(candidate), 3L, candidate)
342+
if (is.na(candidate)) {
343+
3L
344+
} else {
345+
candidate
346+
}
342347
}
343348

344349

@@ -368,10 +373,10 @@ remove_line_break_in_fun_call <- function(pd, strict) {
368373
# no blank lines within function calls
369374
if (strict) {
370375
pd$lag_newlines[
371-
lag(pd$token == "','") & pd$lag_newlines > 1 & pd$token != "COMMENT"
376+
lag(pd$token == "','") & pd$lag_newlines > 1L & pd$token != "COMMENT"
372377
] <- 1L
373378
}
374-
if (nrow(pd) == 3) {
379+
if (nrow(pd) == 3L) {
375380
pd$lag_newlines[3] <- 0L
376381
}
377382
}
@@ -386,7 +391,7 @@ set_linebreak_after_ggplot2_plus <- function(pd) {
386391
first_plus <- which(is_plus_raw)[1]
387392
next_non_comment <- next_non_comment(pd, first_plus)
388393
is_plus_or_comment_after_plus_before_fun_call <-
389-
lag(is_plus_raw, next_non_comment - first_plus - 1, default = FALSE) &
394+
lag(is_plus_raw, next_non_comment - first_plus - 1L, default = FALSE) &
390395
(pd$token_after == "SYMBOL_FUNCTION_CALL" | pd$token_after == "SYMBOL_PACKAGE")
391396
if (any(is_plus_or_comment_after_plus_before_fun_call, na.rm = TRUE)) {
392397
gg_call <- pd$child[[previous_non_comment(pd, first_plus)]]$child[[1]]

R/set-assert-args.R

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -25,10 +25,11 @@ assert_transformers <- function(transformers) {
2525
no_name <- is.null(transformers$style_guide_name)
2626
no_version <- is.null(transformers$style_guide_version)
2727
if (no_name || no_version) {
28-
action <- ifelse(utils::packageVersion("styler") >= version_cutoff,
29-
"are not supported anymore",
28+
action <- if (utils::packageVersion("styler") >= version_cutoff) {
29+
"are not supported anymore"
30+
} else {
3031
"depreciated and will be removed in a future version of styler."
31-
)
32+
}
3233
message <- paste(
3334
"Style guides without a name and a version field are",
3435
action, "\nIf you are a user: Open an issue on",

R/style-guides.R

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -490,7 +490,7 @@ scope_normalize <- function(scope, name = substitute(scope)) {
490490

491491
if (inherits(scope, "AsIs")) {
492492
factor(as.character(scope), levels = levels, ordered = TRUE)
493-
} else if (length(scope) == 1) {
493+
} else if (length(scope) == 1L) {
494494
scope <- levels[as.logical(rev(cumsum(scope == rev(levels))))]
495495
factor(scope, levels = levels, ordered = TRUE)
496496
} else {

R/token-create.R

Lines changed: 30 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -78,7 +78,11 @@ create_tokens <- function(tokens,
7878
#' @family token creators
7979
#' @keywords internal
8080
create_pos_ids <- function(pd, pos, by = 0.1, after = FALSE, n = 1L) {
81-
direction <- ifelse(after, 1L, -1L)
81+
direction <- if (after) {
82+
1L
83+
} else {
84+
-1L
85+
}
8286
first <- find_start_pos_id(pd, pos, by, direction, after)
8387
new_ids <- seq(first,
8488
to = first + direction * (n - 1) * by, by = by * direction
@@ -105,13 +109,28 @@ find_start_pos_id <- function(pd,
105109
candidates = NULL) {
106110
candidates <- append(candidates, pd$pos_id[pos])
107111
if (is.null(pd$child[[pos]])) {
108-
ifelse(after, max(candidates), min(candidates)) + by * direction
112+
start_pos_idx <- if (after) {
113+
max(candidates)
114+
} else {
115+
min(candidates)
116+
}
117+
start_pos_idx <- start_pos_idx + (by * direction)
109118
} else {
110-
find_start_pos_id(
111-
pd$child[[pos]], ifelse(after, nrow(pd$child[[pos]]), 1L),
112-
by, direction, after, candidates
119+
start_pos_idx <- find_start_pos_id(
120+
pd$child[[pos]],
121+
if (after) {
122+
nrow(pd$child[[pos]])
123+
} else {
124+
1L
125+
},
126+
by,
127+
direction,
128+
after,
129+
candidates
113130
)
114131
}
132+
133+
start_pos_idx
115134
}
116135

117136

@@ -129,7 +148,12 @@ find_start_pos_id <- function(pd,
129148
#' @family token creators
130149
#' @keywords internal
131150
validate_new_pos_ids <- function(new_ids, after) {
132-
ref <- ifelse(after, floor(new_ids), ceiling(new_ids))
151+
ref <- if (after) {
152+
floor(new_ids)
153+
} else {
154+
ceiling(new_ids)
155+
}
156+
133157
if (any(abs(new_ids - ref) > 0.5)) abort("too many ids assigned.")
134158
}
135159

R/transform-files.R

Lines changed: 14 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -66,7 +66,15 @@ transform_file <- function(path,
6666
}
6767
changed <- transform_code(path, fun = fun, ..., dry = dry)
6868

69-
bullet <- ifelse(is.na(changed), "warning", ifelse(changed, "info", "tick"))
69+
bullet <- if (is.na(changed)) {
70+
"warning"
71+
} else {
72+
if (changed) {
73+
"info"
74+
} else {
75+
"tick"
76+
}
77+
}
7078

7179
if (!getOption("styler.quiet", FALSE)) {
7280
cli::cat_bullet(bullet = bullet)
@@ -208,7 +216,11 @@ split_roxygen_segments <- function(text, roxygen_examples) {
208216
active_segment <- as.integer(all_lines %in% roxygen_examples)
209217
segment_id <- cumsum(abs(c(0L, diff(active_segment)))) + 1L
210218
separated <- split(text, factor(segment_id))
211-
restyle_selector <- ifelse(roxygen_examples[1] == 1L, odd_index, even_index)
219+
restyle_selector <- if (roxygen_examples[1] == 1L) {
220+
odd_index
221+
} else {
222+
even_index
223+
}
212224

213225
list(separated = separated, selectors = restyle_selector(separated))
214226
}

0 commit comments

Comments
 (0)