Skip to content

Commit 1d2b9f6

Browse files
speed improvement to make speed test pass
speed improvement to make speed test pass
1 parent 587ff61 commit 1d2b9f6

File tree

2 files changed

+37
-3
lines changed

2 files changed

+37
-3
lines changed

R/nest.R

Lines changed: 15 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -93,20 +93,32 @@ drop_cached_children <- function(pd) {
9393

9494
pd_parent_first <- pd[order(pd$line1, pd$col1, -pd$line2, -pd$col2, as.integer(pd$terminal)),]
9595
pos_ids_to_keep <- pd_parent_first %>%
96-
split(cumsum(pd_parent_first$parent < 1)) %>%
96+
split(cumsum(pd_parent_first$parent == 0)) %>%
9797
map(find_pos_id_to_keep) %>%
9898
unlist() %>%
9999
unname()
100100
pd[pd$pos_id %in% pos_ids_to_keep,]
101101
} else {
102102
pd
103103
}
104-
105104
}
106105

106+
#' Find the pos ids to keep
107+
#'
108+
#' To make a parse table shallow, we must know which ids to keep.
109+
#' `split(cumsum(pd_parent_first$parent < 1))` above puts comments with negative
110+
#' parents in the same block as proceeding expressions. `find_pos_id_to_keep()`
111+
#' must hence alyways keep comments. We did not use
112+
#' `split(cumsum(pd_parent_first$parent < 1))` because then every comment is an
113+
#' expression on its own and processing takes much longer for typical roxygen
114+
#' annotated code
115+
#' @param pd A temporary top level nest where the first expression is always a
116+
#' top level expression, potentially cached.
117+
#' @keywords internal
107118
find_pos_id_to_keep <- function(pd) {
108119
if (pd$is_cached[1]) {
109-
pd$pos_id[1]
120+
idx_comment <- which(pd$token == "COMMENT")
121+
pd$pos_id[unique(c(1, idx_comment))]
110122
} else {
111123
pd$pos_id
112124
}

man/find_pos_id_to_keep.Rd

Lines changed: 22 additions & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

0 commit comments

Comments
 (0)