[lld] 11d1aa0 - [COFF] Free some memory used for chunks

Reid Kleckner via llvm-commits llvm-commits at lists.llvm.org
Mon Jun 1 18:51:56 PDT 2020


Author: Reid Kleckner
Date: 2020-06-01T18:51:47-07:00
New Revision: 11d1aa0bcc1197f1b3010171b02c6e9662f34b75

URL: https://github.com/llvm/llvm-project/commit/11d1aa0bcc1197f1b3010171b02c6e9662f34b75
DIFF: https://github.com/llvm/llvm-project/commit/11d1aa0bcc1197f1b3010171b02c6e9662f34b75.diff

LOG: [COFF] Free some memory used for chunks

First, do not reserve numSections in the Chunks array. In cases where
there are many non-prevailing sections, this will overallocate memory
which will not be used.

Second, free the memory for sparseChunks after initializeSymbols. After
that, it is never used.

This saves 50MB of 627MB for my use case without affecting performance.

Added: 
    

Modified: 
    lld/COFF/InputFiles.cpp
    lld/COFF/InputFiles.h

Removed: 
    


################################################################################
diff  --git a/lld/COFF/InputFiles.cpp b/lld/COFF/InputFiles.cpp
index 3d4a27b60743..0adc2b91bd99 100644
--- a/lld/COFF/InputFiles.cpp
+++ b/lld/COFF/InputFiles.cpp
@@ -215,7 +215,6 @@ static SectionChunk *const pendingComdat = reinterpret_cast<SectionChunk *>(1);
 
 void ObjFile::initializeChunks() {
   uint32_t numSections = coffObj->getNumberOfSections();
-  chunks.reserve(numSections);
   sparseChunks.resize(numSections + 1);
   for (uint32_t i = 1; i < numSections + 1; ++i) {
     const coff_section *sec = getSection(i);
@@ -458,6 +457,9 @@ void ObjFile::initializeSymbols() {
     uint32_t idx = kv.second;
     checkAndSetWeakAlias(symtab, this, sym, symbols[idx]);
   }
+
+  // Free the memory used by sparseChunks now that symbol loading is finished.
+  decltype(sparseChunks)().swap(sparseChunks);
 }
 
 Symbol *ObjFile::createUndefined(COFFSymbolRef sym) {

diff  --git a/lld/COFF/InputFiles.h b/lld/COFF/InputFiles.h
index a8ddf29eee04..50323f596e2c 100644
--- a/lld/COFF/InputFiles.h
+++ b/lld/COFF/InputFiles.h
@@ -285,19 +285,19 @@ class ObjFile : public InputFile {
   std::vector<SectionChunk *> guardFidChunks;
   std::vector<SectionChunk *> guardLJmpChunks;
 
-  // This vector contains the same chunks as Chunks, but they are
-  // indexed such that you can get a SectionChunk by section index.
-  // Nonexistent section indices are filled with null pointers.
-  // (Because section number is 1-based, the first slot is always a
-  // null pointer.)
-  std::vector<SectionChunk *> sparseChunks;
-
   // This vector contains a list of all symbols defined or referenced by this
   // file. They are indexed such that you can get a Symbol by symbol
   // index. Nonexistent indices (which are occupied by auxiliary
   // symbols in the real symbol table) are filled with null pointers.
   std::vector<Symbol *> symbols;
 
+  // This vector contains the same chunks as Chunks, but they are
+  // indexed such that you can get a SectionChunk by section index.
+  // Nonexistent section indices are filled with null pointers.
+  // (Because section number is 1-based, the first slot is always a
+  // null pointer.) This vector is only valid during initialization.
+  std::vector<SectionChunk *> sparseChunks;
+
   DWARFCache *dwarf = nullptr;
 };
 


        


More information about the llvm-commits mailing list