-
Notifications
You must be signed in to change notification settings - Fork 71
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
Speedup #90
Speedup #90
Changes from 16 commits
ad70d87
2c004b7
1497a46
f79bbbd
8b4debf
3fa0a13
bc3be9a
3d41115
1b1792d
5ea6961
bdb625b
32ef3a4
c3c5a88
3fe76fe
02742a6
cbf94f2
30aa043
1690b8f
9b9af35
65d99f7
c339fa8
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -29,10 +29,10 @@ compute_parse_data_nested <- function(text) { | |
add_terminal_token_before() %>% | ||
add_terminal_token_after() | ||
|
||
parse_data$child <- rep(list(NULL), length(parse_data$text)) | ||
parse_data$short <- substr(parse_data$text, 1, 5) | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. The There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Do we still need There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. We don't use it further no. I It just helps when working interactively. Should we drop it? There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. If it helps we should keep it for now. There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. moved it into |
||
|
||
pd_nested <- parse_data %>% | ||
mutate_(child = ~rep(list(NULL), length(text))) %>% | ||
mutate_(short = ~substr(text, 1, 5)) %>% | ||
select_(~short, ~everything()) %>% | ||
nest_parse_data() %>% | ||
flatten_operators() | ||
|
||
|
@@ -57,13 +57,13 @@ tokenize <- function(text) { | |
#' description. | ||
#' @param pd A parse table. | ||
enhance_mapping_special <- function(pd) { | ||
pd %>% | ||
mutate(token = case_when( | ||
pd$token <- with(pd, case_when( | ||
token != "SPECIAL" ~ token, | ||
text == "%>%" ~ special_and("PIPE"), | ||
text == "%in%" ~ special_and("IN"), | ||
TRUE ~ special_and("OTHER") | ||
)) | ||
pd | ||
} | ||
|
||
special_and <- function(text) { | ||
|
@@ -98,19 +98,23 @@ NULL | |
|
||
#' @rdname add_token_terminal | ||
add_terminal_token_after <- function(pd_flat) { | ||
pd_flat %>% | ||
terminals <- pd_flat %>% | ||
filter(terminal) %>% | ||
arrange(line1, col1) %>% | ||
transmute(id = id, token_after = lead(token, default = "")) %>% | ||
arrange(line1, col1) | ||
|
||
data_frame(id = terminals$id, | ||
token_after = lead(terminals$token, default = "")) %>% | ||
left_join(pd_flat, ., by = "id") | ||
} | ||
|
||
#' @rdname add_token_terminal | ||
add_terminal_token_before <- function(pd_flat) { | ||
pd_flat %>% | ||
terminals <- pd_flat %>% | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Maybe faster: terminals <- which(pd_flat$terminals)
order <- order(pd_flat$line1, pd_flat$col1)[terminals]
data_frame(id = pd_flat$id[order], token_before = ...) %>% ... Or: terminals <- which(pd_flat$terminals)
order <- order(pd_flat$line1[terminals], pd_flat$col1[terminals])
data_frame(id = pd_flat$id[terminals][order], token_before = ...) %>% ... There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I tried that but I felt since this function is only called once and it seems pretty inexpensive (10 ms out of 15'460 for the whole run, file R/nested.R), I left it as is, for better legibility. Or do you prefer the rearrangement anyways? There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I didn't know that, I just noticed you changed it and assumed that performance matters here. Never mind. |
||
filter(terminal) %>% | ||
arrange(line1, col1) %>% | ||
transmute(id = id, token_before = lag(token, default = "")) %>% | ||
arrange(line1, col1) | ||
|
||
data_frame(id = terminals$id, | ||
token_before = lag(terminals$token, default = "")) %>% | ||
left_join(pd_flat, ., by = "id") | ||
} | ||
|
||
|
@@ -146,24 +150,22 @@ set_spaces <- function(spaces_after_prefix, force_one) { | |
#' @importFrom purrr map2 | ||
nest_parse_data <- function(pd_flat) { | ||
if (all(pd_flat$parent <= 0)) return(pd_flat) | ||
split <- pd_flat %>% | ||
mutate_(internal = ~ (id %in% parent) | (parent <= 0)) %>% | ||
nest_("data", names(pd_flat)) | ||
pd_flat$internal <- with(pd_flat, (id %in% parent) | (parent <= 0)) | ||
split_data <- split(pd_flat, pd_flat$internal) | ||
|
||
child <- split$data[!split$internal][[1L]] | ||
internal <- split$data[split$internal][[1L]] | ||
child <- split_data$`FALSE` | ||
internal <- split_data$`TRUE` | ||
|
||
internal <- rename_(internal, internal_child = ~child) | ||
|
||
nested <- | ||
child$parent_ <- child$parent | ||
joined <- | ||
child %>% | ||
mutate_(parent_ = ~parent) %>% | ||
nest_(., "child", setdiff(names(.), "parent_")) %>% | ||
left_join(internal, ., by = c("id" = "parent_")) %>% | ||
mutate_(child = ~map2(child, internal_child, combine_children)) %>% | ||
select_(~-internal_child) %>% | ||
select_(~short, ~everything(), ~-text, ~text) | ||
|
||
left_join(internal, ., by = c("id" = "parent_")) | ||
nested <- joined | ||
nested$child <- map2(nested$child, nested$internal_child, combine_children) | ||
nested <- nested[, setdiff(names(nested), "internal_child")] | ||
nest_parse_data(nested) | ||
} | ||
|
||
|
@@ -179,7 +181,8 @@ nest_parse_data <- function(pd_flat) { | |
combine_children <- function(child, internal_child) { | ||
bound <- bind_rows(child, internal_child) | ||
if (nrow(bound) == 0) return(NULL) | ||
arrange_(bound, ~line1, ~col1) | ||
bound[order(bound$line1, bound$col1), ] | ||
|
||
} | ||
|
||
#' Get the start right | ||
|
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Can we define a function
get_indent_indices()
instead that returns a (possibly empty) integer vector of positions that need to have indention added?Also, the benchmarks suggest that this doesn't buy us anything.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Ok, sounds good. I think since
dplyr::between()
returns boolean values, we better call itcompute_indent_flags()
and do boolean subsetting instead of integer subsetting. Does that sound reasonable?It has only slightly worse performance (<1%) so I think we should do it. It also reduces code duplication.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Indices have the advantage that they can be checked for length zero, and that they only contain the positions that we care about. Essentially,
compute_indent_indices <- function(...) which(compute_indent_flags(...))
. I'm not sure about performance, because you need to allocate an extra integer vector.There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Ok. I can try that. When I checked the profiling I just got the impression that
which()
is expensive.There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I'm fine with flags if it works well enough.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Ok no, I just figured out that
which()
is not expensive. It was the comparison insidewhich()
, which we can't avoid anyways. Also, always having a numerical vector is better than having NULL sometimes, so I use indices anyways.