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

Replace tibbles with data frames to improve performance #1007

Merged
merged 12 commits into from
Sep 27, 2022
6 changes: 3 additions & 3 deletions R/compat-dplyr.R
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,7 @@ arrange_pos_id <- function(data) {

bind_rows <- function(x, y = NULL, ...) {
if (is.null(x) && is.null(y)) {
return(new_tibble(list()))
return(new_styler_df(list()))
}
if (is.null(x)) {
if (inherits(y, "data.frame")) {
Expand Down Expand Up @@ -62,8 +62,8 @@ left_join <- function(x, y, by) {

res <- merge(x, y, by.x = by_x, by.y = by_y, all.x = TRUE, sort = FALSE) %>%
arrange_pos_id()
res <- new_tibble(res) %>%
as.data.frame()
res <- new_styler_df(res) %>%
styler_df()
# dplyr::left_join set unknown list columns to NULL, merge sets them
# to NA
if (exists("child", res) && anyNA(res$child)) {
Expand Down
3 changes: 1 addition & 2 deletions R/compat-tidyr.R
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,5 @@ nest_ <- function(data, key_col, nest_cols = character()) {
res <- list()
res[[key_column]] <- key_levels
res[[key_col]] <- split(data[, nest_cols], key_factor)
new_tibble(res) %>%
as.data.frame()
new_styler_df(res)
}
10 changes: 4 additions & 6 deletions R/nest.R
Original file line number Diff line number Diff line change
Expand Up @@ -249,13 +249,12 @@ add_terminal_token_after <- function(pd_flat) {
filter(terminal) %>%
arrange_pos_id()

rhs <- new_tibble(
rhs <- new_styler_df(
list(
pos_id = terminals$pos_id,
token_after = lead(terminals$token, default = "")
)
) %>%
as.data.frame()
)

left_join(pd_flat, rhs, by = "pos_id")
}
Expand All @@ -267,13 +266,12 @@ add_terminal_token_before <- function(pd_flat) {
filter(terminal) %>%
arrange_pos_id()

rhs <- new_tibble(
rhs <- new_styler_df(
list(
id = terminals$id,
token_before = lag(terminals$token, default = "")
)
) %>%
as.data.frame()
)

left_join(pd_flat, rhs, by = "id")
}
Expand Down
1 change: 1 addition & 0 deletions R/nested-to-tree.R
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@ create_tree_from_pd_with_default_style_attributes <- function(pd,
structure_only = FALSE) {
pd %>%
create_node_from_nested_root(structure_only) %>%
# don't use `styler_df()` here; `vctrs::data_frame()` only accepts a vector, not a <Node/R6> object
as.data.frame()
}

Expand Down
7 changes: 3 additions & 4 deletions R/parse.R
Original file line number Diff line number Diff line change
Expand Up @@ -94,7 +94,7 @@ get_parse_data <- function(text, include_text = TRUE, ...) {
# 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(
pd <- styler_df(
utils::getParseData(parsed, includeText = include_text),
.name_repair = "minimal"
)
Expand All @@ -112,7 +112,7 @@ get_parse_data <- function(text, include_text = TRUE, ...) {
add_id_and_short()

parser_version_set(parser_version_find(pd))
as.data.frame(pd)
pd
}

#' Add column `pos_id` and `short`
Expand Down Expand Up @@ -163,8 +163,7 @@ ensure_correct_txt <- function(pd, text) {
by.y = "id",
suffixes = c("", "parent")
) %>%
as_tibble(.name_repair = "minimal") %>%
as.data.frame()
styler_df(.name_repair = "minimal")

if (!lines_and_cols_match(new_text)) {
abort(paste(
Expand Down
2 changes: 1 addition & 1 deletion R/style-guides.R
Original file line number Diff line number Diff line change
Expand Up @@ -315,7 +315,7 @@ tidyverse_style <- function(scope = "tokens",
#' }
#' set_line_break_before_curly_opening_style <- function() {
#' create_style_guide(
#' line_break = tibble::lst(set_line_break_before_curly_opening),
#' line_break = list(set_line_break_before_curly_opening),
#' style_guide_name = "some-style-guide",
#' style_guide_version = "some-version"
#' )
Expand Down
4 changes: 2 additions & 2 deletions R/stylerignore.R
Original file line number Diff line number Diff line change
Expand Up @@ -123,8 +123,8 @@ apply_stylerignore <- function(flattened_pd) {
env_current$stylerignore[, colnames_required_apply_stylerignore],
by.x = "pos_id", by.y = "first_pos_id_in_segment", all.x = TRUE,
sort = FALSE
) # %>%
# as_tibble()
)

flattened_pd %>%
stylerignore_consolidate_col("lag_newlines") %>%
stylerignore_consolidate_col("lag_spaces") %>%
Expand Down
5 changes: 2 additions & 3 deletions R/token-create.R
Original file line number Diff line number Diff line change
Expand Up @@ -39,7 +39,7 @@ create_tokens <- function(tokens,
block = NA,
is_cached = FALSE) {
len_text <- length(texts)
new_tibble(
new_styler_df(
list(
token = tokens,
text = texts,
Expand All @@ -60,8 +60,7 @@ create_tokens <- function(tokens,
block = block,
is_cached = is_cached
)
) %>%
as.data.frame()
)
}

#' Create valid pos_ids if possible
Expand Down
2 changes: 1 addition & 1 deletion R/token-define.R
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
token <- tribble(
token <- tibble::tribble(
IndrajeetPatil marked this conversation as resolved.
Show resolved Hide resolved
~text, ~class, ~token,
"&", "logical", "AND",
"&&", "logical", "AND2",
Expand Down
3 changes: 1 addition & 2 deletions R/transform-files.R
Original file line number Diff line number Diff line change
Expand Up @@ -30,8 +30,7 @@ transform_files <- function(files,
)
communicate_summary(changed, max_char)
communicate_warning(changed, transformers)
new_tibble(list(file = files, changed = changed)) %>%
as.data.frame()
new_styler_df(list(file = files, changed = changed))
}

#' Transform a file and output a customized message
Expand Down
6 changes: 3 additions & 3 deletions R/ui-caching.R
Original file line number Diff line number Diff line change
Expand Up @@ -75,9 +75,9 @@ cache_info <- function(cache_name = NULL, format = "both") {
rlang::arg_match(format, c("tabular", "lucid", "both"))
path_cache <- cache_find_path(cache_name)
files <- list.files(path_cache, full.names = TRUE)
file_info <- file.info(files) # %>%
# as_tibble()
tbl <- data.frame( # tibble(
file_info <- file.info(files)

tbl <- styler_df(
n = nrow(file_info),
size = sum(file_info$size),
last_modified = suppressWarnings(max(file_info$mtime)),
Expand Down
8 changes: 8 additions & 0 deletions R/utils.R
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,14 @@ line_col_names <- function() {
c("line1", "line2", "col1", "col2")
}

styler_df <- function(..., .size = NULL, .name_repair = "minimal") {
IndrajeetPatil marked this conversation as resolved.
Show resolved Hide resolved
vctrs::data_frame(..., .size = .size, .name_repair = .name_repair)
}

new_styler_df <- function(x) {
vctrs::new_data_frame(x)
}

#' Ensure there is one (and only one) blank line at the end of a vector
#' @examples
#' styler:::ensure_last_n_empty("")
Expand Down
2 changes: 1 addition & 1 deletion man/create_style_guide.Rd

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

Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@
#' }
#' }
#' set_line_break_before_curly_opening_style <- function() {
#' create_style_guide(line_break = tibble::lst(set_line_break_before_curly_opening))
#' create_style_guide(line_break = list(set_line_break_before_curly_opening))
#' }
#' @examples
#' \dontrun{
Expand All @@ -32,9 +32,9 @@ create_style_guide <- function(initialize = default_style_guide_attributes,
indention = NULL,
use_raw_indention = FALSE,
reindention = tidyverse_reindention()) {
lst(
list(
# transformer functions
initialize = lst(initialize),
initialize = list(initialize),
line_break,
space,
token,
Expand Down Expand Up @@ -63,7 +63,7 @@ create_style_guide <- function(initialize = default_style_guide_attributes,
#' }
#' }
#' set_line_break_before_curly_opening_style <- function() {
#' create_style_guide(line_break= tibble::lst(set_line_break_before_curly_opening))
#' create_style_guide(line_break= list(set_line_break_before_curly_opening))
#' }
#' @examples
#' \donttest{style_text("a <- function(x) { x }
Expand All @@ -78,9 +78,9 @@ create_style_guide <- function(initialize = default_style_guide_attributes,
indention = NULL,
use_raw_indention = FALSE,
reindention = tidyverse_reindention()) {
lst(
list(
#transformer functions
initialize = lst(initialize),
initialize = list(initialize),
line_break,
space,
token,
Expand Down
Loading