Skip to content

Speedup - #90

Merged
lorenzwalthert merged 21 commits into
r-lib:masterfrom
lorenzwalthert:speedup
Jul 27, 2017
Merged

lorenzwalthert merged 21 commits into
r-lib:masterfrom
lorenzwalthert:speedup

Conversation

@lorenzwalthert

@lorenzwalthert lorenzwalthert commented Jul 25, 2017 •

Copy link
Copy Markdown
Collaborator

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

@lorenzwalthert
lorenzwalthert requested a review from krlmlr July 25, 2017 20:38
@codecov

codecov Bot commented Jul 25, 2017 •

Copy link
Copy Markdown

Codecov Report

Merging #90 into master will increase coverage by 0.8%.
The diff coverage is 100%.

Impacted file tree graph

@@            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
Impacted Files Coverage Δ
R/rules-replacement.R 100% <100%> (ø) ⬆️
R/nested.R 85.29% <100%> (ø) ⬆️
R/visit.R 100% <100%> (ø) ⬆️
R/parsed.R 97.72% <100%> (-0.1%) ⬇️
R/unindent.R 96% <100%> (ø) ⬆️
R/modify_pd.R 100% <100%> (+10.52%) ⬆️
R/utils.R 100% <100%> (ø) ⬆️
R/rules-spacing.R 93.87% <100%> (+1.02%) ⬆️

Continue to review full report at Codecov.

Legend - Click here to learn more
Δ = absolute <relative> (impact), ø = not affected, ? = missing data
Powered by Codecov. Last update 59de93f...c339fa8. Read the comment docs.

@krlmlr krlmlr left a comment

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Looks good. Would you mind sharing the output of a relevant profvis run, e.g. on Rpubs?

Comment thread R/modify_pd.R Outdated
indent_curly <- function(pd, indent_by) {
indention_needed <- needs_indention(pd, token = "'{'")
opening <- which(pd$token == "'{'")
indention_needed <- needs_indention(pd, token = "'{'", opening[1])

Copy link
Copy Markdown
Member

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?

indent_indices <- get_indent_indices(pd, ...)
if (length(indent_indices) > 0L) pd$indent[indent_indices] <- pd$indent[indent_indices] + 2L

Also, the benchmarks suggest that this doesn't buy us anything.

Copy link
Copy Markdown
Collaborator Author

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 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.

@krlmlr krlmlr Jul 26, 2017 •

Copy link
Copy Markdown
Member

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.

Copy link
Copy Markdown
Collaborator Author

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.

Copy link
Copy Markdown
Member

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.

Copy link
Copy Markdown
Collaborator Author

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 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.

Comment thread R/modify_pd.R Outdated
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)

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The following pattern seems slightly easier to read:

idx <- which(...)
pd_flat$spaces[idx] <- 0L
pd_flat

Copy link
Copy Markdown
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Or flags instead of the (expensive) which?

idx <- lead(pd_flat$lag_newlines, default = 0) != 0
  pd_flat$spaces[idx] <- 0

Comment thread R/nested.R Outdated
add_terminal_token_after()

parse_data$child <- rep(list(NULL), length(parse_data$text))
parse_data$short <- substr(parse_data$text, 1, 5)

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The short is optional and could be added to the "flat" parse data, too.

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Do we still need short?

Copy link
Copy Markdown
Collaborator Author

Choose a reason for hiding this comment

The 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?

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

If it helps we should keep it for now.

Copy link
Copy Markdown
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

moved it into tokenize()

Comment thread R/nested.R
#' @rdname add_token_terminal
add_terminal_token_before <- function(pd_flat) {
pd_flat %>%
terminals <- pd_flat %>%

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The 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 = ...) %>% ...

Copy link
Copy Markdown
Collaborator Author

Choose a reason for hiding this comment

The 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?

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The 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.

Comment thread R/nested.R Outdated
split <- pd_flat %>%
mutate_(internal = ~ (id %in% parent) | (parent <= 0)) %>%
nest_("data", names(pd_flat))
nest_("data", setdiff(names(pd_flat), "internal"))

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

You could try split_data <- split(pd_flat[...], pd_flat$internal) and use split_data instead of split$data below.

Copy link
Copy Markdown
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

yes, that works well.

Comment thread R/rules-spacing.R Outdated
filter(token != "COMMENT")
non_comments <-pd[pd$token != "COMMENT", ]

comments <- comments %>%

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

A pipe with just one step?

Copy link
Copy Markdown
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Yes. Should we rather do?

extract(comments, text, ...)

Copy link
Copy Markdown
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

done

Comment thread R/rules-spacing.R Outdated

set_space_before_comments <- function(pd_flat) {
comment_after <- pd_flat$token == "COMMENT"
if (all(!comment_after)) return(pd_flat)

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

!any() might be slightly faster (also elsewhere).

Copy link
Copy Markdown
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Ok, Can try that. Had it before, but then I felt it's less legible, but did not think in terms of speed.

Copy link
Copy Markdown
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Changed it, but it's not really faster.

Comment thread R/unindent.R Outdated
#' @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)

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

|| is slightly clearer, no performance difference here.

Copy link
Copy Markdown
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

👍 Will try.

Comment thread R/utils.R Outdated
rep_char <- function(char, times) {
lapply(times, rep.int, x = char) %>%
vapply(paste, collapse = "", character(1L))
map(times, rep.int, x = char) %>%

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Can times be a vector here?

Copy link
Copy Markdown
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

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.

Comment thread R/visit.R Outdated
pd_transformed <- pd_nested %>%
visit_one(funs) %>%
mutate(child = map(child, pre_visit, funs = funs))
visit_one(funs)

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

One-step pipe?

Copy link
Copy Markdown
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I can change this to a one-liner without pipe.

@lorenzwalthert

Copy link
Copy Markdown
Collaborator Author

I added a profiling of commit cbf94f2 of a package (with 10 styled files) here. With this example, styling is about 4 times faster than before optimising for performance (commit 59de93f) .

@lorenzwalthert
lorenzwalthert requested a review from krlmlr July 26, 2017 20:02

@krlmlr krlmlr left a comment

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Looks good. Eventually we might want to use visitors that works in a loop without recursion, for easier to understand profiling results.

@lorenzwalthert
lorenzwalthert requested a review from krlmlr July 27, 2017 14:50
@lorenzwalthert
lorenzwalthert merged commit 0a9f468 into r-lib:master Jul 27, 2017
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants