https://github.com/orgads created 
https://github.com/llvm/llvm-project/pull/244532

Dex builds a table of all the refs by their container when it is created, for 
containedRefs(). That is what the outgoing calls of the call hierarchy use, 
which many sessions never ask for, and the table has an entry for every ref of 
a supported kind.

Build it on the first containedRefs() request.

On a project with 1.6 million refs, the working set of clangd is 12 MB smaller 
after loading the background index: 474 MB instead of 486 MB.

Assisted-by: Claude Code

>From 9484473ebe23201df4cddbd2dd1b84c2e860da10 Mon Sep 17 00:00:00 2001
From: Orgad Shaneh <[email protected]>
Date: Fri, 20 Nov 2026 09:13:13 +0200
Subject: [PATCH] [clangd] Build the refs by container in Dex on first use

Dex builds a table of all the refs by their container when it is
created, for containedRefs(). That is what the outgoing calls of the
call hierarchy use, which many sessions never ask for, and the table
has an entry for every ref of a supported kind.

Build it on the first containedRefs() request.

On a project with 1.6 million refs, the working set of clangd is 12 MB
smaller after loading the background index: 474 MB instead of 486 MB.

Assisted-by: Claude Code
---
 clang-tools-extra/clangd/index/dex/Dex.cpp | 21 +++++++++++++++++----
 clang-tools-extra/clangd/index/dex/Dex.h   | 10 +++++++++-
 2 files changed, 26 insertions(+), 5 deletions(-)

diff --git a/clang-tools-extra/clangd/index/dex/Dex.cpp 
b/clang-tools-extra/clangd/index/dex/Dex.cpp
index 179d8c4da0b3ec..885f213a53c438 100644
--- a/clang-tools-extra/clangd/index/dex/Dex.cpp
+++ b/clang-tools-extra/clangd/index/dex/Dex.cpp
@@ -149,19 +149,28 @@ void Dex::buildIndex(bool SupportContainedRefs) {
     Builder.add(*Symbols[SymbolRank], SymbolRank);
   InvertedIndex = std::move(Builder).build();
 
+  this->SupportContainedRefs = SupportContainedRefs;
+}
+
+const std::vector<Dex::RevRef> &Dex::revRefs() const {
+  std::lock_guard<std::mutex> Lock(LazyRevRefsMutex);
+  if (LazyRevRefsBuilt)
+    return LazyRevRefs;
+  LazyRevRefsBuilt = true;
   // If the containedRefs() operation is supported, build the RevRefs
   // data structure used to implement it.
   if (!SupportContainedRefs)
-    return;
+    return LazyRevRefs;
   for (const auto &[ID, RefList] : Refs)
     for (const auto &R : RefList)
       if ((R.Kind & ContainedRefsRequest::SupportedRefKinds) !=
           RefKind::Unknown)
-        RevRefs.emplace_back(R, ID);
+        LazyRevRefs.emplace_back(R, ID);
   // Sort by container ID so we can use binary search for lookup.
-  llvm::sort(RevRefs, [](const RevRef &A, const RevRef &B) {
+  llvm::sort(LazyRevRefs, [](const RevRef &A, const RevRef &B) {
     return A.ref().Container < B.ref().Container;
   });
+  return LazyRevRefs;
 }
 
 std::unique_ptr<Iterator> Dex::iterator(const Token &Tok) const {
@@ -336,6 +345,7 @@ Dex::lookupRevRefs(const SymbolID &Container) const {
   QueryRef.Container = Container;
   RevRef Query(QueryRef, SymbolID{});
 
+  const std::vector<RevRef> &RevRefs = revRefs();
   auto ItPair = std::equal_range(RevRefs.cbegin(), RevRefs.cend(), Query,
                                  [](const RevRef &A, const RevRef &B) {
                                    return A.ref().Container < 
B.ref().Container;
@@ -416,7 +426,10 @@ size_t Dex::estimateMemoryUsage() const {
   for (const auto &TokenToPostingList : InvertedIndex)
     Bytes += TokenToPostingList.second.bytes();
   Bytes += Refs.getMemorySize();
-  Bytes += RevRefs.size() * sizeof(RevRef);
+  {
+    std::lock_guard<std::mutex> Lock(LazyRevRefsMutex);
+    Bytes += LazyRevRefs.size() * sizeof(RevRef);
+  }
   Bytes += Relations.getMemorySize();
   Bytes += ReverseRelations.getMemorySize();
   return Bytes + BackingDataSize;
diff --git a/clang-tools-extra/clangd/index/dex/Dex.h 
b/clang-tools-extra/clangd/index/dex/Dex.h
index 1ea7d8c06c67c5..b47147ab05d2ca 100644
--- a/clang-tools-extra/clangd/index/dex/Dex.h
+++ b/clang-tools-extra/clangd/index/dex/Dex.h
@@ -26,6 +26,7 @@
 #include "index/dex/PostingList.h"
 #include "index/dex/Token.h"
 #include "llvm/ADT/StringSet.h"
+#include <mutex>
 
 namespace clang {
 namespace clangd {
@@ -127,6 +128,7 @@ class Dex : public SymbolIndex {
   };
 
   void buildIndex(bool EnableOutgoingCalls);
+  const std::vector<RevRef> &revRefs() const;
   llvm::iterator_range<std::vector<RevRef>::const_iterator>
   lookupRevRefs(const SymbolID &Container) const;
   std::unique_ptr<Iterator> iterator(const Token &Tok) const;
@@ -149,7 +151,13 @@ class Dex : public SymbolIndex {
   llvm::DenseMap<Token, PostingList> InvertedIndex;
   dex::Corpus Corpus;
   llvm::DenseMap<SymbolID, llvm::ArrayRef<Ref>> Refs;
-  std::vector<RevRef> RevRefs; // sorted by container ID
+  // The refs by container, for containedRefs(). They are built on its first
+  // request: they take memory for every ref, and many sessions never ask.
+  // Use revRefs() to read them.
+  bool SupportContainedRefs = false;
+  mutable std::mutex LazyRevRefsMutex;
+  mutable bool LazyRevRefsBuilt = false;
+  mutable std::vector<RevRef> LazyRevRefs; // sorted by container ID
   static_assert(sizeof(RelationKind) == sizeof(uint8_t),
                 "RelationKind should be of same size as a uint8_t");
   llvm::DenseMap<std::pair<SymbolID, uint8_t>, std::vector<SymbolID>> 
Relations;

_______________________________________________
cfe-commits mailing list
[email protected]
https://lists.llvm.org/cgi-bin/mailman/listinfo/cfe-commits

Reply via email to