[PATCH] D93452: [clangd] Trim memory after buildINdex

Quentin Chateau via Phabricator via cfe-commits cfe-commits at lists.llvm.org
Thu Dec 17 07:50:26 PST 2020


qchateau updated this revision to Diff 312500.
qchateau added a comment.
Herald added a subscriber: mgorny.

Add macro to use malloc_trim only if available


Repository:
  rG LLVM Github Monorepo

CHANGES SINCE LAST ACTION
  https://reviews.llvm.org/D93452/new/

https://reviews.llvm.org/D93452

Files:
  clang-tools-extra/clangd/CMakeLists.txt
  clang-tools-extra/clangd/index/FileIndex.cpp


Index: clang-tools-extra/clangd/index/FileIndex.cpp
===================================================================
--- clang-tools-extra/clangd/index/FileIndex.cpp
+++ clang-tools-extra/clangd/index/FileIndex.cpp
@@ -32,6 +32,7 @@
 #include "llvm/ADT/DenseMap.h"
 #include "llvm/ADT/Optional.h"
 #include "llvm/ADT/STLExtras.h"
+#include "llvm/ADT/ScopeExit.h"
 #include "llvm/ADT/StringMap.h"
 #include "llvm/ADT/StringRef.h"
 #include "llvm/Support/Error.h"
@@ -41,10 +42,21 @@
 #include <utility>
 #include <vector>
 
+#ifdef HAVE_MALLOC_TRIM
+#include <malloc.h>
+#endif // HAVE_MALLOC_TRIM
+
 namespace clang {
 namespace clangd {
 namespace {
 
+void trimMemory() {
+#ifdef HAVE_MALLOC_TRIM
+  if (malloc_trim(0))
+    vlog("Trimmed memory");
+#endif // HAVE_MALLOC_TRIM
+}
+
 SlabTuple indexSymbols(ASTContext &AST, std::shared_ptr<Preprocessor> PP,
                        llvm::ArrayRef<Decl *> DeclsToIndex,
                        const MainFileMacros *MacroRefsToIndex,
@@ -263,6 +275,10 @@
 std::unique_ptr<SymbolIndex>
 FileSymbols::buildIndex(IndexType Type, DuplicateHandling DuplicateHandle,
                         size_t *Version) {
+  // Building the index often leaves a lof of freed but unreleased memory
+  // (at the OS level): manually trim the memory after the index is built
+  auto _ = llvm::make_scope_exit(trimMemory);
+
   std::vector<std::shared_ptr<SymbolSlab>> SymbolSlabs;
   std::vector<std::shared_ptr<RefSlab>> RefSlabs;
   std::vector<std::shared_ptr<RelationSlab>> RelationSlabs;
Index: clang-tools-extra/clangd/CMakeLists.txt
===================================================================
--- clang-tools-extra/clangd/CMakeLists.txt
+++ clang-tools-extra/clangd/CMakeLists.txt
@@ -154,6 +154,13 @@
   clangdSupport
   )
 
+check_symbol_exists(malloc_trim malloc.h HAVE_MALLOC_TRIM)
+if (HAVE_MALLOC_TRIM)
+  target_compile_definitions(obj.clangDaemon
+    PRIVATE HAVE_MALLOC_TRIM
+    )
+endif ()
+
 add_subdirectory(refactor/tweaks)
 if (${CMAKE_SYSTEM_NAME} STREQUAL "Linux")
   # FIXME: Make fuzzer not use linux-specific APIs, build it everywhere.


-------------- next part --------------
A non-text attachment was scrubbed...
Name: D93452.312500.patch
Type: text/x-patch
Size: 2105 bytes
Desc: not available
URL: <http://lists.llvm.org/pipermail/cfe-commits/attachments/20201217/952a13f2/attachment.bin>


More information about the cfe-commits mailing list