Skip to content

Fix #15293 (Slow cached run with --cppcheck-build-dir on projects with many files) - #9096

Open
g-chauvel wants to merge 2 commits into
cppcheck-opensource:mainfrom
g-chauvel:builddir-cached-run-perf
Open

g-chauvel wants to merge 2 commits into
cppcheck-opensource:mainfrom
g-chauvel:builddir-cached-run-perf

Conversation

@g-chauvel

Copy link
Copy Markdown
Contributor

AnalyzerInformation::getAnalyzerInfoFile() reads and parses the whole files.txt at each call, and it is called several times per file, so with --cppcheck-build-dir the time is quadratic in the number of files (since 1.77). This PR parses files.txt once: the CLI and the GUI keep it in Settings::buildDirFilesTxt and pass it to AnalyzerInformation. Without it, files.txt is read as before.

mkdir src bd
for i in $(seq 1 20000); do printf 'int f%d(void) { return 0; }\n' $i > src/m$i.c; done
cppcheck -q -j16 --cppcheck-build-dir=bd src
time cppcheck -q --cppcheck-build-dir=bd src
Files Before this PR With this PR
5000 real 0m1.066s real 0m0.182s
10000 real 0m4.302s real 0m0.477s
20000 real 0m15.970s real 0m1.215s

@dmcppcheck dmcppcheck left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

AI review

For --project (compile_commands.json, vcxproj and so on), a cached run is still quadratic. The PR stores the parsed files.txt as a std::map by value in Settings. But CppCheck::check(const FileSettings&) makes a full copy of Settings for every file (Settings tempSettings = mSettings; at lib/cppcheck.cpp:851), so every file now copies the N-entry map. Before, every file parsed files.txt. On 10000 files the cached --project run takes 25.9s with the PR and 24.0s on base, so the PR does not help there and is slightly slower. Plain file lists do get faster (1.5s vs 9.5s). Keeping the index behind a shared, immutable pointer in Settings would avoid the copy.

Apart from this, results did not change: in my tests the PR picks the same analyzer info files and reports the same findings as base. I found no other problems. comments.md has no earlier comments.

Comments

# Comment By Where Status
1 Index copied with Settings for every file, so --project cached runs stay quadratic AI review lib/settings.h:156 🆕 New

@g-chauvel, please fix, resolve or reply to the open and new comments: #1.

Testing

What I built and ran

  • Built cppcheck and testrunner from pr/ and cppcheck from base/. I did not build the GUI.
  • ./testrunner TestAnalyzerInformation TestSummaries TestCppcheck TestSingleExecutorFS TestProcessExecutorFS TestThreadExecutorFS: 75 tests, 0 failed.
  • pytest -k "builddir or build_dir or cppcheck_build" in test/cli: 53 passed, 1 skipped, 1 xfailed.
  • Correctness check: a project where a.c appears twice (with different defines) and sub/a.c once, analysed twice with the build dir. PR and base give identical output on both runs and create the same a.a1..a3 / a.s1..a3 files.

Performance with --project

Generate 10000 files and a compile_commands.json for them:

mkdir src; for i in $(seq 1 10000); do printf 'int f%d(void) { return 0; }\n' $i > src/m$i.c; done
python3 -c "
import json,os
d=os.getcwd()
print(json.dumps([{'directory':d,'file':d+'/src/m%d.c'%i,'command':'gcc -c src/m%d.c'%i} for i in range(1,10001)]))" > cc.json
mkdir bd; cppcheck -q -j8 --cppcheck-build-dir=bd --project=cc.json
time cppcheck -q --cppcheck-build-dir=bd --project=cc.json

Cached run times (real):

Files base PR
2500 3.56s 3.84s
5000 8.31s 8.85s
10000 24.0s 25.9s

With a plain directory (src instead of --project) at 10000 files: PR 1.46s, base 9.52s, as the PR description says.

I interrupted the PR's --project run under gdb at random times. Both samples I looked at were copying std::map<std::tuple<std::string,...>>, which is buildDirFilesTxt, called from Settings::Settings (copy) ← CppCheck::check(const FileSettings&) at lib/cppcheck.cpp:851 ← SingleExecutor::check.


Automated review of fcfb4ee by Claude. The pull request was built and tested locally.

Comment thread lib/settings.h
std::string buildDir;

/** AnalyzerInformation::FilesTxt of buildDir, files.txt is read when it is empty */
std::map<std::tuple<std::string, std::string, std::size_t>, std::string> buildDirFilesTxt;

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

AI review

This map is stored by value in Settings, and CppCheck::check(const FileSettings&) copies the whole Settings for every file (Settings tempSettings = mSettings;, lib/cppcheck.cpp:851). So with --project, every file copies all N entries and the cached run is still quadratic. The cost of reading files.txt has only moved into the copy.

Measured on a compile_commands.json with 10000 trivial files (cached run):

  • PR: 25.9s
  • base: 24.0s
  • 2500 / 5000 files: 3.84s / 8.85s with the PR, 3.56s / 8.31s on base

gdb samples land in the _Rb_tree copy of this map inside Settings::Settings. Only the plain-files case benefits (1.5s vs 9.5s). Holding the index as std::shared_ptr<const AnalyzerInformation::FilesTxt> (or similar) would make the copy cheap.

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