Repository navigation
Speedup - #90
Speedup#90
Conversation
instead of start:stop and start > stop return condition.
Codecov Report
@@ Coverage Diff @@
## master #90 +/- ##
=========================================
+ Coverage 91.47% 92.28% +0.8%
=========================================
Files 17 17
Lines 610 596 -14
=========================================
- Hits 558 550 -8
+ Misses 52 46 -6
Continue to review full report at Codecov.
|
krlmlr
left a comment
There was a problem hiding this comment.
Looks good. Would you mind sharing the output of a relevant profvis run, e.g. on Rpubs?
| indent_curly <- function(pd, indent_by) { | ||
| indention_needed <- needs_indention(pd, token = "'{'") | ||
| opening <- which(pd$token == "'{'") | ||
| indention_needed <- needs_indention(pd, token = "'{'", opening[1]) |
There was a problem hiding this comment.
Can we define a function get_indent_indices() instead that returns a (possibly empty) integer vector of positions that need to have indention added?
indent_indices <- get_indent_indices(pd, ...)
if (length(indent_indices) > 0L) pd$indent[indent_indices] <- pd$indent[indent_indices] + 2LAlso, the benchmarks suggest that this doesn't buy us anything.
There was a problem hiding this comment.
Ok, sounds good. I think since dplyr::between() returns boolean values, we better call it compute_indent_flags() and do boolean subsetting instead of integer subsetting. Does that sound reasonable?
indent_round <- function(pd, indent_by) {
indent_flags <- compute_indent_flags(pd, token = "'('")
pd$indent[indent_flags] <- pd$indent[indent_flags] + indent_by
pd %>%
set_unindention_child(token = "')'", unindent_by = indent_by)
}
compute_indent_flags <- function(pd, token = "'('") {
npd <- nrow(pd)
opening <- which(pd$token == token)
if (!needs_indention(pd, token, opening[1])) return()
start <- opening + 1
stop <- npd - 1
between(seq_len(npd), start, stop)
}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.
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.
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.
I'm fine with flags if it works well enough.
There was a problem hiding this comment.
Ok no, I just figured out that which() is not expensive. It was the comparison inside which(), which we can't avoid anyways. Also, always having a numerical vector is better than having NULL sometimes, so I use indices anyways.
| strip_eol_spaces <- function(pd_flat) { | ||
| pd_flat %>% | ||
| mutate(spaces = spaces * (lead(lag_newlines, default = 0) == 0)) | ||
| pd_flat$spaces <- pd_flat$spaces * (lead(pd_flat$lag_newlines, default = 0) == 0) |
There was a problem hiding this comment.
The following pattern seems slightly easier to read:
idx <- which(...)
pd_flat$spaces[idx] <- 0L
pd_flatThere was a problem hiding this comment.
Or flags instead of the (expensive) which?
idx <- lead(pd_flat$lag_newlines, default = 0) != 0
pd_flat$spaces[idx] <- 0| 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.
The short is optional and could be added to the "flat" parse data, too.
There was a problem hiding this comment.
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.
If it helps we should keep it for now.
There was a problem hiding this comment.
moved it into tokenize()
| #' @rdname add_token_terminal | ||
| add_terminal_token_before <- function(pd_flat) { | ||
| pd_flat %>% | ||
| terminals <- pd_flat %>% |
There was a problem hiding this comment.
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.
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.
I didn't know that, I just noticed you changed it and assumed that performance matters here. Never mind.
| split <- pd_flat %>% | ||
| mutate_(internal = ~ (id %in% parent) | (parent <= 0)) %>% | ||
| nest_("data", names(pd_flat)) | ||
| nest_("data", setdiff(names(pd_flat), "internal")) |
There was a problem hiding this comment.
You could try split_data <- split(pd_flat[...], pd_flat$internal) and use split_data instead of split$data below.
There was a problem hiding this comment.
yes, that works well.
| filter(token != "COMMENT") | ||
| non_comments <-pd[pd$token != "COMMENT", ] | ||
|
|
||
| comments <- comments %>% |
There was a problem hiding this comment.
Yes. Should we rather do?
extract(comments, text, ...)|
|
||
| set_space_before_comments <- function(pd_flat) { | ||
| comment_after <- pd_flat$token == "COMMENT" | ||
| if (all(!comment_after)) return(pd_flat) |
There was a problem hiding this comment.
!any() might be slightly faster (also elsewhere).
There was a problem hiding this comment.
Ok, Can try that. Had it before, but then I felt it's less legible, but did not think in terms of speed.
There was a problem hiding this comment.
Changed it, but it's not really faster.
| #' @importFrom purrr map | ||
| set_unindention_child <- function(pd, token = "')'", unindent_by) { | ||
| if(all(pd$terminal) | all(pd$indent == 0)) return(pd) | ||
| if(all(pd$indent == 0) | all(pd$terminal) ) return(pd) |
There was a problem hiding this comment.
|| is slightly clearer, no performance difference here.
There was a problem hiding this comment.
👍 Will try.
| rep_char <- function(char, times) { | ||
| lapply(times, rep.int, x = char) %>% | ||
| vapply(paste, collapse = "", character(1L)) | ||
| map(times, rep.int, x = char) %>% |
There was a problem hiding this comment.
For the flat serialization, newlines_and_spaces() uses rep_char() with a vectorised times input and for setting spaces at the beginning of comments we do that. I think we can use map() in these two places. It speeds things up quite a bit (~15%). Thanks for the hint.
| pd_transformed <- pd_nested %>% | ||
| visit_one(funs) %>% | ||
| mutate(child = map(child, pre_visit, funs = funs)) | ||
| visit_one(funs) |
There was a problem hiding this comment.
I can change this to a one-liner without pipe.
speedup of ~15%
No speed improvement
krlmlr
left a comment
There was a problem hiding this comment.
Looks good. Eventually we might want to use visitors that works in a loop without recursion, for easier to understand profiling results.
On an example file, the changes introduced with this PR speed up styler up to 4x. For styling a whole package, the speed gains turn out to be much less, only about 2.5x. Main bottle neck now is
tidyr::nest().This PR is based on #78