From 06377ae2e585fd4df695f973cd8ee6b3f76bfe5f Mon Sep 17 00:00:00 2001 From: Sam McCall Date: Tue, 16 Jul 2019 10:17:06 +0000 Subject: [PATCH] [clangd] Don't rebuild background index until we indexed one TU per thread. Summary: This increases the odds that the boosted file (cpp file matching header) will be ready. (It always enqueues first, so it'll be present unless another thread indexes *two* files before the first thread indexes one.) Reviewers: kadircet Subscribers: ilya-biryukov, MaskRay, jkorous, arphaman, llvm-commits Tags: #llvm Differential Revision: https://reviews.llvm.org/D64682 llvm-svn: 366199 --- clang-tools-extra/clangd/index/Background.cpp | 2 +- clang-tools-extra/clangd/index/BackgroundRebuild.h | 14 ++++++++------ .../clangd/unittests/BackgroundIndexTests.cpp | 10 +++++----- 3 files changed, 14 insertions(+), 12 deletions(-) diff --git a/clang-tools-extra/clangd/index/Background.cpp b/clang-tools-extra/clangd/index/Background.cpp index 458e6fc355f8..23445e16b2f3 100644 --- a/clang-tools-extra/clangd/index/Background.cpp +++ b/clang-tools-extra/clangd/index/Background.cpp @@ -127,7 +127,7 @@ BackgroundIndex::BackgroundIndex( BackgroundIndexStorage::Factory IndexStorageFactory, size_t ThreadPoolSize) : SwapIndex(llvm::make_unique()), FSProvider(FSProvider), CDB(CDB), BackgroundContext(std::move(BackgroundContext)), - Rebuilder(this, &IndexedSymbols), + Rebuilder(this, &IndexedSymbols, ThreadPoolSize), IndexStorageFactory(std::move(IndexStorageFactory)), CommandsChanged( CDB.watch([&](const std::vector &ChangedFiles) { diff --git a/clang-tools-extra/clangd/index/BackgroundRebuild.h b/clang-tools-extra/clangd/index/BackgroundRebuild.h index 5a6227e8baab..f660957f6241 100644 --- a/clang-tools-extra/clangd/index/BackgroundRebuild.h +++ b/clang-tools-extra/clangd/index/BackgroundRebuild.h @@ -16,6 +16,7 @@ #include "index/FileIndex.h" #include "index/Index.h" +#include "llvm/Support/Threading.h" namespace clang { namespace clangd { @@ -45,12 +46,9 @@ namespace clangd { // This class is exposed in the header so it can be tested. class BackgroundIndexRebuilder { public: - // Thresholds for rebuilding as TUs get indexed. - static constexpr unsigned TUsBeforeFirstBuild = 5; - static constexpr unsigned TUsBeforeRebuild = 100; - - BackgroundIndexRebuilder(SwapIndex *Target, FileSymbols *Source) - : Target(Target), Source(Source) {} + BackgroundIndexRebuilder(SwapIndex *Target, FileSymbols *Source, + unsigned Threads) + : TUsBeforeFirstBuild(Threads), Target(Target), Source(Source) {} // Called to indicate a TU has been indexed. // May rebuild, if enough TUs have been indexed. @@ -71,6 +69,10 @@ public: // Ensures we won't start any more rebuilds. void shutdown(); + // Thresholds for rebuilding as TUs get indexed. + const unsigned TUsBeforeFirstBuild; // Typically one per worker thread. + const unsigned TUsBeforeRebuild = 100; + private: // Run Check under the lock, and rebuild if it returns true. void maybeRebuild(const char *Reason, std::function Check); diff --git a/clang-tools-extra/clangd/unittests/BackgroundIndexTests.cpp b/clang-tools-extra/clangd/unittests/BackgroundIndexTests.cpp index 15d064a995ca..79e081bd6789 100644 --- a/clang-tools-extra/clangd/unittests/BackgroundIndexTests.cpp +++ b/clang-tools-extra/clangd/unittests/BackgroundIndexTests.cpp @@ -575,7 +575,8 @@ TEST_F(BackgroundIndexTest, CmdLineHash) { class BackgroundIndexRebuilderTest : public testing::Test { protected: BackgroundIndexRebuilderTest() - : Target(llvm::make_unique()), Rebuilder(&Target, &Source) { + : Target(llvm::make_unique()), + Rebuilder(&Target, &Source, /*Threads=*/10) { // Prepare FileSymbols with TestSymbol in it, for checkRebuild. TestSymbol.ID = SymbolID("foo"); } @@ -610,11 +611,10 @@ protected: }; TEST_F(BackgroundIndexRebuilderTest, IndexingTUs) { - for (unsigned I = 0; I < BackgroundIndexRebuilder::TUsBeforeFirstBuild - 1; - ++I) + for (unsigned I = 0; I < Rebuilder.TUsBeforeFirstBuild - 1; ++I) EXPECT_FALSE(checkRebuild([&] { Rebuilder.indexedTU(); })); EXPECT_TRUE(checkRebuild([&] { Rebuilder.indexedTU(); })); - for (unsigned I = 0; I < BackgroundIndexRebuilder::TUsBeforeRebuild - 1; ++I) + for (unsigned I = 0; I < Rebuilder.TUsBeforeRebuild - 1; ++I) EXPECT_FALSE(checkRebuild([&] { Rebuilder.indexedTU(); })); EXPECT_TRUE(checkRebuild([&] { Rebuilder.indexedTU(); })); } @@ -640,7 +640,7 @@ TEST_F(BackgroundIndexRebuilderTest, LoadingShards) { // No rebuilding for indexed files while loading. Rebuilder.startLoading(); - for (unsigned I = 0; I < 3 * BackgroundIndexRebuilder::TUsBeforeRebuild; ++I) + for (unsigned I = 0; I < 3 * Rebuilder.TUsBeforeRebuild; ++I) EXPECT_FALSE(checkRebuild([&] { Rebuilder.indexedTU(); })); // But they get indexed when we're done, even if no shards were loaded. EXPECT_TRUE(checkRebuild([&] { Rebuilder.doneLoading(); }));