Skip to content

Commit 5ebc6ab

Browse files
krlmlrjimhester
andauthored
perf: Avoid iterating over already processed files with gcov (#611)
* perf: Avoid accumulation of work for each output file with `clean = FALSE` * Cleaner output * Unused * Use message instead of writeLines * Add note to news [skip ci] --------- Co-authored-by: Jim Hester <[email protected]>
1 parent ee5833b commit 5ebc6ab

File tree

2 files changed

+38
-5
lines changed

2 files changed

+38
-5
lines changed

NEWS.md

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,7 @@
11
# covr (development version)
22

3+
* Performance improvement for compiled code with a lot of compilation units (@krlmlr, #611)
4+
35
* Messages are now displayed using cli instead of crayon (@olivroy, #591).
46

57
* covr now uses `testthat::with_mocked_bindings()` for its internal testing (@olivroy, #595).

R/compiled.R

Lines changed: 36 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -72,27 +72,58 @@ run_gcov <- function(path, quiet = TRUE, clean = TRUE,
7272
return()
7373
}
7474

75-
gcov_inputs <- list.files(path, pattern = rex::rex(".gcno", end), recursive = TRUE, full.names = TRUE)
75+
withr::local_dir(src_path)
76+
77+
gcov_inputs <- list.files(".", pattern = rex::rex(".gcno", end), recursive = TRUE, full.names = TRUE)
78+
7679
if (!nzchar(gcov_path)) {
7780
if (length(gcov_inputs)) stop('gcov not found')
7881
return()
7982
}
83+
8084
run_gcov_one <- function(src) {
8185
system_check(gcov_path,
8286
args = c(gcov_args, src, "-p", "-o", dirname(src)),
8387
quiet = quiet, echo = !quiet)
84-
gcov_outputs <- list.files(path, pattern = rex::rex(".gcov", end), recursive = TRUE, full.names = TRUE)
88+
gcov_outputs <- list.files(".", pattern = rex::rex(".gcov", end), recursive = TRUE, full.names = TRUE)
89+
90+
if (!quiet) {
91+
message("gcov output for ", src, ":")
92+
message(paste(gcov_outputs, collapse = "\n"))
93+
}
94+
8595
if (clean) {
8696
on.exit(unlink(gcov_outputs))
97+
} else {
98+
gcov_output_base <- file.path("..", "covr", src)
99+
gcov_output_targets <- sub(".", gcov_output_base, gcov_outputs)
100+
101+
if (!quiet) {
102+
message("gcov output targets for ", src, ":")
103+
message(paste(gcov_output_targets, collapse = "\n"))
104+
}
105+
106+
lapply(
107+
unique(dirname(gcov_output_targets)),
108+
function(.x) dir.create(.x, recursive = TRUE, showWarnings = FALSE)
109+
)
110+
111+
on.exit({
112+
if (!quiet) {
113+
message("Moving gcov outputs to covr directory.\n")
114+
}
115+
file.rename(gcov_outputs, gcov_output_targets)
116+
})
87117
}
118+
88119
unlist(lapply(gcov_outputs, parse_gcov, package_path = c(path, getOption("covr.gcov_additional_paths", NULL))), recursive = FALSE)
89120
}
90121

91-
res <- withr::with_dir(src_path, {
92-
compact(unlist(lapply(gcov_inputs, run_gcov_one), recursive = FALSE))
93-
})
122+
res <- compact(unlist(lapply(gcov_inputs, run_gcov_one), recursive = FALSE))
123+
94124
if (!length(res) && length(gcov_inputs))
95125
warning('parsed gcov output was empty')
126+
96127
res
97128
}
98129

0 commit comments

Comments
 (0)