hokein created this revision. hokein added reviewers: ioeric, sammccall. Herald added subscribers: kadircet, arphaman, jkorous, MaskRay, ilya-biryukov.
Currently we don't cleanup dynamic index if a file is closed in clangd, which may result in large memory consumption in clangd (if we open many files and closes them). This patch is the first step. Repository: rCTE Clang Tools Extra https://reviews.llvm.org/D52979 Files: clangd/index/FileIndex.cpp clangd/index/FileIndex.h unittests/clangd/FileIndexTests.cpp Index: unittests/clangd/FileIndexTests.cpp =================================================================== --- unittests/clangd/FileIndexTests.cpp +++ unittests/clangd/FileIndexTests.cpp @@ -21,6 +21,7 @@ #include "gtest/gtest.h" using testing::_; +using testing::IsEmpty; using testing::AllOf; using testing::ElementsAre; using testing::Pair; @@ -164,6 +165,17 @@ EXPECT_THAT(match(M, Req), UnorderedElementsAre("ns::f", "ns::X")); } +TEST(FileIndexTest, RemoveFile) { + FileIndex M; + update(M, "f1", "class Foo {};"); + + FuzzyFindRequest Req; + EXPECT_THAT(match(M, Req), UnorderedElementsAre("Foo")); + + M.removeFile("f1.cpp"); + EXPECT_THAT(match(M, Req), IsEmpty()); +} + TEST(FileIndexTest, NoLocal) { FileIndex M; update(M, "f1", "namespace ns { void f() { int local = 0; } class X {}; }"); Index: clangd/index/FileIndex.h =================================================================== --- clangd/index/FileIndex.h +++ clangd/index/FileIndex.h @@ -59,7 +59,6 @@ }; /// This manages symbols from files and an in-memory index on all symbols. -/// FIXME: Expose an interface to remove files that are closed. class FileIndex : public MergedIndex { public: /// If URISchemes is empty, the default schemes in SymbolCollector will be @@ -75,6 +74,9 @@ /// `indexMainDecls`. void updateMain(PathRef Path, ParsedAST &AST); + /// Remove all index data associated with the file \p Path. + void removeFile(PathRef Path); + private: std::vector<std::string> URISchemes; Index: clangd/index/FileIndex.cpp =================================================================== --- clangd/index/FileIndex.cpp +++ clangd/index/FileIndex.cpp @@ -174,5 +174,12 @@ MainFileIndex.reset(MainFileSymbols.buildMemIndex()); } +void FileIndex::removeFile(PathRef Path) { + PreambleSymbols.update(Path, nullptr, nullptr); + PreambleIndex.reset(PreambleSymbols.buildMemIndex()); + MainFileSymbols.update(Path, nullptr, nullptr); + MainFileIndex.reset(MainFileSymbols.buildMemIndex()); +} + } // namespace clangd } // namespace clang
Index: unittests/clangd/FileIndexTests.cpp =================================================================== --- unittests/clangd/FileIndexTests.cpp +++ unittests/clangd/FileIndexTests.cpp @@ -21,6 +21,7 @@ #include "gtest/gtest.h" using testing::_; +using testing::IsEmpty; using testing::AllOf; using testing::ElementsAre; using testing::Pair; @@ -164,6 +165,17 @@ EXPECT_THAT(match(M, Req), UnorderedElementsAre("ns::f", "ns::X")); } +TEST(FileIndexTest, RemoveFile) { + FileIndex M; + update(M, "f1", "class Foo {};"); + + FuzzyFindRequest Req; + EXPECT_THAT(match(M, Req), UnorderedElementsAre("Foo")); + + M.removeFile("f1.cpp"); + EXPECT_THAT(match(M, Req), IsEmpty()); +} + TEST(FileIndexTest, NoLocal) { FileIndex M; update(M, "f1", "namespace ns { void f() { int local = 0; } class X {}; }"); Index: clangd/index/FileIndex.h =================================================================== --- clangd/index/FileIndex.h +++ clangd/index/FileIndex.h @@ -59,7 +59,6 @@ }; /// This manages symbols from files and an in-memory index on all symbols. -/// FIXME: Expose an interface to remove files that are closed. class FileIndex : public MergedIndex { public: /// If URISchemes is empty, the default schemes in SymbolCollector will be @@ -75,6 +74,9 @@ /// `indexMainDecls`. void updateMain(PathRef Path, ParsedAST &AST); + /// Remove all index data associated with the file \p Path. + void removeFile(PathRef Path); + private: std::vector<std::string> URISchemes; Index: clangd/index/FileIndex.cpp =================================================================== --- clangd/index/FileIndex.cpp +++ clangd/index/FileIndex.cpp @@ -174,5 +174,12 @@ MainFileIndex.reset(MainFileSymbols.buildMemIndex()); } +void FileIndex::removeFile(PathRef Path) { + PreambleSymbols.update(Path, nullptr, nullptr); + PreambleIndex.reset(PreambleSymbols.buildMemIndex()); + MainFileSymbols.update(Path, nullptr, nullptr); + MainFileIndex.reset(MainFileSymbols.buildMemIndex()); +} + } // namespace clangd } // namespace clang
_______________________________________________ cfe-commits mailing list cfe-commits@lists.llvm.org http://lists.llvm.org/cgi-bin/mailman/listinfo/cfe-commits