[clang-tools-extra] [clangd] Count the slabs of an index once in the memory usage (PR #244523)
Orgad Shaneh via cfe-commits
cfe-commits at lists.llvm.org
Thu Nov 19 23:41:24 PST 2026
https://github.com/orgads created https://github.com/llvm/llvm-project/pull/244523
FileSymbols::buildIndex() adds the size of the symbol and ref slabs to the memory usage of the index that it builds, as the index keeps them alive. FileIndex::profile() and BackgroundIndex::profile() report the slabs on their own as well, so $/memoryUsage counted them twice.
With a background index that has 163 MB of slabs, it reported 486 MB for the index where there are 322 MB.
Report the index without the slabs.
Assisted-by: Claude Code
>From 0770bf561595cbd608667bbc271e30be7d706bf2 Mon Sep 17 00:00:00 2001
From: Orgad Shaneh <orgad.shaneh at audiocodes.com>
Date: Fri, 20 Nov 2026 09:06:00 +0200
Subject: [PATCH] [clangd] Count the slabs of an index once in the memory usage
FileSymbols::buildIndex() adds the size of the symbol and ref slabs to
the memory usage of the index that it builds, as the index keeps them
alive. FileIndex::profile() and BackgroundIndex::profile() report the
slabs on their own as well, so $/memoryUsage counted them twice.
With a background index that has 163 MB of slabs, it reported 486 MB
for the index where there are 322 MB.
Report the index without the slabs.
Assisted-by: Claude Code
---
clang-tools-extra/clangd/index/Background.cpp | 10 +++++---
clang-tools-extra/clangd/index/FileIndex.cpp | 24 +++++++++++++++----
clang-tools-extra/clangd/index/FileIndex.h | 4 +++-
.../clangd/unittests/FileIndexTests.cpp | 14 +++++++++--
4 files changed, 41 insertions(+), 11 deletions(-)
diff --git a/clang-tools-extra/clangd/index/Background.cpp b/clang-tools-extra/clangd/index/Background.cpp
index 17a8097394492e..6c8cf76d921dee 100644
--- a/clang-tools-extra/clangd/index/Background.cpp
+++ b/clang-tools-extra/clangd/index/Background.cpp
@@ -418,9 +418,13 @@ BackgroundIndex::loadProject(std::vector<std::string> MainFiles) {
}
void BackgroundIndex::profile(MemoryTree &MT) const {
- IndexedSymbols.profile(MT.child("slabs"));
- // We don't want to mix memory used by index and symbols, so call base class.
- MT.child("index").addUsage(SwapIndex::estimateMemoryUsage());
+ size_t SlabBytes = IndexedSymbols.profile(MT.child("slabs"));
+ // We don't want to mix memory used by index and symbols, so call base class,
+ // and leave out the slabs, which the index counts too. If they changed since
+ // the index was built, this is only close.
+ size_t IndexBytes = SwapIndex::estimateMemoryUsage();
+ MT.child("index").addUsage(IndexBytes > SlabBytes ? IndexBytes - SlabBytes
+ : 0);
}
} // namespace clangd
} // namespace clang
diff --git a/clang-tools-extra/clangd/index/FileIndex.cpp b/clang-tools-extra/clangd/index/FileIndex.cpp
index 2e005bfe3537e6..dc6a16456f708f 100644
--- a/clang-tools-extra/clangd/index/FileIndex.cpp
+++ b/clang-tools-extra/clangd/index/FileIndex.cpp
@@ -401,23 +401,27 @@ FileSymbols::buildIndex(IndexType Type, DuplicateHandling DuplicateHandle,
llvm_unreachable("Unknown clangd::IndexType");
}
-void FileSymbols::profile(MemoryTree &MT) const {
+size_t FileSymbols::profile(MemoryTree &MT) const {
std::lock_guard<std::mutex> Lock(Mutex);
+ size_t KeptAliveByIndex = 0;
for (const auto &SymSlab : SymbolsSnapshot) {
MT.detail(SymSlab.first())
.child("symbols")
.addUsage(SymSlab.second->bytes());
+ KeptAliveByIndex += SymSlab.second->bytes();
}
for (const auto &RefSlab : RefsSnapshot) {
MT.detail(RefSlab.first())
.child("references")
.addUsage(RefSlab.second.Slab->bytes());
+ KeptAliveByIndex += RefSlab.second.Slab->bytes();
}
for (const auto &RelSlab : RelationsSnapshot) {
MT.detail(RelSlab.first())
.child("relations")
.addUsage(RelSlab.second->bytes());
}
+ return KeptAliveByIndex;
}
FileIndex::FileIndex(bool SupportContainedRefs)
@@ -494,15 +498,25 @@ void FileIndex::updateMain(PathRef Path, ParsedAST &AST) {
}
}
+// The index counts the slabs that it keeps alive, which are reported on their
+// own. If they changed since the index was built, this is only close.
+static size_t indexUsageWithoutSlabs(const SymbolIndex &Index,
+ size_t SlabBytes) {
+ size_t IndexBytes = Index.estimateMemoryUsage();
+ return IndexBytes > SlabBytes ? IndexBytes - SlabBytes : 0;
+}
+
void FileIndex::profile(MemoryTree &MT) const {
- PreambleSymbols.profile(MT.child("preamble").child("slabs"));
+ size_t PreambleSlabs =
+ PreambleSymbols.profile(MT.child("preamble").child("slabs"));
MT.child("preamble")
.child("index")
- .addUsage(PreambleIndex.estimateMemoryUsage());
- MainFileSymbols.profile(MT.child("main_file").child("slabs"));
+ .addUsage(indexUsageWithoutSlabs(PreambleIndex, PreambleSlabs));
+ size_t MainFileSlabs =
+ MainFileSymbols.profile(MT.child("main_file").child("slabs"));
MT.child("main_file")
.child("index")
- .addUsage(MainFileIndex.estimateMemoryUsage());
+ .addUsage(indexUsageWithoutSlabs(MainFileIndex, MainFileSlabs));
}
} // namespace clangd
} // namespace clang
diff --git a/clang-tools-extra/clangd/index/FileIndex.h b/clang-tools-extra/clangd/index/FileIndex.h
index 86af5ee3723f6e..a9255e3229d832 100644
--- a/clang-tools-extra/clangd/index/FileIndex.h
+++ b/clang-tools-extra/clangd/index/FileIndex.h
@@ -87,7 +87,9 @@ class FileSymbols {
DuplicateHandling DuplicateHandle = DuplicateHandling::PickOne,
size_t *Version = nullptr);
- void profile(MemoryTree &MT) const;
+ /// Returns the size of the symbol and ref slabs. An index that was built
+ /// from them keeps them alive, and counts them in its own memory usage.
+ size_t profile(MemoryTree &MT) const;
private:
IndexContents IdxContents;
diff --git a/clang-tools-extra/clangd/unittests/FileIndexTests.cpp b/clang-tools-extra/clangd/unittests/FileIndexTests.cpp
index a92142fbcd7c4a..2f9226ac3a23d3 100644
--- a/clang-tools-extra/clangd/unittests/FileIndexTests.cpp
+++ b/clang-tools-extra/clangd/unittests/FileIndexTests.cpp
@@ -734,7 +734,15 @@ TEST(FileIndexTest, Profile) {
UnorderedElementsAre(Pair("index", _), Pair("slabs", _)));
ASSERT_THAT(MT.child("preamble").child("index").total(), Gt(0U));
- ASSERT_THAT(MT.child("main_file").child("index").total(), Gt(0U));
+ // The main file has no symbols: its index has nothing but the slabs.
+ ASSERT_THAT(MT.child("main_file").total(), Gt(0U));
+
+ // The indexes keep the slabs alive, and count them. They must not be
+ // reported twice.
+ size_t Slabs = MT.child("preamble").child("slabs").total() +
+ MT.child("main_file").child("slabs").total();
+ ASSERT_THAT(Slabs, Gt(0U));
+ EXPECT_LT(MT.total(), FI.estimateMemoryUsage() + Slabs);
}
TEST(FileSymbolsTest, Profile) {
@@ -746,9 +754,11 @@ TEST(FileSymbolsTest, Profile) {
false);
llvm::BumpPtrAllocator Alloc;
MemoryTree MT(&Alloc);
- FS.profile(MT);
+ size_t KeptAliveByIndex = FS.profile(MT);
ASSERT_THAT(MT.children(), UnorderedElementsAre(Pair("f1", _), Pair("f2", _),
Pair("f3", _)));
+ // The symbols and the refs, which an index keeps alive.
+ EXPECT_EQ(KeptAliveByIndex, MT.child("f1").total() + MT.child("f2").total());
EXPECT_THAT(MT.child("f1").children(), ElementsAre(Pair("symbols", _)));
EXPECT_THAT(MT.child("f1").total(), Gt(0U));
EXPECT_THAT(MT.child("f2").children(), ElementsAre(Pair("references", _)));
More information about the cfe-commits
mailing list