Skip to content

perf: Avoid iterating over already processed files with gcov #611

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

Open
wants to merge 3 commits into
base: main
Choose a base branch
from

Conversation

krlmlr
Copy link
Member

@krlmlr krlmlr commented Jun 27, 2025

This is the reason that covr is running so slow in igraph. Reduced the check time from over one hour (timeout) to a whopping 8 minutes. Other projects with many C/C++ files will also benefit.

https://github.com/igraph/rigraph/actions/runs/15930556545/job/44938446998

Comment on lines 18 to 19
# This header contains a C++ template, which requires you to run gcov for
# each object file separately and merge the results together.
Copy link
Member

@jimhester jimhester Jun 27, 2025

Choose a reason for hiding this comment

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

I think the problem with this change is this comment, if headers are included in multiple files they get tracked only once if you try to process all the object files at once, so you need to run gcov for each object file separately if you want accurate counting. This means you can have tests which exercise a header via one object file, and it might show up as uncovered depending on the order of the object files.

This is also why the coverage counts have now changed in the test.

Copy link
Member Author

Choose a reason for hiding this comment

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

I believe gcov is still run separately for each file, it's only that parse_gcov() (in R) is run once for each file, and not n times for the first file and once for the last file (giving a total of O(n^2) runs). I admit, the patch is a little difficult to read. What am I missing?

Copy link
Member

Choose a reason for hiding this comment

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

For each compilation unit you need to run gcov, then parse the results, then remove the output files. You can't just run gcov multiple times, then parse all the output files after the fact because the output files get overwritten by each individual run of gcov and you lose coverage information.

Copy link
Member

Choose a reason for hiding this comment

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

It is possible there is another better way to do this, but this was the only way I found back when I wrote this code originally.

Copy link
Member Author

Choose a reason for hiding this comment

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

Thanks: I thought I saw increasing runtime with each file: https://github.com/igraph/rigraph/actions/runs/15911464511/job/44880531901 . Let me do some experiments and get back to you.

@krlmlr
Copy link
Member Author

krlmlr commented Jun 27, 2025

It seems that with clean = FALSE we're processing more and more files with each new object. Possible solutions:

  • Collect gcov artifacts in separate subdirectories
  • Only look for changed files (could be unreliable with some file systems)

@krlmlr krlmlr changed the title fix: Avoid iterating over already processed files with gcov perf: Avoid iterating over already processed files with gcov Jun 27, 2025
@krlmlr
Copy link
Member Author

krlmlr commented Jun 27, 2025

Now:

  • Entirely running inside src/, shorter path names
  • Moving generated files out of sight with clean = FALSE
  • More verbosity

@krlmlr
Copy link
Member Author

krlmlr commented Jun 29, 2025

R/compiled.R Outdated
@@ -72,27 +72,56 @@ run_gcov <- function(path, quiet = TRUE, clean = TRUE,
return()
}

gcov_inputs <- list.files(path, pattern = rex::rex(".gcno", end), recursive = TRUE, full.names = TRUE)
res <- withr::local_dir(src_path)
Copy link
Contributor

Choose a reason for hiding this comment

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

This seems unused?

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.

3 participants