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

Indent between comment and line break #814

Merged
merged 3 commits into from Jun 27, 2021

Conversation

lorenzwalthert
Copy link
Collaborator

@lorenzwalthert lorenzwalthert commented Jun 25, 2021

Closes #813.

@codecov-commenter
Copy link

codecov-commenter commented Jun 25, 2021

Codecov Report

Merging #814 (d07b7ea) into master (14aebe0) will increase coverage by 0.01%.
The diff coverage is 100.00%.

❗ Current head d07b7ea differs from pull request most recent head f22501d. Consider uploading reports for the commit f22501d to get more accurate results
Impacted file tree graph

@@            Coverage Diff             @@
##           master     #814      +/-   ##
==========================================
+ Coverage   90.45%   90.47%   +0.01%     
==========================================
  Files          47       47              
  Lines        2430     2435       +5     
==========================================
+ Hits         2198     2203       +5     
  Misses        232      232              
Impacted Files Coverage Δ
R/rules-indention.R 100.00% <100.00%> (ø)

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 14aebe0...f22501d. Read the comment docs.

@github-actions
Copy link
Contributor

This is how benchmark results would change (along with a 95% confience interval in relative change) if ee0ade9 is merged into master:

  • cache_applying: 0.06s -> 0.07s [-0.16%, +7.53%]
  • cache_recording: 1.43s -> 1.45s [-1.54%, +4.16%]
  • without_cache: 3.7s -> 3.68s [-2.95%, +1.79%]

Further explanation regarding interpretation and methodology can be found in the documentation.

@github-actions
Copy link
Contributor

This is how benchmark results would change (along with a 95% confience interval in relative change) if d07b7ea is merged into master:

  • cache_applying: 0.06s -> 0.06s [-3.4%, +3.58%]
  • cache_recording: 1.52s -> 1.53s [-0.51%, +1.65%]
  • without_cache: 4s -> 3.99s [-1.09%, +0.42%]

Further explanation regarding interpretation and methodology can be found in the documentation.

@@ -54,12 +54,21 @@ indent_op <- function(pd,
indent_eq_sub <- function(pd,
indent_by,
token = c("EQ_SUB", "EQ_FORMALS")) {
eq_sub <- which(pd$token %in% token)
Copy link
Collaborator Author

Choose a reason for hiding this comment

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

@MichaelChirico changing to boolean here because I think it's slightly more efficient, but later realised we need to convert to int again for handling your exception

Copy link
Contributor

Choose a reason for hiding this comment

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

Thanks for fixing. How did you know to look in indent_eq_sub if I might ask?

Copy link
Contributor

Choose a reason for hiding this comment

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

Oh I just saw your comment on the issue, nvm.

Copy link
Collaborator Author

Choose a reason for hiding this comment

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

I just full text searched the rules in R/rules-indention.R With “eq_sub” to find a rule that has this in the name.

indent_indices <- intersect(eq_sub + 1, has_line_break)
has_line_break <- pd$lag_newlines > 0 | pd$token == "COMMENT"
indent_indices <- which(lag(eq_sub, default = FALSE) & has_line_break)
if (any(pd$token[indent_indices] == "COMMENT")) {
Copy link
Collaborator Author

Choose a reason for hiding this comment

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

As the case in #813 is probably occurring quite infrequently, avoid an unnecessary map() call for performance reasons.

Copy link
Contributor

Choose a reason for hiding this comment

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

Wouldn't a subassign be faster?

comment_idx <- pd$token[indent_indices] == "COMMENT"
indent_indices[comment_idx] <- next_non_comment(pd, indent_indices[comment_idx])

(or a map on the RHS of <- if next_non_comment isn't vectorized in arg 2)

Copy link
Collaborator Author

Choose a reason for hiding this comment

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

It’s not vectorised. Good thing now with {touchstone} we can see what’s faster for an average Code input. I can try that. Or we can benchmark just this changed expression locally.

Copy link
Collaborator Author

Choose a reason for hiding this comment

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

apparently not, sub-assignment and sub-setting is also expensive. All in all it's about the same.

# devtools::load_all()
# debug(indent_eq_sub)
# run some code

bench::mark(
  first = {
    indent_indices <- which(lag(eq_sub, default = FALSE) & has_line_break)
    x <- purrr::map_int(indent_indices, function(idx) {
    if (pd$token[idx] == "COMMENT") {
      next_non_comment(pd, idx)
    } else {
      idx
    }
  })
  x
  },
  second = {
    indent_indices <- which(lag(eq_sub, default = FALSE) & has_line_break)
    comment_idx <- pd$token[indent_indices] == "COMMENT"
    indent_indices[comment_idx] <- purrr::map_int(indent_indices[comment_idx], function(idx) {
      next_non_comment(pd, idx)
    })
    indent_indices
    }
)

#>  # A tibble: 2 x 13
#>  expression     min  median `itr/sec` mem_alloc `gc/sec` n_itr  n_gc total_time result memory  time   gc    
#>  <bch:expr> <bch:t> <bch:t>     <dbl> <bch:byt>    <dbl> <int> <dbl>   <bch:tm> <list> <list>  <list> <list>
#> 1 first         88µs  95.4µs     8973.        0B     2.03  4420     1      493ms <int … <Rprof… <benc… <tibb…
#> 2 second      89.8µs  95.2µs     9172.        0B     4.26  4310     2      470ms <int … <Rprof… <benc… <tibb…

@lorenzwalthert
Copy link
Collaborator Author

lorenzwalthert commented Jun 25, 2021

@MichaelChirico if you have suggestions for improvement, happy to hear them 😀. I don't think it's very elegant.

@lorenzwalthert lorenzwalthert merged commit 1aaae90 into r-lib:master Jun 27, 2021
@lorenzwalthert lorenzwalthert deleted the issue-813 branch June 27, 2021 18:57
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.

trailing comment within call breaks hanging indent
3 participants